Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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
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