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
16 # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
17 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
18 attr_accessor :url, :date, :artist, :location, :attendance
19 def initialize(url, date, artist, location, attendance)
24 @attendance = attendance
29 return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @url]
31 return "%s %s @ %s (%s) %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @attendance, @url]
34 alias :to_s :compact_display
38 class LastFmPlugin < Plugin
39 BotConfig.register BotConfigIntegerValue.new('lastfm.max_events',
40 :default => 25, :validate => Proc.new{|v| v > 1},
41 :desc => "Maximum number of events to display.")
42 BotConfig.register BotConfigIntegerValue.new('lastfm.default_events',
43 :default => 3, :validate => Proc.new{|v| v > 1},
44 :desc => "Default number of events to display.")
46 LASTFM = "http://www.last.fm"
48 def help(plugin, topic="")
49 case (topic.intern rescue nil)
51 "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']}"
53 "lastfm artist <name> => show information on artist/group <name> from last.fm"
55 "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
57 "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
59 "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"
63 def find_event(m, params)
64 num = params[:num] || @bot.config['lastfm.default_events']
65 num = num.to_i.clip(1, @bot.config['lastfm.max_events'])
67 location = artist = nil
68 location = params[:location].to_s if params[:location]
69 artist = params[:who].to_s if params[:who]
71 spec = location ? "in #{location}" : "by #{artist}"
72 query = location ? "?findloc=#{CGI.escape(location)}" : "?s=#{CGI.escape(artist)}&findloc="
75 page = @bot.httputil.get LASTFM + "/events/" + query
78 disp_events = Array.new
80 pre_events = page.scan(LastFmEvent::REGEXP)
81 # debug pre_events.inspect
83 # We may not find any even because the page gives a list
84 # of locations instead. In this case, retry with the first of
86 if page.match(/<a href="(\/events\/\?l=[^"]+)">/)
87 debug "Rechecking with #{$1}"
88 page = @bot.httputil.get(LASTFM+$1)
89 pre_events = page.scan(LastFmEvent::REGEXP) if page
92 m.reply "No events found #{spec}, sorry"
96 pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
97 date = Time.utc(year.to_i, month.to_i, day.to_i)
98 url = LASTFM + url_who
99 if who.match(/<strong>(.*?)<\/strong>(.+)?/)
100 artist = Bold + $1.ircify_html + Bold
101 artist << ", " << $2.ircify_html if $2
103 debug "who: #{who.inspect}"
104 artist = who.ircify_html
106 if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
107 loc = Bold + $1.ircify_html + Bold
108 loc << ", " << $2.ircify_html if $2
111 loc = where.ircify_html
113 attendance = how_many.ircify_html
114 events << LastFmEvent.new(url, date, artist, loc, attendance)
116 # debug events.inspect
118 events[0...num].each { |event|
119 disp_events << event.to_s
121 m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
123 m.reply "No events found #{spec}"
126 rescue Exception => e
127 m.reply "I had problems looking for events #{spec}"
129 debug e.backtrace.join("\n")
130 debug page[0...10*1024] if page
136 def find_artist(m, params)
137 artist = params[:who].to_s
140 esc = URI.escape(CGI.escape(artist))
141 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
143 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
145 title = $2.ircify_html
147 raise "No URL/Title found for #{artist}"
150 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
151 if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
152 wiki = $1.ircify_html
155 m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
157 m.reply "no data found on #{artist}"
160 rescue Exception => e
161 m.reply "I had problems looking for #{artist}"
163 debug e.backtrace.join("\n")
164 debug page[0...10*1024] if page
169 def find_track(m, params)
170 m.reply "not implemented yet, sorry"
173 def find_album(m, params)
174 m.reply "not implemented yet, sorry"
177 def lastfm(m, params)
178 action = params[:action].intern
179 action = :neighbours if action == :neighbors
183 data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
184 m.reply "#{action} for #{user}:"
185 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
187 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
193 plugin = LastFmPlugin.new
194 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }
195 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }
196 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }
197 plugin.map 'lastfm artist *who', :action => :find_artist
198 plugin.map 'lastfm group *who', :action => :find_artist
199 plugin.map 'lastfm track *dunno', :action => :find_track
200 plugin.map 'lastfm song *dunno', :action => :find_track
201 plugin.map 'lastfm album *dunno', :action => :find_album
202 plugin.map 'lastfm :action *user'