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
17 attr_accessor :url, :date, :artist, :location, :attendance
18 def initialize(url, date, artist, location, attendance)
23 @attendance = attendance
27 class LastFmPlugin < Plugin
29 LASTFM = "http://www.last.fm"
31 def help(plugin, topic="")
34 "lastfm events in <location> => show information on events in or near <location> from last.fm"
36 "lastfm artist <name> => show information on artist/group <name> from last.fm"
38 "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
40 "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
42 "lastfm <function> <user> => lastfm data for <user> on last.fm where <function> in [recenttracks, topartists, topalbums, toptracks, tags, friends, neighbors]. other topics: events, artist, group, song, track, album"
46 def find_event(m, params)
47 location = params[:location].to_s
50 esc = URI.escape(location)
51 page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
54 disp_events = Array.new
57 # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
58 pre_events = page.scan(/<tr class="vevent\s+\w+\s+\S+?-(\d\d)-(\d\d)-(\d\d\d\d)\s*">.*?<a class="url summary" href="(\/event\/\d+)">(.*?)<\/a>.*?<a href="(\/venue\/\d+)">(.*?)<\/a>.*?<td class="attendance">(.*?)<\/td>\s+<\/tr>/m)
59 # debug pre_events.inspect
61 m.reply "No events found in #{location}, sorry"
63 pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
64 date = Time.utc(year.to_i, month.to_i, day.to_i)
65 url = LASTFM + url_who
66 if who.match(/<strong>(.*?)<\/strong>(.+)?/)
67 artist = Bold + $1.ircify_html + Bold
68 artist << ", " << $2.ircify_html if $2
70 debug "who: #{who.inspect}"
71 artist = who.ircify_html
73 if where.match(/<strong>(.*?)<\/strong>(.+)?/)
74 loc = Bold + $1.ircify_html + Bold
75 loc << ", " << $2.ircify_html if $2
78 loc = where.ircify_html
80 attendance = how_many.ircify_html
81 events << LastFmEvent.new(url, date, artist, loc, attendance)
83 # debug events.inspect
85 events[0..2].each { |event|
86 disp_events << "%s %s @ %s (%s) %s" % [event.date.strftime("%a %b, %d %Y"), event.artist, event.location, event.attendance, event.url]
88 m.reply disp_events.join(' | ')
90 m.reply "No events found in #{location}"
94 m.reply "I had problems looking for events in #{location}"
96 debug e.backtrace.join("\n")
97 debug page[0...10*1024] if page
102 def find_artist(m, params)
103 artist = params[:who].to_s
106 esc = URI.escape(artist)
107 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
109 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
111 title = $2.ircify_html
113 raise "No URL/Title found for #{artist}"
116 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
117 if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
118 wiki = $1.ircify_html
121 m.reply "%s : %s\n%s" % [title, url, wiki]
123 m.reply "no data found on #{artist}"
126 rescue Exception => e
127 m.reply "I had problems looking for #{artist}"
129 debug e.backtrace.join("\n")
130 debug page[0...10*1024] if page
135 def find_track(m, params)
136 m.reply "not implemented yet, sorry"
139 def find_album(m, params)
140 m.reply "not implemented yet, sorry"
143 def lastfm(m, params)
144 action = params[:action].intern
145 action = :neighbours if action == :neighbors
148 data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
149 m.reply "#{action} for #{user}:"
150 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
152 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
157 plugin = LastFmPlugin.new
158 plugin.map 'lastfm event[s] in :location', :action => :find_event
159 plugin.map 'lastfm artist *who', :action => :find_artist
160 plugin.map 'lastfm group *who', :action => :find_artist
161 plugin.map 'lastfm track *dunno', :action => :find_track
162 plugin.map 'lastfm song *dunno', :action => :find_track
163 plugin.map 'lastfm album *dunno', :action => :find_album
164 plugin.map 'lastfm :action *user'