]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lastfm.rb
f87e524591235b85f08a465e94f6ad6bdde9e418
[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   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             if who.match(/<strong>(.*?)<\/strong>(.*)?/)
73               artist = Bold + $1.ircify_html + Bold
74               artist << ": " << $2.ircify_html if $2
75             else
76               debug "who: #{who.inspect}"
77               artist = who.ircify_html
78             end
79             if where.match(/<strong>(.*?)<\/strong>(.*)?/)
80               loc = Bold + $1.ircify_html + Bold
81               loc << ", " << $2.ircify_html if $2
82             else
83               debug where.inspect
84               loc = where.ircify_html
85             end
86             attendance = how_many.ircify_html
87             events << LastFmEvent.new(url, date, artist, loc, attendance)
88           }
89           # debug events.inspect
90
91           events[0..2].each { |event|
92             disp_events << "%s %s @ %s (%s) %s" % [event.date.strftime("%a %b, %d %Y"), event.artist, event.location, event.attendance, event.url]
93           }
94           m.reply disp_events.join(' | ')
95         else
96           m.reply "No events found in #{location}"
97           return
98         end
99       rescue Exception => e
100         m.reply "I had problems looking for events #{what.to_s}"
101         error e.inspect
102         debug e.backtrace.join("\n")
103         debug page[0...10*1024] if page
104         return
105       end
106     when :artist, :group
107       artist = what.to_s
108       page = nil
109       begin
110         esc = URI.escape(artist)
111         page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
112         if page
113           if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
114             url = LASTFM + $1
115             title = $2.ircify_html
116           else
117             raise "No URL/Title found for #{artist}"
118           end
119
120           wiki = "This #{action} doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
121           if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
122             wiki = $1.ircify_html
123           end
124
125           m.reply "%s : %s\n%s" % [title, url, wiki]
126         else
127           m.reply "no data found on #{artist}"
128           return
129         end
130       rescue Exception => e
131         m.reply "I had problems looking for #{artist}"
132         error e.inspect
133         debug e.backtrace.join("\n")
134         debug page[0...10*1024] if page
135         return
136       end
137     when :song, :track
138       m.reply "not implemented yet, sorry"
139     when :album
140       m.reply "not implemented yet, sorry"
141     else
142       return usage(m) unless what.length == 1
143       user = what.first
144       begin
145         data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
146         m.reply "#{action} for #{user}:"
147         m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
148       rescue
149         m.reply "could not find #{action} for #{user} (is #{user} a user?)"
150       end
151     end
152   end
153 end
154
155 plugin = LastFmPlugin.new
156 plugin.map 'lastfm :action *what'