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
17 attr_accessor :url, :date, :artist, :location, :attendance
18 def initialize(url, date, artist, location, attendance)
23 @attendance = attendance
28 return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @url]
30 return "%s %s @ %s (%s) %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @attendance, @url]
33 alias :to_s :compact_display
37 class LastFmPlugin < Plugin
38 BotConfig.register BotConfigIntegerValue.new('lastfm.max_events',
39 :default => 25, :validate => Proc.new{|v| v > 1},
40 :desc => "Maximum number of events to display.")
41 BotConfig.register BotConfigIntegerValue.new('lastfm.default_events',
42 :default => 3, :validate => Proc.new{|v| v > 1},
43 :desc => "Default number of events to display.")
45 LASTFM = "http://www.last.fm"
47 def help(plugin, topic="")
50 "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']}"
52 "lastfm artist <name> => show information on artist/group <name> from last.fm"
54 "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
56 "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
58 "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"
62 def find_event(m, params)
63 num = params[:num] || @bot.config['lastfm.default_events']
64 num = num.to_i.clip(1, @bot.config['lastfm.max_events'])
66 location = artist = nil
67 location = params[:location].to_s if params[:location]
68 artist = params[:who].to_s if params[:who]
70 spec = location ? "in #{location}" : "by #{artist}"
73 esc = CGI.escape(location)
74 page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
76 esc = CGI.escape(artist)
77 page = @bot.httputil.get "#{LASTFM}/events?s=#{esc}&findloc="
82 disp_events = Array.new
85 # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
86 pre_events = page.scan(/<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)
87 # debug pre_events.inspect
89 m.reply "No events found #{spec}, sorry"
91 pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
92 date = Time.utc(year.to_i, month.to_i, day.to_i)
93 url = LASTFM + url_who
94 if who.match(/<strong>(.*?)<\/strong>(.+)?/)
95 artist = Bold + $1.ircify_html + Bold
96 artist << ", " << $2.ircify_html if $2
98 debug "who: #{who.inspect}"
99 artist = who.ircify_html
101 if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
102 loc = Bold + $1.ircify_html + Bold
103 loc << ", " << $2.ircify_html if $2
106 loc = where.ircify_html
108 attendance = how_many.ircify_html
109 events << LastFmEvent.new(url, date, artist, loc, attendance)
111 # debug events.inspect
113 events[0...num].each { |event|
114 disp_events << event.to_s
116 m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
118 m.reply "No events found #{spec}"
121 rescue Exception => e
122 m.reply "I had problems looking for events #{spec}"
124 debug e.backtrace.join("\n")
125 debug page[0...10*1024] if page
130 def find_artist(m, params)
131 artist = params[:who].to_s
134 esc = URI.escape(CGI.escape(artist))
135 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
137 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
139 title = $2.ircify_html
141 raise "No URL/Title found for #{artist}"
144 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
145 if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
146 wiki = $1.ircify_html
149 m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
151 m.reply "no data found on #{artist}"
154 rescue Exception => e
155 m.reply "I had problems looking for #{artist}"
157 debug e.backtrace.join("\n")
158 debug page[0...10*1024] if page
163 def find_track(m, params)
164 m.reply "not implemented yet, sorry"
167 def find_album(m, params)
168 m.reply "not implemented yet, sorry"
171 def lastfm(m, params)
172 action = params[:action].intern
173 action = :neighbours if action == :neighbors
176 data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
177 m.reply "#{action} for #{user}:"
178 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
180 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
185 plugin = LastFmPlugin.new
186 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }
187 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }
188 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }
189 plugin.map 'lastfm artist *who', :action => :find_artist
190 plugin.map 'lastfm group *who', :action => :find_artist
191 plugin.map 'lastfm track *dunno', :action => :find_track
192 plugin.map 'lastfm song *dunno', :action => :find_track
193 plugin.map 'lastfm album *dunno', :action => :find_album
194 plugin.map 'lastfm :action *user'