+ end
+ now = artist = track = albumtxt = date = nil
+ score = doc.root.elements["comparison/result/score"].text.to_f
+ rating = nil
+ case
+ when score >= 0.9
+ rating = _("Super")
+ when score >= 0.7
+ rating = _("Very High")
+ when score >= 0.5
+ rating = _("High")
+ when score >= 0.3
+ rating = _("Medium")
+ when score >= 0.1
+ rating = _("Low")
+ else
+ rating = _("Very Low")
+ end
+ m.reply _("%{a}'s and %{b}'s musical compatibility rating is: %{r}") % {:a => user1, :b => user2, :r => rating}
+ end
+
+ def now_playing(m, params)
+ opts = { :cache => false }
+ user = resolve_username(m, params[:who])
+ xml = @bot.httputil.get_response("#{APIURL}method=user.getrecenttracks&user=#{CGI.escape user}", opts)
+ doc = Document.new xml.body
+ unless doc
+ m.reply _("last.fm parsing failed")
+ return
+ end
+ if xml.class == Net::HTTPBadRequest
+ if doc.root.elements["error"].text == "Invalid user name supplied" then
+ m.reply _("%{user} doesn't exist on last.fm, perhaps they need to: lastfm 2 <username>") % {
+ :user => user
+ }
+ return
+ else
+ m.reply _("error: %{e}") % {:e => doc.root.element["error"].text}
+ return
+ end
+ 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
+ past = nil
+ date = XPath.first(first, "//date")
+ if date != nil
+ time = date.attributes["uts"]
+ past = Time.at(time.to_i)
+ end
+ if now == "true"
+ verb = _("is listening to")
+ if @registry.has_key? "#{m.sourcenick}_verb_present"
+ verb = @registry["#{m.sourcenick}_verb_present"]
+ end
+ m.reply _("%{u} %{v} \"%{t}\" by %{a} %{b}") % {:u => user, :v => verb, :t => track, :a => artist, :b => album}
+ else
+ verb = _("listened to")
+ if @registry.has_key? "#{m.sourcenick}_verb_past"
+ verb = @registry["#{m.sourcenick}_verb_past"]
+ end
+ ago = Utils.timeago(past)
+ m.reply _("%{u} %{v} \"%{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["playcount"].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 _("okay, I'll remember that %{n} is %{u} on 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 _("okay, 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} on last.fm") % {
+ :nick => nick,
+ :user => user
+ }
+ else
+ m.reply _("sorry, I don't know who %{n} is on last.fm, perhaps they need to: lastfm set user <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 = resolve_username(m, params[:user])
+ 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(", ")