Skip to content

Instantly share code, notes, and snippets.

@brettbuddin
Created November 3, 2009 19:14
Show Gist options
  • Save brettbuddin/225334 to your computer and use it in GitHub Desktop.
Save brettbuddin/225334 to your computer and use it in GitHub Desktop.
Fetches the tracks that your friends are currently listening to from Last.fm. Written for use with GeekTool.
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'xml'
USERNAME = 'brettbuddin'
USE_REAL_NAMES = true # if available
ALLOWED_USERS = [] # enter the usernames of those you'd like to see displayed
def fetch(url)
content = ''
open(url) { |s| content = s.read }
parser = XML::Parser.string(content)
parser.parse
end
begin
doc = fetch("http://ws.audioscrobbler.com/2.0/user/#{USERNAME}/friends.xml")
friends = []
doc.find('user').each do |user|
friend = {}
%w[name realname].each do |p|
friend[p.intern] = user.find(p).first.content
end
friends << friend
end
friends.each do |friend|
if ALLOWED_USERS.include?(friend[:name])
doc = fetch("http://ws.audioscrobbler.com/2.0/user/#{friend[:name]}/recenttracks.xml")
doc.find('track[@nowplaying="true"]').each do |track|
info = {}
%w[artist name album].each do |p|
info[p.intern] = track.find(p).first.content
end
friend[:listening_to] = info
end
puts "#{friend[:realname] && USE_REAL_NAMES ? friend[:realname] : friend[:name]}: #{friend[:listening_to][:artist]} - #{friend[:listening_to][:name]}" if friend[:listening_to]
end
end
rescue; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment