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 <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"
67 def find_event(m, params)
68 num = params[:num] || @bot.config['lastfm.default_events']
69 num = num.to_i.clip(1, @bot.config['lastfm.max_events'])
71 location = artist = nil
72 location = params[:location].to_s if params[:location]
73 artist = params[:who].to_s if params[:who]
75 spec = location ? "in #{location}" : "by #{artist}"
76 query = location ? "?findloc=#{CGI.escape(location)}" : "?s=#{CGI.escape(artist)}&findloc="
78 page = @bot.httputil.get LASTFM + "/events/" + query
81 disp_events = Array.new
83 pre_events = page.scan(LastFmEvent::SELECTOR)
84 # debug pre_events.inspect
86 # We may not find any even because the page gives a list
87 # of locations instead. In this case, retry with the first of
89 if page.match(/<a href="(\/events\/\?l=[^"]+)">/)
90 debug "Rechecking with #{$1}"
91 page = @bot.httputil.get(LASTFM+$1)
93 pre_events = page.scan(LastFmEvent::SELECTOR) if page
97 m.reply "No events found #{spec}, sorry"
101 pre_events.each { |s| s.scan(LastFmEvent::MATCHER) { |day, month, year, url_who, who, url_where, where, how_many|
102 date = Time.utc(year.to_i, month.to_i, day.to_i)
103 url = LASTFM + url_who
104 if who.match(/<strong>(.*?)<\/strong>(.+)?/)
105 artist = Bold + $1.ircify_html + Bold
106 artist << ", " << $2.ircify_html if $2
108 debug "who: #{who.inspect}"
109 artist = who.ircify_html
111 if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
112 loc = Bold + $1.ircify_html + Bold
113 loc << ", " << $2.ircify_html if $2
116 loc = where.ircify_html
118 attendance = how_many ? how_many.ircify_html : ''
119 events << LastFmEvent.new(url, date, artist, loc, attendance)
121 # debug events.inspect
123 events[0...num].each { |event|
124 disp_events << event.to_s
126 m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
128 m.reply "No events found #{spec}"
131 rescue Exception => e
132 m.reply "I had problems looking for events #{spec}"
134 debug e.backtrace.join("\n")
135 debug page[0...10*1024] if page
140 def now_playing(m, params)
141 opts = { :cache => false }
142 user = params[:who].to_s
145 page = @bot.httputil.get("#{LASTFM}/user/#{user}", opts)
147 if page.match(/class="nowListening">\s*<td class="subject">\s*<a href="\/music.*?">(.*)<\/a>\s*<\/td/)
148 m.reply "#{user} is jammin to #{$1.ircify_html}"
150 m.reply "#{user} isn't listening to anything right now."
154 m.reply "I had problems getting #{user}'s current info"
158 def find_artist(m, params)
159 artist = params[:who].to_s
162 esc = URI.escape(CGI.escape(artist))
163 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
165 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
167 title = $2.ircify_html
169 raise "No URL/Title found for #{artist}"
172 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
173 if page.match(/<div (?:class|id)="wikiAbstract">(.*?)<\/div>/m)
174 wiki = $1.ircify_html
177 m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
179 m.reply "no data found on #{artist}"
182 rescue Exception => e
183 m.reply "I had problems looking for #{artist}"
185 debug e.backtrace.join("\n")
186 debug page[0...10*1024] if page
191 def find_track(m, params)
192 m.reply "not implemented yet, sorry"
195 def find_album(m, params)
196 m.reply "not implemented yet, sorry"
199 def lastfm(m, params)
200 action = params[:action].intern
201 action = :neighbours if action == :neighbors
204 data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
205 m.reply "#{action} for #{user}:"
206 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
208 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
213 plugin = LastFmPlugin.new
214 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
215 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
216 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
217 plugin.map 'lastfm artist *who', :action => :find_artist, :thread => true
218 plugin.map 'lastfm group *who', :action => :find_artist, :thread => true
219 plugin.map 'lastfm now *who', :action => :now_playing, :thread => true
220 plugin.map 'lastfm track *dunno', :action => :find_track
221 plugin.map 'lastfm song *dunno', :action => :find_track
222 plugin.map 'lastfm album *dunno', :action => :find_album
223 plugin.map 'lastfm :action *user', :thread => true