]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/lastfm.rb
3801126e5a8b037d765cacf8c30b709e53f09093
[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 # Author:: Casey Link <unnamedrambler@gmail.com>
9 #
10 # Copyright:: (C) 2005 Jeremy Voorhis
11 # Copyright:: (C) 2007 Giuseppe Bilotta
12 # Copyright:: (C) 2008 Casey Link
13 #
14 # License:: GPL v2
15
16 require 'rexml/document'
17
18 class ::LastFmEvent
19   def initialize(url, date, artist, location, description)
20     @url = url
21     @date = date
22     @artist = artist
23     @location = location
24     @description = description
25   end
26
27   def compact_display
28     return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @url]
29   end
30   alias :to_s :compact_display
31
32 end
33
34 class LastFmPlugin < Plugin
35   include REXML
36   Config.register Config::IntegerValue.new('lastfm.max_events',
37     :default => 25, :validate => Proc.new{|v| v > 1},
38     :desc => "Maximum number of events to display.")
39   Config.register Config::IntegerValue.new('lastfm.default_events',
40     :default => 3, :validate => Proc.new{|v| v > 1},
41     :desc => "Default number of events to display.")
42
43   APIKEY = "b25b959554ed76058ac220b7b2e0a026"
44   APIURL = "http://ws.audioscrobbler.com/2.0/?api_key=#{APIKEY}&"
45
46   def initialize
47     super
48     class << @registry
49       def store(val)
50         val
51       end
52       def restore(val)
53         val
54       end
55     end
56   end
57
58   def help(plugin, topic="")
59     case (topic.intern rescue nil)
60     when :event, :events
61       _("lastfm [<num>] events in <location> => show information on events in or near <location>. lastfm [<num>] events by <artist/group> => show information on events by <artist/group>. The number of events <num> that can be displayed is optional, defaults to %{d} and cannot be higher than %{m}") % {:d => @bot.config['lastfm.default_events'], :m => @bot.config['lastfm.max_events']}
62     when :artist
63       _("lastfm artist <name> => show information on artist <name> from last.fm")
64     when :album
65       _("lastfm album <name> => show information on album <name> from last.fm [not implemented yet]")
66     when :now, :np
67       _("lastfm now [<user>] => show the now playing track from last.fm.  np [<user>] does the same.")
68     when :set
69       _("lastfm set nick <user> => associate your current irc nick with a last.fm user. lastfm set verb <present> <past> => set your preferred now playing verb. default \"listening\" and \"listened\".")
70     when :who
71       _("lastfm who [<nick>] => show who <nick> is at last.fm. if <nick> is empty, show who you are at lastfm.")
72     else
73       _("lastfm [<user>] => show your or <user>'s now playing track at lastfm. np [<user>] => same as 'lastfm'. other topics: events, artist, album, now, set, who")
74     end
75   end
76
77   def find_events(m, params)
78     num = params[:num] || @bot.config['lastfm.default_events']
79     num = num.to_i.clip(1, @bot.config['lastfm.max_events'])
80
81     location = artist = nil
82     location = params[:location].to_s if params[:location]
83     artist = params[:who].to_s if params[:who]
84
85     uri = nil
86     if artist == nil
87       uri = URI.escape("#{APIURL}method=geo.getevents&location=#{location}")
88     else
89       uri = URI.escape("#{APIURL}method=artist.getevents&artist=#{artist}")
90     end
91     xml = @bot.httputil.get_response(uri)
92
93     doc = Document.new xml.body
94     if xml.class == Net::HTTPInternalServerError
95       if doc.root.attributes["status"] == "failed"
96         m.reply doc.root.elements["error"].text
97       else
98         m.reply _("Could not retrieve events")
99       end
100     end
101     disp_events = Array.new
102     events = Array.new
103     doc.root.elements.each("events/event"){ |e|
104       title = e.elements["title"].text
105       venue = e.elements["venue"].elements["name"].text
106       city = e.elements["venue"].elements["location"].elements["city"].text
107       country =  e.elements["venue"].elements["location"].elements["country"].text
108       loc = Bold + venue + Bold + " #{city}, #{country}"
109       date = e.elements["startDate"].text.split
110       date = Time.utc(date[3].to_i, date[2], date[1].to_i)
111       desc = e.elements["description"].text
112       url = e.elements["url"].text
113       artists = Array.new
114       e.elements.each("artists/artist"){ |a|
115         artists << a.text
116       }
117       if artists.length > 10 #more than 10 artists and it floods
118        diff = artists.length - 10
119        artists = artists[0..10]
120        artists << _(" and %{n} more...") % {:n => diff}
121       end
122       artists = artists.join(", ")
123       events << LastFmEvent.new(url, date, artists, loc, desc)
124     }
125     events[0...num].each { |event|
126       disp_events << event.to_s
127     }
128     m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
129
130   end  
131
132   def tasteometer(m, params)
133     opts = { :cache => false }
134     user1 = params[:user1].to_s
135     user2 = params[:user2].to_s
136     xml = @bot.httputil.get_response("#{APIURL}method=tasteometer.compare&type1=user&type2=user&value1=#{user1}&value2=#{user2}", opts)
137     doc = Document.new xml.body
138     unless doc
139       m.reply _("last.fm parsing failed")
140       return
141     end
142     if xml.class == Net::HTTPInternalServerError
143       if doc.root.elements["error"].attributes["code"] == "7" then 
144         error = doc.root.elements["error"].text
145         error.match(/Invalid username: \[(.*)\]/);
146         if @registry.has_key? $1 and not params[:recurs]
147           if user1 == $1
148             params[:user1] = @registry[ $1 ]
149           elsif user2 == $1
150             params[:user2] = @registry[ $1 ]
151           end
152           params[:recurs] = true
153           tasteometer(m, params)
154         else
155           m.reply _("%{u} doesn't exist at last.fm. Perhaps you need to: lastfm set <username>") % {:u => baduser}
156           return
157         end
158       else
159         m.reply _("Bad: %{e}") % {:e => doc.root.element["error"].text}
160         return
161       end
162     end
163     now = artist = track = albumtxt = date = nil
164     score = doc.root.elements["comparison/result/score"].text.to_f
165     rating = nil
166     case
167       when score >= 0.9
168         rating = _("Super")
169       when score >= 0.7
170         rating = _("Very High")
171       when score >= 0.5
172         rating = _("High")
173       when score >= 0.3
174         rating = _("Medium")
175       when score >= 0.1
176         rating = _("Low")
177       else
178         rating = _("Very Low")
179     end
180     m.reply _("%{a}'s and %{b}'s musical compatibility rating is: %{r}") % {:a => user1, :b => user2, :r => rating}
181   end
182
183   def now_playing(m, params)
184     opts = { :cache => false }
185     user = nil
186     if params[:who]
187       user = params[:who].to_s
188     elsif @registry.has_key? m.sourcenick
189       user = @registry[ m.sourcenick ]
190     else
191       user = m.sourcenick
192     end
193     xml = @bot.httputil.get_response("#{APIURL}method=user.getrecenttracks&user=#{user}", opts)
194     doc = Document.new xml.body
195     unless doc
196       m.reply _("last.fm parsing failed")
197       return
198     end
199     if xml.class == Net::HTTPBadRequest
200       if doc.root.elements["error"].text == "Invalid user name supplied" then 
201         if @registry.has_key? user and not params[:recurs]
202           params[:who] = @registry[ user ]
203           params[:recurs] = true
204           now_playing(m, params)
205         else
206           m.reply "#{user} doesn't exist at last.fm. Perhaps you need to: lastfm set <username>"
207           return
208         end
209       else
210         m.reply _("Error %{e}") % {:e => doc.root.element["error"].text}
211         return
212       end
213     end
214     now = artist = track = albumtxt = date = nil
215     unless doc.root.elements[1].has_elements?
216      m.reply _("%{u} hasn't played anything recently") % {:u => user}
217      return
218     end
219     first = doc.root.elements[1].elements[1]
220     now = first.attributes["nowplaying"]
221     artist = first.elements["artist"].text
222     track = first.elements["name"].text
223     albumtxt = first.elements["album"].text
224     year = get_album(artist, albumtxt)[2]
225     album = "[#{albumtxt}, #{year}] " unless albumtxt == nil or year.length == 1
226     date = first.elements["date"].attributes["uts"]
227     past = Time.at(date.to_i)
228     if now == "true"
229        verb = _("listening")
230        if @registry.has_key? "#{m.sourcenick}_verb_present"
231          verb = @registry["#{m.sourcenick}_verb_present"]
232        end
233       m.reply _("%{u} is %{v} to \"%{t}\" by %{a} %{b}") % {:u => user, :v => verb, :t => track, :a => artist, :b => album}
234     else
235       verb = _("listened")
236        if @registry.has_key? "#{m.sourcenick}_verb_past"
237          verb = @registry["#{m.sourcenick}_verb_past"]
238        end
239       ago = Utils.timeago(past)
240       m.reply _("%{u} %{v} to \"%{t}\" by %{a} %{b}%{p}") % {:u => user, :v => verb, :t => track, :a => artist, :b => album, :p => ago}
241     end
242   end
243
244   def find_artist(m, params)
245     xml = @bot.httputil.get(URI.escape("#{APIURL}method=artist.getinfo&artist=#{params[:artist]}"))
246     unless xml
247       m.reply _("I had problems getting info for %{a}.") % {:a => params[:artist]}
248       return
249     end
250     doc = Document.new xml
251     unless doc
252       m.reply _("last.fm parsing failed")
253       return
254     end
255     first = doc.root.elements["artist"]
256     artist = first.elements["name"].text
257     playcount = first.elements["stats"].elements["plays"].text
258     listeners = first.elements["stats"].elements["listeners"].text
259     summary = first.elements["bio"].elements["summary"].text
260     m.reply _("\"%{a}\" has been played %{c} times and is being listened to by %{l} people.") % {:a => artist, :c => playcount, :l => listeners}
261     m.reply summary.strip
262   end
263
264   def get_album(artist, album)
265     xml = @bot.httputil.get(URI.escape("#{APIURL}method=album.getinfo&artist=#{artist}&album=#{album}"))
266     unless xml
267       return [_("I had problems getting album info")]
268     end
269     doc = Document.new xml
270     unless doc
271       return [_("last.fm parsing failed")]
272     end
273     album = date = playcount = artist = date = year = nil
274     first = doc.root.elements["album"]
275     artist = first.elements["artist"].text
276     playcount = first.elements["playcount"].text
277     album = first.elements["name"].text
278     date = first.elements["releasedate"].text
279     unless date.strip.length < 2 
280       year = date.strip.split[2].chop
281     end
282     result = [artist, album, year, playcount]
283     return result
284   end
285
286   def find_album(m, params)
287     album = get_album(params[:artist].to_s, params[:album].to_s)
288     if album.length == 1
289       m.reply _("I couldn't locate: \"%{a}\" by %{r}") % {:a => params[:album], :r => params[:artist]}
290       return
291     end
292     year = "(#{album[2]}) " unless album[2] == nil
293     m.reply _("The album \"%{a}\" by %{r} %{y}has been played %{c} times.") % {:a => album[1], :r => album[0], :y => year, :c => album[3]}
294   end
295
296   def set_user(m, params)
297     user = params[:who].to_s
298     nick = m.sourcenick
299     @registry[ nick ] = user
300     m.reply _("Ok, I'll remember that %{n} is %{u} at last.fm") % {:n => nick, :u => user}
301   end
302
303   def set_verb(m, params)
304     past = params[:past].to_s
305     present = params[:present].to_s
306     key = "#{m.sourcenick}_verb_"
307     @registry[ "#{key}past" ] = past
308     @registry[ "#{key}present" ] = present
309     m.reply _("Ok, I'll remember that %{n} prefers %{r} and %{p}.") % {:n => m.sourcenick, :p => past, :r => present}
310   end
311
312   def get_user(m, params)
313     nick = ""
314     if params[:who]
315       nick = params[:who].to_s
316     else 
317       nick = m.sourcenick
318     end
319     if @registry.has_key? nick
320       user = @registry[ nick ]
321       m.reply "#{nick} is #{user} at last.fm"
322     else
323       m.reply _("Sorry, I don't know who %{n} is at last.fm perhaps you need to: lastfm set <username>") % {:n => nick}
324     end
325   end
326
327   def lastfm(m, params)
328     action = params[:action].intern
329     action = :neighbours if action == :neighbors
330     user = nil
331     if params[:user] then
332       user = params[:user].to_s
333     elsif @registry.has_key? m.sourcenick
334       user = @registry[ m.sourcenick ]
335     else
336       # m.reply "I don't know who you are on last.fm. Use 'lastfm set username' to identify yourself."
337       # return
338       user = m.sourcenick
339     end
340     begin
341       data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
342       m.reply "#{action} for #{user}:"
343       m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
344     rescue
345       m.reply "could not find #{action} for #{user} (is #{user} a user?). perhaps you need to: lastfm set <username>"
346     end
347   end
348 end
349
350 plugin = LastFmPlugin.new
351 plugin.map 'lastfm [:num] event[s] in *location', :action => :find_events, :requirements => { :num => /\d+/ }, :thread => true
352 plugin.map 'lastfm [:num] event[s] by *who', :action => :find_events, :requirements => { :num => /\d+/ }, :thread => true
353 plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_events, :requirements => { :num => /\d+/ }, :thread => true
354 plugin.map 'lastfm now :who', :action => :now_playing, :thread => true
355 plugin.map 'lastfm now', :action => :now_playing, :thread => true
356 plugin.map 'np :who', :action => :now_playing, :thread => true
357 plugin.map 'lastfm artist *artist', :action => :find_artist, :thread => true
358 plugin.map 'lastfm album *album [by *artist]', :action => :find_album
359 plugin.map 'lastfm set nick :who', :action => :set_user, :thread => true
360 plugin.map 'lastfm set verb :present :past', :action => :set_verb, :thread => true
361 plugin.map 'lastfm who :who', :action => :get_user, :thread => true
362 plugin.map 'lastfm who', :action => :get_user, :thread => true
363 plugin.map 'lastfm compare :user1 :user2', :action => :tasteometer, :thread => true
364 #plugin.map 'lastfm :action :user', :thread => true
365 #plugin.map 'lastfm :action', :thread => true
366 plugin.map 'np', :action => :now_playing, :thread => true
367 plugin.map 'lastfm', :action => :now_playing, :thread => true