+ end
+ now = artist = track = albumtxt = date = nil
+ unless doc.root.elements[1].has_elements?
+ m.reply _("%{u} hasn't played anything recently") % {:u => user}
+ return
+ end
+ first = doc.root.elements[1].elements[1]
+ now = first.attributes["nowplaying"]
+ artist = first.elements["artist"].text
+ track = first.elements["name"].text
+ albumtxt = first.elements["album"].text
+ 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"
+ verb = _("listening")
+ if @registry.has_key? "#{m.sourcenick}_verb_present"
+ verb = @registry["#{m.sourcenick}_verb_present"]
+ end
+ m.reply _("%{u} is %{v} to \"%{t}\" by %{a} %{b}") % {:u => user, :v => verb, :t => track, :a => artist, :b => album}
+ else
+ verb = _("listened")
+ if @registry.has_key? "#{m.sourcenick}_verb_past"
+ verb = @registry["#{m.sourcenick}_verb_past"]
+ end
+ ago = Utils.timeago(past)
+ m.reply _("%{u} %{v} to \"%{t}\" by %{a} %{b}%{p}") % {:u => user, :v => verb, :t => track, :a => artist, :b => album, :p => ago}
+ end
+ end
+
+ def find_artist(m, params)
+ xml = @bot.httputil.get("#{APIURL}method=artist.getinfo&artist=#{CGI.escape params[:artist].to_s}")
+ unless xml
+ m.reply _("I had problems getting info for %{a}.") % {:a => params[:artist]}
+ return
+ end
+ doc = Document.new xml
+ unless doc
+ m.reply _("last.fm parsing failed")
+ return
+ end
+ first = doc.root.elements["artist"]
+ artist = first.elements["name"].text
+ playcount = first.elements["stats"].elements["plays"].text
+ listeners = first.elements["stats"].elements["listeners"].text
+ summary = first.elements["bio"].elements["summary"].text
+ m.reply _("%{b}%{a}%{b} has been played %{c} times and is being listened to by %{l} people.") % {:b => Bold, :a => artist, :c => playcount, :l => listeners}
+ m.reply summary.ircify_html
+ end
+
+ def find_track(m, params)
+ track = params[:track].to_s
+ xml = @bot.httputil.get("#{APIURL}method=track.search&track=#{CGI.escape track}")
+ unless xml
+ m.reply _("I had problems getting info for %{a}.") % {:a => track}
+ return
+ end
+ debug xml
+ doc = Document.new xml
+ unless doc
+ m.reply _("last.fm parsing failed")
+ return
+ end
+ debug doc.root
+ results = doc.root.elements["results/opensearch:totalResults"].text.to_i rescue 0
+ if results > 0
+ begin
+ hits = []
+ doc.root.each_element("results/trackmatches/track") do |track|
+ 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,
+ :bold => Bold
+ }
+ end
+ m.reply hits.join(' -- '), :split_at => ' -- '
+ rescue
+ error $!
+ m.reply _("last.fm parsing failed")
+ end
+ else
+ m.reply _("track %{a} not found") % {:a => track}
+ end
+ end
+
+ def get_album(artist, album)
+ xml = @bot.httputil.get("#{APIURL}method=album.getinfo&artist=#{CGI.escape artist}&album=#{CGI.escape album}")
+ unless xml
+ return [_("I had problems getting album info")]
+ end
+ doc = Document.new xml
+ unless doc
+ return [_("last.fm parsing failed")]
+ end
+ album = date = playcount = artist = date = year = nil
+ first = doc.root.elements["album"]
+ artist = first.elements["artist"].text
+ playcount = first.elements["playcount"].text
+ album = first.elements["name"].text
+ date = first.elements["releasedate"].text
+ unless date.strip.length < 2
+ year = date.strip.split[2].chop
+ end
+ result = [artist, album, year, playcount]
+ return result
+ end
+
+ def find_album(m, params)
+ album = get_album(params[:artist].to_s, params[:album].to_s)
+ if album.length == 1
+ m.reply _("I couldn't locate: \"%{a}\" by %{r}") % {:a => params[:album], :r => params[:artist]}
+ return
+ end
+ year = "(#{album[2]}) " unless album[2] == nil
+ m.reply _("The album \"%{a}\" by %{r} %{y}has been played %{c} times.") % {:a => album[1], :r => album[0], :y => year, :c => album[3]}
+ end
+
+ def set_user(m, params)
+ user = params[:who].to_s
+ nick = m.sourcenick
+ @registry[ nick ] = user
+ m.reply _("Ok, I'll remember that %{n} is %{u} at last.fm") % {:n => nick, :u => user}
+ end
+
+ def set_verb(m, params)
+ past = params[:past].to_s
+ present = params[:present].to_s
+ key = "#{m.sourcenick}_verb_"
+ @registry[ "#{key}past" ] = past
+ @registry[ "#{key}present" ] = present
+ m.reply _("Ok, I'll remember that %{n} prefers %{r} and %{p}.") % {:n => m.sourcenick, :p => past, :r => present}
+ end
+
+ def get_user(m, params)
+ nick = ""
+ if params[:who]
+ nick = params[:who].to_s
+ else
+ nick = m.sourcenick
+ end
+ if @registry.has_key? nick
+ user = @registry[ nick ]
+ m.reply "#{nick} is #{user} at last.fm"
+ else
+ m.reply _("Sorry, I don't know who %{n} is at last.fm perhaps you need to: lastfm set <username>") % {:n => nick}
+ 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
+ elsif @registry.has_key? m.sourcenick
+ user = @registry[ m.sourcenick ]
+ else
+ # m.reply "I don't know who you are on last.fm. Use 'lastfm set username' to identify yourself."
+ # return
+ user = m.sourcenick
+ end
+ begin
+ data = @bot.httputil.get("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
+ m.reply "#{action} for #{user}:"
+ m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")