]> 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 3801126e5a8b037d765cacf8c30b709e53f09093..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
 
@@ -101,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
@@ -221,8 +235,11 @@ class LastFmPlugin < Plugin
     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"
@@ -324,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
@@ -361,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)$/
+}