diff options
Diffstat (limited to 'data')
-rw-r--r-- | data/rbot/plugins/lastfm.rb | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/data/rbot/plugins/lastfm.rb b/data/rbot/plugins/lastfm.rb index 3d0b79be..77584941 100644 --- a/data/rbot/plugins/lastfm.rb +++ b/data/rbot/plugins/lastfm.rb @@ -15,6 +15,23 @@ # # License:: GPL v2 +module Spotify + def self.get(service, method, query, page=1) + url = URI.escape("http://ws.spotify.com/#{service}/1/#{method}?q=#{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' @@ -384,10 +401,14 @@ class LastFmPlugin < Plugin 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 %{a}%{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} end - reply << _("; see %{uri} for more") % { :uri => "http://www.last.fm/user/#{CGI.escape user}"} + reply << _(" -- see %{uri} for more") % { :uri => "http://www.last.fm/user/#{CGI.escape user}"} m.reply reply end |