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
18 # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
19 REGEXP = /<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
20 attr_accessor :url, :date, :artist, :location, :attendance
21 def initialize(url, date, artist, location, attendance)
26 @attendance = attendance
31 return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @url]
33 return "%s %s @ %s (%s) %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @attendance, @url]
36 alias :to_s :compact_display
40 class LastFmPlugin < Plugin
41 BotConfig.register BotConfigIntegerValue.new('lastfm.max_events',
42 :default => 25, :validate => Proc.new{|v| v > 1},
43 :desc => "Maximum number of events to display.")
44 BotConfig.register BotConfigIntegerValue.new('lastfm.default_events',
45 :default => 3, :validate => Proc.new{|v| v > 1},
46 :desc => "Default number of events to display.")
48 LASTFM = "http://www.last.fm"
50 def help(plugin, topic="")
53 "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']}"
55 "lastfm artist <name> => show information on artist/group <name> from last.fm"
57 "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
59 "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
61 "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"
65 def find_event(m, params)
66 num = params[:num] || @bot.config['lastfm.default_events']
67 num = num.to_i.clip(1, @bot.config['lastfm.max_events'])
69 location = artist = nil
70 location = params[:location].to_s if params[:location]
71 artist = params[:who].to_s if params[:who]
73 spec = location ? "in #{location}" : "by #{artist}"
76 esc = CGI.escape(location)
77 page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
79 esc = CGI.escape(artist)
80 page = @bot.httputil.get "#{LASTFM}/events?s=#{esc}&findloc="
85 disp_events = Array.new
87 pre_events = page.scan(LastFmEvent::REGEXP)
88 # debug pre_events.inspect
90 # We may not find any even because the page gives a list
91 # of locations instead. In this case, retry with the first of
93 if page.match(/<a href="(\/events\/\?l=[^"]+)">/)
94 debug "Rechecking with #{$1}"
95 page = @bot.httputil.get(LASTFM+$1)
96 pre_events = page.scan(LastFmEvent::REGEXP) if page
99 m.reply "No events found #{spec}, sorry"
103 pre_events.each { |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.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 find_artist(m, params)
143 artist = params[:who].to_s
146 esc = URI.escape(CGI.escape(artist))
147 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
149 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
151 title = $2.ircify_html
153 raise "No URL/Title found for #{artist}"
156 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
157 if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
158 wiki = $1.ircify_html
161 m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
163 m.reply "no data found on #{artist}"
166 rescue Exception => e
167 m.reply "I had problems looking for #{artist}"
169 debug e.backtrace.join("\n")
170 debug page[0...10*1024] if page
175 def find_track(m, params)
176 m.reply "not implemented yet, sorry"
179 def find_album(m, params)
180 m.reply "not implemented yet, sorry"
183 def lastfm(m, params)
184 action = params[:action].intern
185 action = :neighbours if action == :neighbors
188 data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
189 m.reply "#{action} for #{user}:"
190 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
192 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
197 plugin = LastFmPlugin.new
198 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }
199 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }
200 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }
201 plugin.map 'lastfm artist *who', :action => :find_artist
202 plugin.map 'lastfm group *who', :action => :find_artist
203 plugin.map 'lastfm track *dunno', :action => :find_track
204 plugin.map 'lastfm song *dunno', :action => :find_track
205 plugin.map 'lastfm album *dunno', :action => :find_album
206 plugin.map 'lastfm :action *user'