lastfm plugin: overlong artisti descriptions are truncated; artist events can be...
[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
39   LASTFM = "http://www.last.fm"
40
41   def help(plugin, topic="")
42     case topic.intern
43     when :event, :events
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>"
45     when :artist, :group
46       "lastfm artist <name> => show information on artist/group <name> from last.fm"
47     when :song, :track
48       "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
49     when :album
50       "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
51     else
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"
53     end
54   end
55
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]
60     page = nil
61     spec = location ? "in #{location}" : "by #{artist}"
62     begin
63       if location
64         esc = URI.escape(location)
65         page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
66       else
67         esc = URI.escape(artist)
68         page = @bot.httputil.get "#{LASTFM}/events?s=#{esc}&findloc="
69       end
70
71       if page
72         events = Array.new
73         disp_events = Array.new
74
75         # matches are:
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
79         if pre_events.empty?
80           m.reply "No events found #{spec}, sorry"
81         end
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
88           else
89             debug "who: #{who.inspect}"
90             artist = who.ircify_html
91           end
92           if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
93             loc = Bold + $1.ircify_html + Bold
94             loc << ", " << $2.ircify_html if $2
95           else
96             debug where.inspect
97             loc = where.ircify_html
98           end
99           attendance = how_many.ircify_html
100           events << LastFmEvent.new(url, date, artist, loc, attendance)
101         }
102         # debug events.inspect
103
104         events[0..2].each { |event|
105           disp_events << event.to_s
106         }
107         m.reply disp_events.join(' | ')
108       else
109         m.reply "No events found #{spec}"
110         return
111       end
112     rescue Exception => e
113       m.reply "I had problems looking for events #{spec}"
114       error e.inspect
115       debug e.backtrace.join("\n")
116       debug page[0...10*1024] if page
117       return
118     end
119   end
120
121   def find_artist(m, params)
122     artist = params[:who].to_s
123     page = nil
124     begin
125       esc = URI.escape(artist)
126       page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
127       if page
128         if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
129           url = LASTFM + $1
130           title = $2.ircify_html
131         else
132           raise "No URL/Title found for #{artist}"
133         end
134
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
138         end
139
140         m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
141       else
142         m.reply "no data found on #{artist}"
143         return
144       end
145     rescue Exception => e
146       m.reply "I had problems looking for #{artist}"
147       error e.inspect
148       debug e.backtrace.join("\n")
149       debug page[0...10*1024] if page
150       return
151     end
152   end
153
154   def find_track(m, params)
155     m.reply "not implemented yet, sorry"
156   end
157
158   def find_album(m, params)
159     m.reply "not implemented yet, sorry"
160   end
161
162   def lastfm(m, params)
163     action = params[:action].intern
164     action = :neighbours if action == :neighbors
165     user = params[:user]
166     begin
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(", ")
170     rescue
171       m.reply "could not find #{action} for #{user} (is #{user} a user?)"
172     end
173   end
174 end
175
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'