lastfm plugin: refactoring
[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 end
26
27 class LastFmPlugin < Plugin
28
29   LASTFM = "http://www.last.fm"
30
31   def help(plugin, topic="")
32     case topic.intern
33     when :event, :events
34       "lastfm events in <location> => show information on events in or near <location> from last.fm"
35     when :artist, :group
36       "lastfm artist <name> => show information on artist/group <name> from last.fm"
37     when :song, :track
38       "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
39     when :album
40       "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
41     else
42       "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"
43     end
44   end
45
46   def find_event(m, params)
47     location = params[:location].to_s
48     page = nil
49     begin
50       esc = URI.escape(location)
51       page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
52       if page
53         events = Array.new
54         disp_events = Array.new
55
56         # matches are:
57         # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
58         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)
59         # debug pre_events.inspect
60         if pre_events.empty?
61           m.reply "No events found in #{location}, sorry"
62         end
63         pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
64           date = Time.utc(year.to_i, month.to_i, day.to_i)
65           url = LASTFM + url_who
66           if who.match(/<strong>(.*?)<\/strong>(.+)?/)
67             artist = Bold + $1.ircify_html + Bold
68             artist << ", " << $2.ircify_html if $2
69           else
70             debug "who: #{who.inspect}"
71             artist = who.ircify_html
72           end
73           if where.match(/<strong>(.*?)<\/strong>(.+)?/)
74             loc = Bold + $1.ircify_html + Bold
75             loc << ", " << $2.ircify_html if $2
76           else
77             debug where.inspect
78             loc = where.ircify_html
79           end
80           attendance = how_many.ircify_html
81           events << LastFmEvent.new(url, date, artist, loc, attendance)
82         }
83         # debug events.inspect
84
85         events[0..2].each { |event|
86           disp_events << "%s %s @ %s (%s) %s" % [event.date.strftime("%a %b, %d %Y"), event.artist, event.location, event.attendance, event.url]
87         }
88         m.reply disp_events.join(' | ')
89       else
90         m.reply "No events found in #{location}"
91         return
92       end
93     rescue Exception => e
94       m.reply "I had problems looking for events in #{location}"
95       error e.inspect
96       debug e.backtrace.join("\n")
97       debug page[0...10*1024] if page
98       return
99     end
100   end
101
102   def find_artist(m, params)
103     artist = params[:who].to_s
104     page = nil
105     begin
106       esc = URI.escape(artist)
107       page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
108       if page
109         if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
110           url = LASTFM + $1
111           title = $2.ircify_html
112         else
113           raise "No URL/Title found for #{artist}"
114         end
115
116         wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
117         if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
118           wiki = $1.ircify_html
119         end
120
121         m.reply "%s : %s\n%s" % [title, url, wiki]
122       else
123         m.reply "no data found on #{artist}"
124         return
125       end
126     rescue Exception => e
127       m.reply "I had problems looking for #{artist}"
128       error e.inspect
129       debug e.backtrace.join("\n")
130       debug page[0...10*1024] if page
131       return
132     end
133   end
134
135   def find_track(m, params)
136     m.reply "not implemented yet, sorry"
137   end
138
139   def find_album(m, params)
140     m.reply "not implemented yet, sorry"
141   end
142
143   def lastfm(m, params)
144     action = params[:action].intern
145     action = :neighbours if action == :neighbors
146     user = params[:user]
147     begin
148       data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
149       m.reply "#{action} for #{user}:"
150       m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
151     rescue
152       m.reply "could not find #{action} for #{user} (is #{user} a user?)"
153     end
154   end
155 end
156
157 plugin = LastFmPlugin.new
158 plugin.map 'lastfm event[s] in :location', :action => :find_event
159 plugin.map 'lastfm artist *who', :action => :find_artist
160 plugin.map 'lastfm group *who', :action => :find_artist
161 plugin.map 'lastfm track *dunno', :action => :find_track
162 plugin.map 'lastfm song *dunno', :action => :find_track
163 plugin.map 'lastfm album *dunno', :action => :find_album
164 plugin.map 'lastfm :action *user'