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
39 LASTFM = "http://www.last.fm"
41 def help(plugin, topic="")
44 "lastfm events in <location> => show information on events in or near <location>. lastfm events by <artist/group> => show information on events by <artist/group>"
46 "lastfm artist <name> => show information on artist/group <name> from last.fm"
48 "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
50 "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
52 "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"
56 def find_event(m, params)
57 location = artist = nil
58 location = params[:location].to_s if params[:location]
59 artist = params[:who].to_s if params[:who]
61 spec = location ? "in #{location}" : "by #{artist}"
64 esc = URI.escape(location)
65 page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
67 esc = URI.escape(artist)
68 page = @bot.httputil.get "#{LASTFM}/events?s=#{esc}&findloc="
73 disp_events = Array.new
76 # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
77 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)
78 # debug pre_events.inspect
80 m.reply "No events found #{spec}, sorry"
82 pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
83 date = Time.utc(year.to_i, month.to_i, day.to_i)
84 url = LASTFM + url_who
85 if who.match(/<strong>(.*?)<\/strong>(.+)?/)
86 artist = Bold + $1.ircify_html + Bold
87 artist << ", " << $2.ircify_html if $2
89 debug "who: #{who.inspect}"
90 artist = who.ircify_html
92 if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
93 loc = Bold + $1.ircify_html + Bold
94 loc << ", " << $2.ircify_html if $2
97 loc = where.ircify_html
99 attendance = how_many.ircify_html
100 events << LastFmEvent.new(url, date, artist, loc, attendance)
102 # debug events.inspect
104 events[0..2].each { |event|
105 disp_events << event.to_s
107 m.reply disp_events.join(' | ')
109 m.reply "No events found #{spec}"
112 rescue Exception => e
113 m.reply "I had problems looking for events #{spec}"
115 debug e.backtrace.join("\n")
116 debug page[0...10*1024] if page
121 def find_artist(m, params)
122 artist = params[:who].to_s
125 esc = URI.escape(artist)
126 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
128 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
130 title = $2.ircify_html
132 raise "No URL/Title found for #{artist}"
135 wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
136 if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
137 wiki = $1.ircify_html
140 m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
142 m.reply "no data found on #{artist}"
145 rescue Exception => e
146 m.reply "I had problems looking for #{artist}"
148 debug e.backtrace.join("\n")
149 debug page[0...10*1024] if page
154 def find_track(m, params)
155 m.reply "not implemented yet, sorry"
158 def find_album(m, params)
159 m.reply "not implemented yet, sorry"
162 def lastfm(m, params)
163 action = params[:action].intern
164 action = :neighbours if action == :neighbors
167 data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
168 m.reply "#{action} for #{user}:"
169 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
171 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
176 plugin = LastFmPlugin.new
177 plugin.map 'lastfm event[s] in *location', :action => :find_event
178 plugin.map 'lastfm event[s] by *who', :action => :find_event
179 plugin.map 'lastfm event[s] [for] *who', :action => :find_event
180 plugin.map 'lastfm artist *who', :action => :find_artist
181 plugin.map 'lastfm group *who', :action => :find_artist
182 plugin.map 'lastfm track *dunno', :action => :find_track
183 plugin.map 'lastfm song *dunno', :action => :find_track
184 plugin.map 'lastfm album *dunno', :action => :find_album
185 plugin.map 'lastfm :action *user'