Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'pp'
- require 'rubygems'
- require 'json'
- class Hash
- def to_http_params
- results = []
- return results if self.empty?
- self.each do |key, val|
- results << "#{key}=#{URI.encode(val.to_s)}"
- end
- results.join('&')
- end
- end
- pp ARGV
- username = ARGV[0]
- password = ARGV[1]
- api_key = ARGV[2]
- file = ARGV[3]
- list_name = ARGV[4]
- cue_lang = ARGV[5]
- response_lang = ARGV[6]
- print "#{username}, #{password}, #{api_key}, #{file}, #{list_name}, #{cue_lang}, #{response_lang}\n"
- list_id = 0
- Net::HTTP.version_1_2
- # Look for the user's lists
- req = Net::HTTP::Get.new("/users/#{username}/lists/creator.json?per_page=100&include_private=true")
- req.basic_auth(username, password)
- Net::HTTP.start('api.smart.fm', 80) {|http|
- res = http.request(req)
- JSON.parse(res.body).each { |item|
- print "id: #{item['id']}\n"
- # Use the id if it matches
- if item['title'] == list_name
- list_id = item['id']
- end
- }
- http.finish()
- }
- if list_id == 0
- # Create the list
- sleep 1
- req = Net::HTTP::Post.new('/lists')
- req.basic_auth(username, password)
- params = {
- :api_key => api_key,
- 'list[name]' => list_name,
- 'list[language]' => cue_lang,
- 'list[translation_language]' => response_lang,
- }
- req.body = params.to_http_params
- Net::HTTP.start('api.smart.fm', 80) {|http|
- res = http.request(req)
- puts "list: #{res.body}\n"
- if ! res || ! res.body || "#{res.body.to_i}" != res.body
- exit 1
- end
- list_id = res.body.to_i
- http.finish()
- }
- end
- f = File.open(file, "r")
- lines = f.readlines
- lines.each { |line|
- line = line.chomp
- bits = line.split("\t")
- cue = bits[0]
- response = bits[1]
- pos = bits[2]
- pp bits
- # Create the item
- sleep 1
- req = nil
- params = nil
- req = Net::HTTP::Post.new('/items')
- req.basic_auth(username, password)
- params = {
- :api_key => api_key,
- 'response[text]' => response,
- 'response[language]' => response_lang,
- 'cue[text]' => cue,
- 'cue[language]' => cue_lang,
- 'list_id' => list_id,
- }
- pp params
- req.body = params.to_http_params
- Net::HTTP.start('api.smart.fm', 80) {|http|
- res = http.request(req)
- pp res
- puts "new item: #{res.body}\n"
- if ! res || ! res.body || "#{res.body.to_i}" != res.body
- exit 1
- end
- item_id = res.body.to_i
- http.finish()
- # # Add the sound
- # sleep 1
- # req = Net::HTTP::Post.new("/items/#{res.body}/sounds")
- # req.basic_auth(username, password)
- #
- # params = {
- # :api_key => api_key,
- # 'sound[file]' => "#{cue}.mp3",
- # 'sound[text]' => cue,
- # 'sound[language]' => cue_lang,
- # }
- #
- # req.body = params.to_http_params
- #
- # Net::HTTP.start('api.smart.fm', 80) {|http|
- # res = http.request(req)
- # puts "sound addition: #{res.body}\n"
- # }
- }
- }
Add Comment
Please, Sign In to add comment