Skip to content

Instantly share code, notes, and snippets.

@butlermh
Created June 12, 2011 13:06
Show Gist options
  • Save butlermh/1021527 to your computer and use it in GitHub Desktop.
Save butlermh/1021527 to your computer and use it in GitHub Desktop.
Ruby code to get JSON data from MusicBrainz and post it to a local NoSQL / Search database e.g. CouchDB, ElasticSearch
#!/usr/bin/ruby
#Retrieve data from MusicBrainz in JSON format and upload it to server
require 'rubygems'
require 'json'
require 'open-uri'
require 'active_support'
require 'net/http'
$KCODE = "UTF8"
def search_and_post(artist, host, port, path)
base_uri = 'http://search.test.musicbrainz.org/ws/2/recording/'
json = open(base_uri + "?&offset=0&max=1000&fmt=json&query=artist:#{artist}").read
pJson = ActiveSupport::JSON.decode(json)
pJson['recording-list']['recording'].each do |r|
req = Net::HTTP::Post.new(path)
req.body = ActiveSupport::JSON.encode(r)
req["content-type"] = "application/json"
response = Net::HTTP.start(host, port){ |http|http.request(req) }
puts "Response #{response.code} #{response.message}: #{response.body}"
end
end
unless ARGV.length == 4
puts "Usage: #{$0} [search term] [host] [port] [path]"
exit
end
search_and_post(ARGV[0], ARGV[1], ARGV[2], ARGV[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment