4 # :title: lastfm plugin for rbot
6 # Author:: Jeremy Voorhis
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 # Author:: Casey Link <unnamedrambler@gmail.com>
10 # Copyright:: (C) 2005 Jeremy Voorhis
11 # Copyright:: (C) 2007 Giuseppe Bilotta
12 # Copyright:: (C) 2008 Casey Link
17 SELECTOR = /<tr class="vevent.*?<\/tr>/m
19 # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
20 # TODO festival have TWO dates -------+
21 # TODO event type -------------+ |
23 MATCHER = /<tr class="vevent\s+\w+\s+(?:\S+\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>(?:(.*?) attending\s+)?.*?<\/td>\s+<\/tr>/m
24 attr_accessor :url, :date, :artist, :location, :attendance
25 def initialize(url, date, artist, location, attendance)
30 @attendance = attendance
35 return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @url]
37 return "%s %s @ %s (%s) %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @attendance, @url]
40 alias :to_s :compact_display
44 class LastFmPlugin < Plugin
45 Config.register Config::IntegerValue.new('lastfm.max_events',
46 :default => 25, :validate => Proc.new{|v| v > 1},
47 :desc => "Maximum number of events to display.")
48 Config.register Config::IntegerValue.new('lastfm.default_events',
49 :default => 3, :validate => Proc.new{|v| v > 1},
50 :desc => "Default number of events to display.")
52 LASTFM = "http://www.last.fm"
66 def help(plugin, topic="")
67 case (topic.intern rescue nil)
69 "lastfm [<num>] events in <location> => show information on events in or near <location>. lastfm [<num>] events by <artist/group> => show information on events by <artist/group>. The number of events <num> that can be displayed is optional, defaults to #{@bot.config['lastfm.default_events']} and cannot be higher than #{@bot.config['lastfm.max_events']}"
71 "lastfm artist <name> => show information on artist/group <name> from last.fm"
73 "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
75 "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
77 "lastfm now [<user>] => show the now playing track from last.fm"
79 "lastfm set <user> => associate your current irc nick with a last.fm user"
81 "lastfm who [<nick>] => show who <nick> is at last.fm. if <nick> is empty, show who you are at lastfm."
83 "lastfm => show your now playing track at lastfm. 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, now, set, who"
87 def find_event(m, params)
88 num = params[:num] || @bot.config['lastfm.default_events']
89 num = num.to_i.clip(1, @bot.config['lastfm.max_events'])
91 location = artist = nil
92 location = params[:location].to_s if params[:location]
93 artist = params[:who].to_s if params[:who]
95 spec = location ? "in #{location}" : "by #{artist}"
96 query = location ? "?findloc=#{CGI.escape(location)}" : "?s=#{CGI.escape(artist)}&findloc="
98 page = @bot.httputil.get LASTFM + "/events/" + query
101 disp_events = Array.new
103 pre_events = page.scan(LastFmEvent::SELECTOR)
104 # debug pre_events.inspect
106 # We may not find any even because the page gives a list
107 # of locations instead. In this case, retry with the first of
109 if page.match(/<a href="(\/events\/\?l=[^"]+)">/)
110 debug "Rechecking with #{$1}"
111 page = @bot.httputil.get(LASTFM+$1)
113 pre_events = page.scan(LastFmEvent::SELECTOR) if page
117 m.reply "No events found #{spec}, sorry"
121 pre_events.each { |s| s.scan(LastFmEvent::MATCHER) { |day, month, year, url_who, who, url_where, where, how_many|
122 date = Time.utc(year.to_i, month.to_i, day.to_i)
123 url = LASTFM + url_who
124 if who.match(/<strong>(.*?)<\/strong>(.+)?/)
125 artist = Bold + $1.ircify_html + Bold
126 artist << ", " << $2.ircify_html if $2
128 debug "who: #{who.inspect}"
129 artist = who.ircify_html
131 if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
132 loc = Bold + $1.ircify_html + Bold
133 loc << ", " << $2.ircify_html if $2
136 loc = where.ircify_html
138 attendance = how_many ? how_many.ircify_html : ''
139 events << LastFmEvent.new(url, date, artist, loc, attendance)
141 # debug events.inspect
143 events[0...num].each { |event|
144 disp_events << event.to_s
146 m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
148 m.reply "No events found #{spec}"
151 rescue Exception => e
152 m.reply "I had problems looking for events #{spec}"
154 debug e.backtrace.join("\n")
155 debug page[0...10*1024] if page
160 def now_playing(m, params)
161 opts = { :cache => false }
164 user = params[:who].to_s
165 elsif @registry.has_key? m.sourcenick
166 user = @registry[ m.sourcenick ]
168 # m.reply "I don't know who you are on last.fm. Use 'lastfm set username' to identify yourself."
174 page = @bot.httputil.get("#{LASTFM}/user/#{user}", opts)
176 if page.match(/class="nowListening">\s*<td class="subject">\s*<a href="\/music.*?">(.*)<\/a>\s*<\/td/)
178 if page.match(/class="nowListening currentStation">\s*(.*?)<\/a>/m)
179 m.reply "#{user} is #{$1.ircify_html}"
181 m.reply "#{user} is jammin to #{track.ircify_html}"
182 elsif page.match(/class="justlistened first">\s*<td class="subject">.*<\/span><\/a>?(.*)<\/a>\s*<\/td>\s*<td class="date">\s*just/m)
183 m.reply "#{user} just jammed to #{$1.ircify_html}"
185 params[:action] = "recenttracks"
190 return if params[:recurs]
191 if @registry.has_key? user
192 params[:who] = @registry[ user ]
193 params[:recurs] = true
194 now_playing(m, params)
196 m.reply "#{user} doesn't exist at last.fm. Perhaps you need to: lastfm set <username>"
200 m.reply "I had problems getting #{user}'s current info"
204 def find_artist(m, params)
205 artist = params[:who].to_s
208 esc = URI.escape(CGI.escape(artist))
209 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
211 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
213 title = $2.ircify_html
215 raise "No URL/Title found for #{artist}"
218 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
219 if page.match(/<div (?:class|id)="wikiAbstract">(.*?)<\/div>/m)
220 wiki = $1.ircify_html
223 m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
225 m.reply "no data found on #{artist}"
228 rescue Exception => e
229 m.reply "I had problems looking for #{artist}"
231 debug e.backtrace.join("\n")
232 debug page[0...10*1024] if page
237 def find_track(m, params)
238 m.reply "not implemented yet, sorry"
241 def find_album(m, params)
242 m.reply "not implemented yet, sorry"
245 def set_user(m, params)
246 user = params[:who].to_s
248 @registry[ nick ] = user
249 m.reply "Ok, I'll remember that #{nick} is #{user} at last.fm"
252 def get_user(m, params)
255 nick = params[:who].to_s
259 if @registry.has_key? nick
260 user = @registry[ nick ]
261 m.reply "#{nick} is #{user} at last.fm"
263 m.reply "Sorry, I don't know who #{nick} is at last.fm perhaps you need to: lastfm set <username>"
267 def lastfm(m, params)
268 action = params[:action].intern
269 action = :neighbours if action == :neighbors
271 if params[:user] then
272 user = params[:user].to_s
273 elsif @registry.has_key? m.sourcenick
274 user = @registry[ m.sourcenick ]
276 # m.reply "I don't know who you are on last.fm. Use 'lastfm set username' to identify yourself."
281 data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
282 m.reply "#{action} for #{user}:"
283 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
285 m.reply "could not find #{action} for #{user} (is #{user} a user?). perhaps you need to: lastfm set <username>"
290 plugin = LastFmPlugin.new
291 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
292 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
293 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
294 plugin.map 'lastfm artist *who', :action => :find_artist, :thread => true
295 plugin.map 'lastfm group *who', :action => :find_artist, :thread => true
296 plugin.map 'lastfm now *who', :action => :now_playing, :thread => true
297 plugin.map 'lastfm now', :action => :now_playing, :thread => true
298 plugin.map 'lastfm track *dunno', :action => :find_track
299 plugin.map 'lastfm song *dunno', :action => :find_track
300 plugin.map 'lastfm album *dunno', :action => :find_album
301 plugin.map 'lastfm set *who', :action => :set_user, :thread => true
302 plugin.map 'lastfm who *who', :action => :get_user, :thread => true
303 plugin.map 'lastfm who', :action => :get_user, :thread => true
304 plugin.map 'lastfm :action *user', :thread => true
305 plugin.map 'lastfm :action', :thread => true
306 plugin.map 'lastfm', :action => :now_playing, :thread => true