lastfm plugin: don't be silent when no events are found
[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 lastfm(m, params)
47     action = params[:action].intern
48     action = :neighbours if action == :neighbors
49     what = params[:what]
50     case action
51     when :events, :event
52       page = nil
53       begin
54         location = what.to_s.sub(/^in\s+/,'')
55         raise "wrong location #{location}" if location.empty?
56         esc = URI.escape(location)
57         page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
58         if page
59           events = Array.new
60           disp_events = Array.new
61
62           # matches are:
63           # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
64           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)
65           # debug pre_events.inspect
66           if pre_events.empty?
67             m.reply "No events found in #{location}, sorry"
68           end
69           pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
70             date = Time.utc(year.to_i, month.to_i, day.to_i)
71             url = LASTFM + url_who
72             artist = who.ircify_html
73             loc = where.ircify_html
74             attendance = how_many.ircify_html
75             events << LastFmEvent.new(url, date, artist, loc, attendance)
76           }
77           # debug events.inspect
78
79           events[0..2].each { |event|
80             disp_events << "%s %s @ %s (%s) %s" % [event.date.strftime("%a %b, %d %Y"), event.artist, event.location, event.attendance, event.url]
81           }
82           m.reply disp_events.join(' | ')
83         else
84           m.reply "No events found in #{location}"
85           return
86         end
87       rescue Exception => e
88         m.reply "I had problems looking for events #{what.to_s}"
89         error e.inspect
90         debug e.backtrace.join("\n")
91         debug page[0...10*1024] if page
92         return
93       end
94     when :artist, :group
95       artist = what.to_s
96       page = nil
97       begin
98         esc = URI.escape(artist)
99         page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
100         if page
101           if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
102             url = LASTFM + $1
103             title = $2.ircify_html
104           else
105             raise "No URL/Title found for #{artist}"
106           end
107
108           wiki = "This #{action} doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
109           if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
110             wiki = $1.ircify_html
111           end
112
113           m.reply "%s : %s\n%s" % [title, url, wiki]
114         else
115           m.reply "no data found on #{artist}"
116           return
117         end
118       rescue Exception => e
119         m.reply "I had problems looking for #{artist}"
120         error e.inspect
121         debug e.backtrace.join("\n")
122         debug page[0...10*1024] if page
123         return
124       end
125     when :song, :track
126       m.reply "not implemented yet, sorry"
127     when :album
128       m.reply "not implemented yet, sorry"
129     else
130       return usage(m) unless what.length == 1
131       user = what.first
132       begin
133         data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
134         m.reply "#{action} for #{user}:"
135         m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
136       rescue
137         m.reply "could not find #{action} for #{user} (is #{user} a user?)"
138       end
139     end
140   end
141 end
142
143 plugin = LastFmPlugin.new
144 plugin.map 'lastfm :action *what'