4 # :title: lastfm plugin for rbot
6 # Author:: Jeremy Voorhis
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 # Copyright:: (C) 2005 Jeremy Voorhis
10 # Copyright:: (C) 2007 Giuseppe Bilotta
16 class LastFmPlugin < Plugin
18 LASTFM = "http://www.last.fm"
20 def help(plugin, topic="")
23 "lastfm artist <name> => show information on artist/group <name> from last.fm"
25 "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
27 "lastfm album <name> => show information on album <name> from last.fm"
29 "lastfm <function> <user> => lastfm data for <user> on last.fm where <function> in [recenttracks, topartists, topalbums, toptracks, tags, friends, neighbors]. other topics: artist, group, song, track, album"
34 action = params[:action].intern
35 action = :neighbours if action == :neighbors
41 esc = URI.escape(artist)
42 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
44 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
46 title = $2.ircify_html
48 raise "No URL/Title found for #{artist}"
51 wiki = "This #{action} doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
52 if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
56 m.reply "%s : %s\n%s" % [title, url, wiki]
58 m.reply "no data found on #{artist}"
61 m.reply "I had problems looking for #{artist}"
63 debug e.backtrace.join("\n")
64 debug page[0...10*1024]
68 m.reply "not implemented yet, sorry"
70 m.reply "not implemented yet, sorry"
72 return usage(m) unless what.length == 1
75 data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
76 m.reply "#{action} for #{user}:"
77 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
79 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
85 plugin = LastFmPlugin.new
86 plugin.map 'lastfm :action *what'