]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/lastfm.rb
grouphug plugin: help text simplification
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / lastfm.rb
index 05d9df412f31dbeda039c8aba172ef7d20eff764..7c0309e14ee81d43872024668b980c476d624351 100644 (file)
 require 'rexml/document'
 
 class ::LastFmEvent
-  def initialize(url, date, artist, location, description)
-    @url = url
-    @date = date
-    @artist = artist
-    @location = location
-    @description = description
+  def initialize(hash)
+    @url = hash[:url]
+    @date = hash[:date]
+    @location = hash[:location]
+    @description = hash[:description]
+    @attendance = hash[:attendance]
+
+    @artists = hash[:artists]
+
+    if @artists.length > 10 #more than 10 artists and it floods
+      diff = @artists.length - 10
+      @artist_string = @artists[0..10].join(', ')
+      @artist_string << _(" and %{n} more...") % {:n => diff}
+    else
+      @artist_string = @artists.join(', ')
+    end
   end
 
   def compact_display
-    return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @url]
+   if @attendance
+     return "%s %s @ %s (%s attending) %s" % [@date.strftime("%a %b, %d %Y"), @artist_string, @location, @attendance, @url]
+   end
+   return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist_string, @location, @url]
   end
   alias :to_s :compact_display
 
@@ -40,8 +53,6 @@ class LastFmPlugin < Plugin
     :default => 3, :validate => Proc.new{|v| v > 1},
     :desc => "Default number of events to display.")
 
-  LASTFM = "http://www.last.fm"
-
   APIKEY = "b25b959554ed76058ac220b7b2e0a026"
   APIURL = "http://ws.audioscrobbler.com/2.0/?api_key=#{APIKEY}&"
 
@@ -72,7 +83,7 @@ class LastFmPlugin < Plugin
     when :who
       _("lastfm who [<nick>] => show who <nick> is at last.fm. if <nick> is empty, show who you are at lastfm.")
     else
-      _("lastfm [<user>] => show your or <user>'s now playing track at lastfm. np [<user>] => same as 'lastfm'. lastfm <function> [<user>] => lastfm data for <user> on last.fm where <function> in [recenttracks, topartists, topalbums, toptracks, tags, friends, neighbors]. other topics: events, artist, album, now, set, who")
+      _("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")
     end
   end
 
@@ -103,26 +114,27 @@ class LastFmPlugin < Plugin
     disp_events = Array.new
     events = Array.new
     doc.root.elements.each("events/event"){ |e|
-      title = e.elements["title"].text
+      h = {}
+      h[:title] = e.elements["title"].text
       venue = e.elements["venue"].elements["name"].text
       city = e.elements["venue"].elements["location"].elements["city"].text
       country =  e.elements["venue"].elements["location"].elements["country"].text
-      loc = Bold + venue + Bold + " #{city}, #{country}"
+      h[:location] = Bold + venue + Bold + " #{city}, #{country}"
       date = e.elements["startDate"].text.split
-      date = Time.utc(date[3].to_i, date[2], date[1].to_i)
-      desc = e.elements["description"].text
-      url = e.elements["url"].text
+      h[:date] = Time.utc(date[3].to_i, date[2], date[1].to_i)
+      h[:desc] = e.elements["description"].text
+      h[:url] = e.elements["url"].text
+      e.detect {|node|
+        if node.kind_of? Element and node.attributes["name"] == "attendance" then
+          h[:attendance] = node.text
+        end
+      }
       artists = Array.new
       e.elements.each("artists/artist"){ |a|
         artists << a.text
       }
-      if artists.length > 10 #more than 10 artists and it floods
-       diff = artists.length - 10
-       artists = artists[0..10]
-       artists << _(" and %{n} more...") % {:n => diff}
-      end
-      artists = artists.join(", ")
-      events << LastFmEvent.new(url, date, artists, loc, desc)
+      h[:artists] = artists
+      events << LastFmEvent.new(h)
     }
     events[0...num].each { |event|
       disp_events << event.to_s
@@ -155,9 +167,11 @@ class LastFmPlugin < Plugin
           tasteometer(m, params)
         else
           m.reply _("%{u} doesn't exist at last.fm. Perhaps you need to: lastfm set <username>") % {:u => baduser}
+          return
         end
       else
         m.reply _("Bad: %{e}") % {:e => doc.root.element["error"].text}
+        return
       end
     end
     now = artist = track = albumtxt = date = nil
@@ -204,22 +218,28 @@ class LastFmPlugin < Plugin
           now_playing(m, params)
         else
           m.reply "#{user} doesn't exist at last.fm. Perhaps you need to: lastfm set <username>"
+          return
         end
       else
         m.reply _("Error %{e}") % {:e => doc.root.element["error"].text}
+        return
       end
     end
     now = artist = track = albumtxt = date = nil
-    unless doc.root.elements["recenttracks"].has_elements?
-      m.reply _("%{u} hasn't played anything recently") % {:u => user}
+    unless doc.root.elements[1].has_elements?
+     m.reply _("%{u} hasn't played anything recently") % {:u => user}
+     return
     end
     first = doc.root.elements[1].elements[1]
     now = first.attributes["nowplaying"]
     artist = first.elements["artist"].text
     track = first.elements["name"].text
     albumtxt = first.elements["album"].text
-    year = get_album(artist, albumtxt)[2]
-    album = "[#{albumtxt}, #{year}] " unless albumtxt == nil or year.length == 1
+    album = ""
+    if albumtxt
+      year = get_album(artist, albumtxt)[2]
+      album = "[#{albumtxt}, #{year}] " if year
+    end
     date = first.elements["date"].attributes["uts"]
     past = Time.at(date.to_i)
     if now == "true"
@@ -242,10 +262,12 @@ class LastFmPlugin < Plugin
     xml = @bot.httputil.get(URI.escape("#{APIURL}method=artist.getinfo&artist=#{params[:artist]}"))
     unless xml
       m.reply _("I had problems getting info for %{a}.") % {:a => params[:artist]}
+      return
     end
     doc = Document.new xml
     unless doc
       m.reply _("last.fm parsing failed")
+      return
     end
     first = doc.root.elements["artist"]
     artist = first.elements["name"].text
@@ -301,7 +323,7 @@ class LastFmPlugin < Plugin
     key = "#{m.sourcenick}_verb_"
     @registry[ "#{key}past" ] = past
     @registry[ "#{key}present" ] = present
-    m.reply _("Ok, I'll remember that %{n} prefers %{p} and %{r}.") % {:n => m.sourcenick, :p => past, :r => present}
+    m.reply _("Ok, I'll remember that %{n} prefers %{r} and %{p}.") % {:n => m.sourcenick, :p => past, :r => present}
   end
 
   def get_user(m, params)
@@ -319,9 +341,15 @@ class LastFmPlugin < Plugin
     end
   end
 
+  # TODO this user data retrieval should be upgraded to API 2.0 but it would need separate parsing
+  # for each dataset, or almost
   def lastfm(m, params)
     action = params[:action].intern
     action = :neighbours if action == :neighbors
+    action = :recenttracks if action == :recentracks
+    action = :topalbums if action == :topalbum
+    action = :topartists if action == :topartist
+    action = :toptags if action == :toptag
     user = nil
     if params[:user] then
       user = params[:user].to_s
@@ -356,7 +384,9 @@ plugin.map 'lastfm set verb :present :past', :action => :set_verb, :thread => tr
 plugin.map 'lastfm who :who', :action => :get_user, :thread => true
 plugin.map 'lastfm who', :action => :get_user, :thread => true
 plugin.map 'lastfm compare :user1 :user2', :action => :tasteometer, :thread => true
-#plugin.map 'lastfm :action :user', :thread => true
-#plugin.map 'lastfm :action', :thread => true
 plugin.map 'np', :action => :now_playing, :thread => true
 plugin.map 'lastfm', :action => :now_playing, :thread => true
+plugin.map "lastfm [user] :action [:user]", :thread => true,
+  :requirements => { :action =>
+    /^(?:events|friends|neighbou?rs|playlists|recent?tracks|top(?:album|artist|tag)s?|weekly(?:album|artist|track)chart|weeklychartlist)$/
+}