Created
December 15, 2013 18:50
-
-
Save benfoxall/7976631 to your computer and use it in GitHub Desktop.
this could be a lot faster by firing off parallel requests with something like http://rubydoc.info/github/typhoeus/typhoeus - though this is for one-off use, so should be okay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'rexml/document' | |
require 'CSV' | |
user = 'benjaminf' | |
key = '' # public api key | |
page = 0 | |
total = 1 # set properly by response | |
CSV.open("#{user}.csv", "wb") do |csv| | |
while page < total | |
url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=#{user}&api_key=#{key}&limit=200&page=#{page}" | |
xml_data = Net::HTTP.get_response(URI.parse(url)).body | |
doc = REXML::Document.new(xml_data) | |
doc.elements.each('lfm/recenttracks/track') do |ele| | |
csv << [ | |
user, | |
ele.get_text('date'), | |
ele.get_text('artist'), | |
ele.get_text('album'), | |
ele.get_text('name'), # track | |
ele.get_text('url') | |
] | |
end | |
total = doc.elements.each('lfm/recenttracks') {|t| t}.first.attributes['totalPages'].to_i | |
puts "loaded page #{page} of #{total}" | |
page += 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment