]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/lastfm.rb
spotify: handle errors
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / lastfm.rb
index f5cd3ec7c7ba8a699a4c266ac874a1a4fac81d55..57cf3ec14596078c0aec421f3eecb2a7b5d758c6 100644 (file)
 #
 # License:: GPL v2
 
-module Spotify
-  def self.get(service, method, query, page=1)
-    query.tr!('-','')
-    url = "http://ws.spotify.com/#{service}/1/#{method}?q=#{CGI.escape(query)}&page=#{page}"
-    xml = Irc::Utils.bot.httputil.get_response(url).body
-    return REXML::Document.new(xml).root
-  end
-
-  # returns a Spotify URL, e.g. 'http://open.spotify.com/track/3y6EhoUO3A8bPr3zt3Tm9b'
-  def self.search(method, query, page=1)
-    doc = get(:search, method, query, page)
-    return nil if doc.elements["opensearch:totalResults"].text.to_i.zero?
-    uri = doc.elements[method.to_s].attributes["href"]
-    id  = uri[uri.rindex(':')+1..-1]
-    return URI.escape("http://open.spotify.com/#{method}/#{id}")
-  end
-end
-
 require 'rexml/document'
 require 'cgi'
 
@@ -395,18 +377,20 @@ class LastFmPlugin < Plugin
        if @registry.has_key? "#{m.sourcenick}_verb_present"
          verb = @registry["#{m.sourcenick}_verb_present"]
        end
-       reply = _("%{u} %{v} \"%{t}\" by %{a}%{b}") % {:u => user, :v => verb, :t => track, :a => artist, :b => album, :bold => Bold}
+       reply = _("%{u} %{v} \"%{t}\" by %{bold}%{a}%{bold}%{b}") % {:u => user, :v => verb, :t => track, :a => artist, :b => album, :bold => Bold}
     else
       verb = _("listened to")
        if @registry.has_key? "#{m.sourcenick}_verb_past"
          verb = @registry["#{m.sourcenick}_verb_past"]
        end
       ago = Utils.timeago(past)
-      reply = _("%{u} %{v} \"%{t}\" by %{a}%{b} %{p};") % {:u => user, :v => verb, :t => track, :a => artist, :b => album, :p => ago, :bold => Bold}
+      reply = _("%{u} %{v} \"%{t}\" by %{bold}%{a}%{bold}%{b} %{p};") % {:u => user, :v => verb, :t => track, :a => artist, :b => album, :p => ago, :bold => Bold}
     end
 
-    if (spotify_url = Spotify.search(:track, "#{artist} #{track}"))
-      reply << _(" [%{u}%{url}%{u}]") % {:u => Underline, :url => spotify_url}
+    if Object.const_defined?('Spotify')
+      if track = Spotify.search(:track, "#{artist} #{track}")
+        reply << _(" [%{u}%{url}%{u}]") % {:u => Underline, :url => track.url}
+      end
     end
 
     reply << _(" -- see %{uri} for more") % { :uri => "http://www.last.fm/user/#{CGI.escape user}"}