X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Flastfm.rb;h=e29ae31ea477fa91a1daf888578caef86c2a5c6a;hb=0ddf77c9d54236bbc75c2b603871fc0c9e072873;hp=f5cd3ec7c7ba8a699a4c266ac874a1a4fac81d55;hpb=e4d81b0cf2c5158d859c33b6e1cbde442cb183de;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/lastfm.rb b/data/rbot/plugins/lastfm.rb index f5cd3ec7..e29ae31e 100644 --- a/data/rbot/plugins/lastfm.rb +++ b/data/rbot/plugins/lastfm.rb @@ -15,24 +15,6 @@ # # 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' @@ -212,11 +194,11 @@ class LastFmPlugin < Plugin elsif venue begin venues = search_venue_by(:name => venue.to_s, :limit => 1) - rescue Exception => e - error e + rescue Exception => err + error err m.reply _("an error occurred looking for venue %{venue}: %{e}") % { :venue => venue.to_s, - :e => e.message + :e => err.message } end @@ -355,8 +337,9 @@ class LastFmPlugin < Plugin end if xml.class == Net::HTTPBadRequest if doc.root.elements["error"].attributes["code"] == "6" then - m.reply _("%{user} doesn't exist on last.fm, perhaps they need to: lastfm user ") % { - :user => user + m.reply _("%{user} doesn't exist on last.fm, perhaps they need to: %{prefix}lastfm set user ") % { + :user => user, + :prefix => @bot.config['core.address_prefix'].first } return else @@ -371,7 +354,7 @@ class LastFmPlugin < Plugin end first = doc.root.elements[1].elements[1] now = first.attributes["nowplaying"] - artist = first.elements["artist"].text + artist = first.elements["artist/name"].text track = first.elements["name"].text albumtxt = first.elements["album"].text album = if albumtxt @@ -395,18 +378,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 @bot.plugins['spotify'] && 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}"} @@ -427,7 +412,7 @@ class LastFmPlugin < Plugin tags_xml = @bot.httputil.get("#{APIURL}method=artist.gettoptags&artist=#{CGI.escape params[:artist].to_s}") tags_doc = Document.new tags_xml - first = info_doc.root.elements["artist"] + first = info_doc.root.elements["artist/name"] artist = first.elements["name"].text url = first.elements["url"].text stats = {} @@ -468,11 +453,11 @@ class LastFmPlugin < Plugin if results > 0 begin hits = [] - doc.root.each_element("results/trackmatches/track") do |track| + doc.root.each_element("results/trackmatches/track") do |trck| hits << _("%{bold}%{t}%{bold} by %{bold}%{a}%{bold} (%{n} listeners)") % { - :t => track.elements["name"].text, - :a => track.elements["artist"].text, - :n => track.elements["listeners"].text, + :t => trck.elements["name"].text, + :a => trck.elements["artist/name"].text, + :n => trck.elements["listeners"].text, :bold => Bold } end @@ -623,8 +608,8 @@ class LastFmPlugin < Plugin begin res = @bot.httputil.get_response(uri) raise _("no response body") unless res.body - rescue Exception => e - m.reply _("I had problems accessing last.fm: %{e}") % {:e => e.message} + rescue Exception => err + m.reply _("I had problems accessing last.fm: %{e}") % {:e => err.message} return end doc = Document.new(res.body) @@ -700,7 +685,7 @@ class LastFmPlugin < Plugin } when :recenttracks tracks = doc.root.get_elements("recenttracks/track").map do |track| - [track.elements["artist"].text, track.elements["name"].text].join(" - ") + [track.elements["artist/name"].text, track.elements["name"].text].join(" - ") end counts = [] @@ -745,7 +730,7 @@ class LastFmPlugin < Plugin case action when :weeklytrackchart, :weeklyalbumchart format = "%{artist} - %{title} (%{bold}%{plays}%{bold})" - artist = item.elements["artist"].text + artist = item.elements["artist/name"].text when :weeklyartistchart, :topartists format = "%{artist} (%{bold}%{plays}%{bold})" artist = item.elements["name"].text