Created
July 19, 2009 15:23
-
-
Save dentarg/149942 to your computer and use it in GitHub Desktop.
last.fm loved tracks
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'net/http' | |
require 'rexml/document' | |
require 'rss/maker' | |
# data source | |
# http://www.lastfm.se/api/show?service=329 | |
user = "" | |
key = "" | |
url = "http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user=#{user}&api_key=#{key}" | |
# get the XML data as a string | |
xml_data = Net::HTTP.get_response(URI.parse(url)).body | |
# extract event information | |
doc = REXML::Document.new(xml_data) | |
titles = [] | |
artists = [] | |
dates = [] | |
urls = [] | |
doc.elements.each('lfm/lovedtracks/track/name') do |ele| | |
titles << ele.text | |
end | |
doc.elements.each('lfm/lovedtracks/track/date') do |ele| | |
dates << ele.text | |
end | |
doc.elements.each('lfm/lovedtracks/track/artist/name') do |ele| | |
artists << ele.text | |
end | |
doc.elements.each('lfm/lovedtracks/track/url') do |ele| | |
urls << "http://" + ele.text | |
end | |
# Create RSS feed | |
version = "2.0" # ["0.9", "1.0", "2.0"] | |
destination = "feed.xml" # local file to write | |
content = RSS::Maker.make(version) do |m| | |
m.channel.title = "Tracks loved by #{user} at Last.fm" | |
m.channel.link = "http://www.last.fm/user/#{user}" | |
m.channel.description = "Tracks loved by #{user} at Last.fm" | |
m.items.do_sort = true # sort items by date | |
# loop through tracks | |
titles.each_with_index do |title, idx| | |
i = m.items.new_item | |
i.title = "#{artists[idx]} - #{title}" | |
i.link = urls[idx] | |
i.date = Time.parse(dates[idx]) | |
end | |
end | |
File.open(destination,"w") do |f| | |
f.write(content) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment