Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Created December 15, 2013 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfoxall/7976631 to your computer and use it in GitHub Desktop.
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
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