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 ) then
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."
173 page = @bot.httputil.get("#{LASTFM}/user/#{user}", opts)
175 if page.match(/class="nowListening">\s*<td class="subject">\s*<a href="\/music.*?">(.*)<\/a>\s*<\/td/)
176 m.reply "#{user} is jammin to #{$1.ircify_html}"
178 m.reply "#{user} isn't listening to anything right now."
182 m.reply "I had problems getting #{user}'s current info"
186 def find_artist(m, params)
187 artist = params[:who].to_s
190 esc = URI.escape(CGI.escape(artist))
191 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
193 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
195 title = $2.ircify_html
197 raise "No URL/Title found for #{artist}"
200 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
201 if page.match(/<div (?:class|id)="wikiAbstract">(.*?)<\/div>/m)
202 wiki = $1.ircify_html
205 m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
207 m.reply "no data found on #{artist}"
210 rescue Exception => e
211 m.reply "I had problems looking for #{artist}"
213 debug e.backtrace.join("\n")
214 debug page[0...10*1024] if page
219 def find_track(m, params)
220 m.reply "not implemented yet, sorry"
223 def find_album(m, params)
224 m.reply "not implemented yet, sorry"
227 def set_user(m, params)
228 user = params[:who].to_s
230 @registry[ nick ] = user
231 m.reply "Ok, I'll remember that #{nick} is #{user} at last.fm"
234 def get_user(m, params)
237 nick = params[:who].to_s
241 if @registry.has_key?( nick ) then
242 user = @registry[ nick ]
243 m.reply "#{nick} is #{user} at last.fm"
245 m.reply "Sorry, I don't know who #{nick} is at last.fm"
249 def lastfm(m, params)
250 action = params[:action].intern
251 action = :neighbours if action == :neighbors
253 if params[:user] then
254 user = params[:user].to_s
255 elsif @registry.has_key? ( m.sourcenick ) then
256 user = @registry[ m.sourcenick ]
258 m.reply "I don't know who you are on last.fm. Use 'lastfm set username' to identify yourself."
262 data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
263 m.reply "#{action} for #{user}:"
264 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
266 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
271 plugin = LastFmPlugin.new
272 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
273 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
274 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
275 plugin.map 'lastfm artist *who', :action => :find_artist, :thread => true
276 plugin.map 'lastfm group *who', :action => :find_artist, :thread => true
277 plugin.map 'lastfm now *who', :action => :now_playing, :thread => true
278 plugin.map 'lastfm now', :action => :now_playing, :thread => true
279 plugin.map 'lastfm track *dunno', :action => :find_track
280 plugin.map 'lastfm song *dunno', :action => :find_track
281 plugin.map 'lastfm album *dunno', :action => :find_album
282 plugin.map 'lastfm set *who', :action => :set_user, :thread => true
283 plugin.map 'lastfm who *who', :action => :get_user, :thread => true
284 plugin.map 'lastfm who', :action => :get_user, :thread => true
285 plugin.map 'lastfm :action *user', :thread => true
286 plugin.map 'lastfm :action', :thread => true
287 plugin.map 'lastfm', :action => :now_playing, :thread => true