]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lastfm.rb
9c6e97b9ac246ac36cd9e815cb0e1862142de8f2
[user/henk/code/ruby/rbot.git] / 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   # matches are:
18   # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
19   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
20   attr_accessor :url, :date, :artist, :location, :attendance
21   def initialize(url, date, artist, location, attendance)
22     @url = url
23     @date = date
24     @artist = artist
25     @location = location
26     @attendance = attendance
27   end
28
29   def compact_display
30     if @attendance.empty?
31       return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @url]
32     else
33       return "%s %s @ %s (%s) %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @attendance, @url]
34     end
35   end
36   alias :to_s :compact_display
37
38 end
39
40 class LastFmPlugin < Plugin
41   BotConfig.register BotConfigIntegerValue.new('lastfm.max_events',
42     :default => 25, :validate => Proc.new{|v| v > 1},
43     :desc => "Maximum number of events to display.")
44   BotConfig.register BotConfigIntegerValue.new('lastfm.default_events',
45     :default => 3, :validate => Proc.new{|v| v > 1},
46     :desc => "Default number of events to display.")
47
48   LASTFM = "http://www.last.fm"
49
50   def help(plugin, topic="")
51     case topic.intern
52     when :event, :events
53       "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']}"
54     when :artist, :group
55       "lastfm artist <name> => show information on artist/group <name> from last.fm"
56     when :song, :track
57       "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
58     when :album
59       "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
60     else
61       "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     end
63   end
64
65   def find_event(m, params)
66     num = params[:num] || @bot.config['lastfm.default_events']
67     num = num.to_i.clip(1, @bot.config['lastfm.max_events'])
68
69     location = artist = nil
70     location = params[:location].to_s if params[:location]
71     artist = params[:who].to_s if params[:who]
72     page = nil
73     spec = location ? "in #{location}" : "by #{artist}"
74     begin
75       if location
76         esc = CGI.escape(location)
77         page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
78       else
79         esc = CGI.escape(artist)
80         page = @bot.httputil.get "#{LASTFM}/events?s=#{esc}&findloc="
81       end
82
83       if page
84         events = Array.new
85         disp_events = Array.new
86
87         pre_events = page.scan(LastFmEvent::REGEXP)
88         # debug pre_events.inspect
89         if pre_events.empty?
90           # We may not find any even because the page gives a list
91           # of locations instead. In this case, retry with the first of
92           # these location
93           if page.match(/<a href="(\/events\/\?l=[^"]+)">/)
94             debug "Rechecking with #{$1}"
95             page = @bot.httputil.get(LASTFM+$1)
96             pre_events = page.scan(LastFmEvent::REGEXP) if page
97           end
98           if pre_events.empty?
99             m.reply "No events found #{spec}, sorry"
100             return
101           end
102         end
103         pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
104           date = Time.utc(year.to_i, month.to_i, day.to_i)
105           url = LASTFM + url_who
106           if who.match(/<strong>(.*?)<\/strong>(.+)?/)
107             artist = Bold + $1.ircify_html + Bold
108             artist << ", " << $2.ircify_html if $2
109           else
110             debug "who: #{who.inspect}"
111             artist = who.ircify_html
112           end
113           if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
114             loc = Bold + $1.ircify_html + Bold
115             loc << ", " << $2.ircify_html if $2
116           else
117             debug where.inspect
118             loc = where.ircify_html
119           end
120           attendance = how_many.ircify_html
121           events << LastFmEvent.new(url, date, artist, loc, attendance)
122         }
123         # debug events.inspect
124
125         events[0...num].each { |event|
126           disp_events << event.to_s
127         }
128         m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
129       else
130         m.reply "No events found #{spec}"
131         return
132       end
133     rescue Exception => e
134       m.reply "I had problems looking for events #{spec}"
135       error e.inspect
136       debug e.backtrace.join("\n")
137       debug page[0...10*1024] if page
138       return
139     end
140   end
141
142   def find_artist(m, params)
143     artist = params[:who].to_s
144     page = nil
145     begin
146       esc = URI.escape(CGI.escape(artist))
147       page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
148       if page
149         if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
150           url = LASTFM + $1
151           title = $2.ircify_html
152         else
153           raise "No URL/Title found for #{artist}"
154         end
155
156         wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
157         if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
158           wiki = $1.ircify_html
159         end
160
161         m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
162       else
163         m.reply "no data found on #{artist}"
164         return
165       end
166     rescue Exception => e
167       m.reply "I had problems looking for #{artist}"
168       error e.inspect
169       debug e.backtrace.join("\n")
170       debug page[0...10*1024] if page
171       return
172     end
173   end
174
175   def find_track(m, params)
176     m.reply "not implemented yet, sorry"
177   end
178
179   def find_album(m, params)
180     m.reply "not implemented yet, sorry"
181   end
182
183   def lastfm(m, params)
184     action = params[:action].intern
185     action = :neighbours if action == :neighbors
186     user = params[:user]
187     begin
188       data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
189       m.reply "#{action} for #{user}:"
190       m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
191     rescue
192       m.reply "could not find #{action} for #{user} (is #{user} a user?)"
193     end
194   end
195 end
196
197 plugin = LastFmPlugin.new
198 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }
199 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }
200 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }
201 plugin.map 'lastfm artist *who', :action => :find_artist
202 plugin.map 'lastfm group *who', :action => :find_artist
203 plugin.map 'lastfm track *dunno', :action => :find_track
204 plugin.map 'lastfm song *dunno', :action => :find_track
205 plugin.map 'lastfm album *dunno', :action => :find_album
206 plugin.map 'lastfm :action *user'