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
15 SELECTOR = /<tr class="vevent.*?<\/tr>/m
17 # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
18 # TODO festival have TWO dates -------+
19 # TODO event type -------------+ |
21 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
22 attr_accessor :url, :date, :artist, :location, :attendance
23 def initialize(url, date, artist, location, attendance)
28 @attendance = attendance
33 return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @url]
35 return "%s %s @ %s (%s) %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @attendance, @url]
38 alias :to_s :compact_display
42 class LastFmPlugin < Plugin
43 Config.register Config::IntegerValue.new('lastfm.max_events',
44 :default => 25, :validate => Proc.new{|v| v > 1},
45 :desc => "Maximum number of events to display.")
46 Config.register Config::IntegerValue.new('lastfm.default_events',
47 :default => 3, :validate => Proc.new{|v| v > 1},
48 :desc => "Default number of events to display.")
50 LASTFM = "http://www.last.fm"
52 def help(plugin, topic="")
53 case (topic.intern rescue nil)
55 "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']}"
57 "lastfm artist <name> => show information on artist/group <name> from last.fm"
59 "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
61 "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
63 "lastfm now <user> => show the now playing track from last.fm"
65 "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"
69 def find_event(m, params)
70 num = params[:num] || @bot.config['lastfm.default_events']
71 num = num.to_i.clip(1, @bot.config['lastfm.max_events'])
73 location = artist = nil
74 location = params[:location].to_s if params[:location]
75 artist = params[:who].to_s if params[:who]
77 spec = location ? "in #{location}" : "by #{artist}"
78 query = location ? "?findloc=#{CGI.escape(location)}" : "?s=#{CGI.escape(artist)}&findloc="
80 page = @bot.httputil.get LASTFM + "/events/" + query
83 disp_events = Array.new
85 pre_events = page.scan(LastFmEvent::SELECTOR)
86 # debug pre_events.inspect
88 # We may not find any even because the page gives a list
89 # of locations instead. In this case, retry with the first of
91 if page.match(/<a href="(\/events\/\?l=[^"]+)">/)
92 debug "Rechecking with #{$1}"
93 page = @bot.httputil.get(LASTFM+$1)
95 pre_events = page.scan(LastFmEvent::SELECTOR) if page
99 m.reply "No events found #{spec}, sorry"
103 pre_events.each { |s| s.scan(LastFmEvent::MATCHER) { |day, month, year, url_who, who, url_where, where, how_many|
104 date = Time.utc(year.to_i, month.to_i, day.to_i)
105 url = LASTFM + url_who
106 if who.match(/<strong>(.*?)<\/strong>(.+)?/)
107 artist = Bold + $1.ircify_html + Bold
108 artist << ", " << $2.ircify_html if $2
110 debug "who: #{who.inspect}"
111 artist = who.ircify_html
113 if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
114 loc = Bold + $1.ircify_html + Bold
115 loc << ", " << $2.ircify_html if $2
118 loc = where.ircify_html
120 attendance = how_many ? how_many.ircify_html : ''
121 events << LastFmEvent.new(url, date, artist, loc, attendance)
123 # debug events.inspect
125 events[0...num].each { |event|
126 disp_events << event.to_s
128 m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
130 m.reply "No events found #{spec}"
133 rescue Exception => e
134 m.reply "I had problems looking for events #{spec}"
136 debug e.backtrace.join("\n")
137 debug page[0...10*1024] if page
142 def now_playing(m, params)
143 opts = { :cache => false }
144 user = params[:who].to_s
147 page = @bot.httputil.get("#{LASTFM}/user/#{user}", opts)
149 if page.match(/class="nowListening">\s*<td class="subject">\s*<a href="\/music.*?">(.*)<\/a>\s*<\/td/)
150 m.reply "#{user} is jammin to #{$1.ircify_html}"
152 m.reply "#{user} isn't listening to anything right now."
156 m.reply "I had problems getting #{user}'s current info"
160 def find_artist(m, params)
161 artist = params[:who].to_s
164 esc = URI.escape(CGI.escape(artist))
165 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
167 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
169 title = $2.ircify_html
171 raise "No URL/Title found for #{artist}"
174 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
175 if page.match(/<div (?:class|id)="wikiAbstract">(.*?)<\/div>/m)
176 wiki = $1.ircify_html
179 m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
181 m.reply "no data found on #{artist}"
184 rescue Exception => e
185 m.reply "I had problems looking for #{artist}"
187 debug e.backtrace.join("\n")
188 debug page[0...10*1024] if page
193 def find_track(m, params)
194 m.reply "not implemented yet, sorry"
197 def find_album(m, params)
198 m.reply "not implemented yet, sorry"
201 def lastfm(m, params)
202 action = params[:action].intern
203 action = :neighbours if action == :neighbors
206 data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
207 m.reply "#{action} for #{user}:"
208 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
210 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
215 plugin = LastFmPlugin.new
216 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
217 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
218 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
219 plugin.map 'lastfm artist *who', :action => :find_artist, :thread => true
220 plugin.map 'lastfm group *who', :action => :find_artist, :thread => true
221 plugin.map 'lastfm now *who', :action => :now_playing, :thread => true
222 plugin.map 'lastfm track *dunno', :action => :find_track
223 plugin.map 'lastfm song *dunno', :action => :find_track
224 plugin.map 'lastfm album *dunno', :action => :find_album
225 plugin.map 'lastfm :action *user', :thread => true