+ 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 = nil
+ if params[:who]
+ user = params[:who].to_s
+ elsif @registry.has_key? m.sourcenick
+ user = @registry[ m.sourcenick ]
+ else
+ user = m.sourcenick
+ end
+ xml = @bot.httputil.get_response("#{APIURL}method=user.getrecenttracks&user=#{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
+ if @registry.has_key? user and not params[:recurs]
+ params[:who] = @registry[ user ]
+ params[:recurs] = true
+ now_playing(m, params)
+ else
+ m.reply "#{user} doesn't exist at last.fm. Perhaps you need to: lastfm set <username>"
+ return
+ end
+ 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
+ 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(URI.escape("#{APIURL}method=artist.getinfo&artist=#{params[:artist]}"))
+ 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 _("\"%{a}\" has been played %{c} times and is being listened to by %{l} people.") % {:a => artist, :c => playcount, :l => listeners}
+ m.reply summary.strip
+ end
+
+ def get_album(artist, album)
+ xml = @bot.httputil.get(URI.escape("#{APIURL}method=album.getinfo&artist=#{artist}&album=#{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(", ")