Guest User

Untitled

a guest
Jul 24th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. require 'pp'
  2. require 'rubygems'
  3. require 'json'
  4.  
  5. class Hash
  6. def to_http_params
  7. results = []
  8. return results if self.empty?
  9. self.each do |key, val|
  10. results << "#{key}=#{URI.encode(val.to_s)}"
  11. end
  12. results.join('&')
  13. end
  14. end
  15.  
  16. pp ARGV
  17.  
  18. username = ARGV[0]
  19. password = ARGV[1]
  20. api_key = ARGV[2]
  21. file = ARGV[3]
  22. list_name = ARGV[4]
  23. cue_lang = ARGV[5]
  24. response_lang = ARGV[6]
  25.  
  26. print "#{username}, #{password}, #{api_key}, #{file}, #{list_name}, #{cue_lang}, #{response_lang}\n"
  27.  
  28. list_id = 0
  29.  
  30. Net::HTTP.version_1_2
  31.  
  32. # Look for the user's lists
  33. req = Net::HTTP::Get.new("/users/#{username}/lists/creator.json?per_page=100&include_private=true")
  34. req.basic_auth(username, password)
  35.  
  36. Net::HTTP.start('api.smart.fm', 80) {|http|
  37. res = http.request(req)
  38.  
  39. JSON.parse(res.body).each { |item|
  40. print "id: #{item['id']}\n"
  41.  
  42. # Use the id if it matches
  43. if item['title'] == list_name
  44. list_id = item['id']
  45. end
  46. }
  47.  
  48. http.finish()
  49. }
  50.  
  51. if list_id == 0
  52. # Create the list
  53. sleep 1
  54. req = Net::HTTP::Post.new('/lists')
  55. req.basic_auth(username, password)
  56.  
  57. params = {
  58. :api_key => api_key,
  59. 'list[name]' => list_name,
  60. 'list[language]' => cue_lang,
  61. 'list[translation_language]' => response_lang,
  62. }
  63.  
  64. req.body = params.to_http_params
  65.  
  66. Net::HTTP.start('api.smart.fm', 80) {|http|
  67. res = http.request(req)
  68. puts "list: #{res.body}\n"
  69.  
  70. if ! res || ! res.body || "#{res.body.to_i}" != res.body
  71. exit 1
  72. end
  73.  
  74. list_id = res.body.to_i
  75.  
  76. http.finish()
  77. }
  78. end
  79.  
  80. f = File.open(file, "r")
  81. lines = f.readlines
  82. lines.each { |line|
  83.  
  84. line = line.chomp
  85.  
  86. bits = line.split("\t")
  87. cue = bits[0]
  88. response = bits[1]
  89. pos = bits[2]
  90.  
  91. pp bits
  92.  
  93. # Create the item
  94. sleep 1
  95. req = nil
  96. params = nil
  97. req = Net::HTTP::Post.new('/items')
  98. req.basic_auth(username, password)
  99.  
  100. params = {
  101. :api_key => api_key,
  102. 'response[text]' => response,
  103. 'response[language]' => response_lang,
  104. 'cue[text]' => cue,
  105. 'cue[language]' => cue_lang,
  106. 'list_id' => list_id,
  107. }
  108.  
  109. pp params
  110.  
  111. req.body = params.to_http_params
  112.  
  113. Net::HTTP.start('api.smart.fm', 80) {|http|
  114. res = http.request(req)
  115. pp res
  116. puts "new item: #{res.body}\n"
  117.  
  118. if ! res || ! res.body || "#{res.body.to_i}" != res.body
  119. exit 1
  120. end
  121.  
  122. item_id = res.body.to_i
  123.  
  124. http.finish()
  125.  
  126. # # Add the sound
  127. # sleep 1
  128. # req = Net::HTTP::Post.new("/items/#{res.body}/sounds")
  129. # req.basic_auth(username, password)
  130. #
  131. # params = {
  132. # :api_key => api_key,
  133. # 'sound[file]' => "#{cue}.mp3",
  134. # 'sound[text]' => cue,
  135. # 'sound[language]' => cue_lang,
  136. # }
  137. #
  138. # req.body = params.to_http_params
  139. #
  140. # Net::HTTP.start('api.smart.fm', 80) {|http|
  141. # res = http.request(req)
  142. # puts "sound addition: #{res.body}\n"
  143. # }
  144. }
  145. }
Add Comment
Please, Sign In to add comment