lastfm plugin: number of events to be displayed can now be configured
[rbot] / data / rbot / plugins / lastfm.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: lastfm plugin for rbot
5 #
6 # Author:: Jeremy Voorhis
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 #
9 # Copyright:: (C) 2005 Jeremy Voorhis
10 # Copyright:: (C) 2007 Giuseppe Bilotta
11 #
12 # License:: GPL v2
13
14 require 'open-uri'
15
16 class ::LastFmEvent
17   attr_accessor :url, :date, :artist, :location, :attendance
18   def initialize(url, date, artist, location, attendance)
19     @url = url
20     @date = date
21     @artist = artist
22     @location = location
23     @attendance = attendance
24   end
25
26   def compact_display
27     if @attendance.empty?
28       return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @url]
29     else
30       return "%s %s @ %s (%s) %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @attendance, @url]
31     end
32   end
33   alias :to_s :compact_display
34
35 end
36
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.")
44
45   LASTFM = "http://www.last.fm"
46
47   def help(plugin, topic="")
48     case topic.intern
49     when :event, :events
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']}"
51     when :artist, :group
52       "lastfm artist <name> => show information on artist/group <name> from last.fm"
53     when :song, :track
54       "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
55     when :album
56       "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
57     else
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"
59     end
60   end
61
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'])
65
66     location = artist = nil
67     location = params[:location].to_s if params[:location]
68     artist = params[:who].to_s if params[:who]
69     page = nil
70     spec = location ? "in #{location}" : "by #{artist}"
71     begin
72       if location
73         esc = URI.escape(location)
74         page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
75       else
76         esc = URI.escape(artist)
77         page = @bot.httputil.get "#{LASTFM}/events?s=#{esc}&findloc="
78       end
79
80       if page
81         events = Array.new
82         disp_events = Array.new
83
84         # matches are:
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
88         if pre_events.empty?
89           m.reply "No events found #{spec}, sorry"
90         end
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
97           else
98             debug "who: #{who.inspect}"
99             artist = who.ircify_html
100           end
101           if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
102             loc = Bold + $1.ircify_html + Bold
103             loc << ", " << $2.ircify_html if $2
104           else
105             debug where.inspect
106             loc = where.ircify_html
107           end
108           attendance = how_many.ircify_html
109           events << LastFmEvent.new(url, date, artist, loc, attendance)
110         }
111         # debug events.inspect
112
113         events[0...num].each { |event|
114           disp_events << event.to_s
115         }
116         m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
117       else
118         m.reply "No events found #{spec}"
119         return
120       end
121     rescue Exception => e
122       m.reply "I had problems looking for events #{spec}"
123       error e.inspect
124       debug e.backtrace.join("\n")
125       debug page[0...10*1024] if page
126       return
127     end
128   end
129
130   def find_artist(m, params)
131     artist = params[:who].to_s
132     page = nil
133     begin
134       esc = URI.escape(artist)
135       page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
136       if page
137         if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
138           url = LASTFM + $1
139           title = $2.ircify_html
140         else
141           raise "No URL/Title found for #{artist}"
142         end
143
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
147         end
148
149         m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
150       else
151         m.reply "no data found on #{artist}"
152         return
153       end
154     rescue Exception => e
155       m.reply "I had problems looking for #{artist}"
156       error e.inspect
157       debug e.backtrace.join("\n")
158       debug page[0...10*1024] if page
159       return
160     end
161   end
162
163   def find_track(m, params)
164     m.reply "not implemented yet, sorry"
165   end
166
167   def find_album(m, params)
168     m.reply "not implemented yet, sorry"
169   end
170
171   def lastfm(m, params)
172     action = params[:action].intern
173     action = :neighbours if action == :neighbors
174     user = params[:user]
175     begin
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(", ")
179     rescue
180       m.reply "could not find #{action} for #{user} (is #{user} a user?)"
181     end
182   end
183 end
184
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'