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 Config.register Config::IntegerValue.new('lastfm.max_events',
40 :default => 25, :validate => Proc.new{|v| v > 1},
41 :desc => "Maximum number of events to display.")
42 Config.register Config::IntegerValue.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="
74 page = @bot.httputil.get LASTFM + "/events/" + query
77 disp_events = Array.new
79 pre_events = page.scan(LastFmEvent::REGEXP)
80 # debug pre_events.inspect
82 # We may not find any even because the page gives a list
83 # of locations instead. In this case, retry with the first of
85 if page.match(/<a href="(\/events\/\?l=[^"]+)">/)
86 debug "Rechecking with #{$1}"
87 page = @bot.httputil.get(LASTFM+$1)
88 pre_events = page.scan(LastFmEvent::REGEXP) if page
91 m.reply "No events found #{spec}, sorry"
95 pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
96 date = Time.utc(year.to_i, month.to_i, day.to_i)
97 url = LASTFM + url_who
98 if who.match(/<strong>(.*?)<\/strong>(.+)?/)
99 artist = Bold + $1.ircify_html + Bold
100 artist << ", " << $2.ircify_html if $2
102 debug "who: #{who.inspect}"
103 artist = who.ircify_html
105 if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
106 loc = Bold + $1.ircify_html + Bold
107 loc << ", " << $2.ircify_html if $2
110 loc = where.ircify_html
112 attendance = how_many.ircify_html
113 events << LastFmEvent.new(url, date, artist, loc, attendance)
115 # debug events.inspect
117 events[0...num].each { |event|
118 disp_events << event.to_s
120 m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
122 m.reply "No events found #{spec}"
125 rescue Exception => e
126 m.reply "I had problems looking for events #{spec}"
128 debug e.backtrace.join("\n")
129 debug page[0...10*1024] if page
134 def find_artist(m, params)
135 artist = params[:who].to_s
138 esc = URI.escape(CGI.escape(artist))
139 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
141 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
143 title = $2.ircify_html
145 raise "No URL/Title found for #{artist}"
148 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
149 if page.match(/<div (?:class|id)="wikiAbstract">(.*?)<\/div>/m)
150 wiki = $1.ircify_html
153 m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
155 m.reply "no data found on #{artist}"
158 rescue Exception => e
159 m.reply "I had problems looking for #{artist}"
161 debug e.backtrace.join("\n")
162 debug page[0...10*1024] if page
167 def find_track(m, params)
168 m.reply "not implemented yet, sorry"
171 def find_album(m, params)
172 m.reply "not implemented yet, sorry"
175 def lastfm(m, params)
176 action = params[:action].intern
177 action = :neighbours if action == :neighbors
180 data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
181 m.reply "#{action} for #{user}:"
182 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
184 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
189 plugin = LastFmPlugin.new
190 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
191 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
192 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
193 plugin.map 'lastfm artist *who', :action => :find_artist, :thread => true
194 plugin.map 'lastfm group *who', :action => :find_artist, :thread => true
195 plugin.map 'lastfm track *dunno', :action => :find_track
196 plugin.map 'lastfm song *dunno', :action => :find_track
197 plugin.map 'lastfm album *dunno', :action => :find_album
198 plugin.map 'lastfm :action *user', :thread => true