]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/lastfm.rb
lastfm plugin: better handling missing album in playing info
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / lastfm.rb
index 541fc1dc8d7150eb6de3f1d1b78d5949cd43d2d6..2e5dc3f53b2d4ba72259b8144b0c60d67e387274 100644 (file)
@@ -17,19 +17,28 @@ require 'rexml/document'
 
 class ::LastFmEvent
   def initialize(hash)
-    @url = hash[:url] if hash.key? :url
-    @date = hash[:date] if hash.key? :date
-    @artist = hash[:artist] if hash.key? :artist
-    @location = hash[:location] if hash.key? :location
-    @description = hash[:description] if hash.key? :description
-    @attendance = hash[:attendance] if hash.key? :attendance
+    @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
    if @attendance
-     return "%s %s @ %s (%s attending) %s" % [@date.strftime("%a %b, %d %Y"), @artist, @location, @attendance, @url]
+     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, @location, @url]
+   return "%s %s @ %s %s" % [@date.strftime("%a %b, %d %Y"), @artist_string, @location, @url]
   end
   alias :to_s :compact_display
 
@@ -124,12 +133,7 @@ class LastFmPlugin < Plugin
       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
-      h[:artists] = artists.join(", ")
+      h[:artists] = artists
       events << LastFmEvent.new(h)
     }
     events[0...num].each { |event|
@@ -231,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"