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