+ doc = Document.new(res.body)
+ unless doc
+ m.reply _("last.fm parsing failed")
+ return
+ end
+
+ case res
+ when Net::HTTPBadRequest
+ if doc.root and doc.root.attributes["status"] == "failed"
+ m.reply "error: " << doc.root.elements["error"].text.downcase
+ end
+ return
+ end
+
+ seemore = _("; see %{uri} for more")
+ case action
+ when :friends
+ friends = doc.root.get_elements("friends/user").map do |u|
+ u.elements["name"].text
+ end
+
+ if friends.empty?
+ reply = _("%{user} has no friends :(")
+ elsif friends.length <= num
+ reply = _("%{user} has %{total} friends: %{friends}")
+ else
+ reply = _("%{user} has %{total} friends, including %{friends}%{seemore}")
+ end
+ m.reply reply % {
+ :user => user,
+ :total => friends.size,
+ :friends => Utils.comma_list(friends.shuffle[0, num]),
+ :uri => "http://www.last.fm/user/#{CGI.escape user}/friends",
+ :seemore => seemore
+ }
+ when :lovedtracks
+ loved = doc.root.get_elements("lovedtracks/track").map do |track|
+ [track.elements["artist/name"].text, track.elements["name"].text].join(" - ")
+ end
+ loved_prep = loved.shuffle[0, num].to_enum(:each_with_index).collect { |e,i| (i % 2).zero? ? Underline+e+Underline : e }
+
+ if loved.empty?
+ reply = _("%{user} has not loved any tracks")
+ elsif loved.length <= num
+ reply = _("%{user} has loved %{total} tracks: %{tracks}")
+ else
+ reply = _("%{user} has loved %{total} tracks, including %{tracks}%{seemore}")
+ end
+ m.reply reply % {
+ :user => user,
+ :total => loved.size,
+ :tracks => Utils.comma_list(loved_prep),
+ :uri => "http://www.last.fm/user/#{CGI.escape user}/library/loved",
+ :seemore => seemore
+ }
+ when :neighbours
+ nbrs = doc.root.get_elements("neighbours/user").map do |u|
+ u.elements["name"].text
+ end
+
+ if nbrs.empty?
+ reply = _("no one seems to share %{user}'s musical taste")
+ elsif nbrs.length <= num
+ reply = _("%{user}'s musical neighbours are %{nbrs}")
+ else
+ reply = _("%{user}'s musical neighbours include %{nbrs}%{seemore}")
+ end
+ m.reply reply % {
+ :user => user,
+ :nbrs => Utils.comma_list(nbrs.shuffle[0, num]),
+ :uri => "http://www.last.fm/user/#{CGI.escape user}/neighbours",
+ :seemore => seemore
+ }
+ when :recenttracks
+ tracks = doc.root.get_elements("recenttracks/track").map do |track|
+ [track.elements["artist"].text, track.elements["name"].text].join(" - ")
+ end
+
+ counts = []
+ tracks.each do |track|
+ if t = counts.assoc(track)
+ counts[counts.rindex(t)] = [track, t[-1] += 1]
+ else
+ counts << [track, 1]
+ end
+ end
+
+ tracks_prep = counts[0, num].to_enum(:each_with_index).map do |e,i|
+ str = (i % 2).zero? ? Underline+e[0]+Underline : e[0]
+ str << " (%{i} times%{m})" % {
+ :i => e.last,
+ :m => counts.size == 1 ? _(" or more") : nil
+ } if e.last > 1
+ str
+ end
+
+ if tracks.empty?
+ m.reply _("%{user} hasn't played anything recently") % { :user => user }
+ else
+ m.reply _("%{user} has recently played %{tracks}") %
+ { :user => user, :tracks => Utils.comma_list(tracks_prep) }
+ end
+ when :shouts
+ shouts = doc.root.get_elements("shouts/shout")
+ if shouts.empty?
+ m.reply _("there are no shouts for %{user}") % { :user => user }
+ else
+ shouts[0, num].each do |shout|
+ m.reply _("<%{author}> %{body}") % {
+ :body => shout.elements["body"].text,
+ :author => shout.elements["author"].text,
+ }
+ end
+ end
+ when :toptracks, :topalbums, :topartists, :weeklytrackchart, :weeklyalbumchart, :weeklyartistchart
+ type = action.to_s.scan(/track|album|artist/).to_s
+ items = doc.root.get_elements("#{action}/#{type}").map do |item|
+ case action
+ when :weeklytrackchart, :weeklyalbumchart
+ format = "%{artist} - %{title} (%{bold}%{plays}%{bold})"
+ artist = item.elements["artist"].text
+ when :weeklyartistchart, :topartists
+ format = "%{artist} (%{bold}%{plays}%{bold})"
+ artist = item.elements["name"].text
+ when :toptracks, :topalbums
+ format = "%{artist} - %{title} (%{bold}%{plays}%{bold})"
+ artist = item.elements["artist/name"].text
+ end
+
+ _(format) % {
+ :artist => artist,
+ :title => item.elements["name"].text,
+ :plays => item.elements["playcount"].text,
+ :bold => Bold
+ }
+ end
+ if items.empty?
+ m.reply _("%{user} hasn't played anything in this period of time") % { :user => user }
+ else
+ m.reply items[0, num].join(", ")
+ end
+ end
+ end
+
+ def resolve_username(m, name)
+ name = m.sourcenick if name.nil?
+ @registry[name] or name