+ uri = "#{APIURL}method=user.get#{action}&user=#{CGI.escape user}"
+
+ if period = params[:period]
+ period_uri = (period.last == "year" ? "12month" : period.first + "month")
+ uri << "&period=#{period_uri}"
+ end
+
+ res = @bot.httputil.get_response(uri)
+ unless res.body
+ m.reply _("I had problems accessing last.fm")
+ return
+ end
+ 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
+
+ case action
+ when :friends
+ friends = doc.root.get_elements("friends/user").map do |u|
+ u.elements["name"].text
+ end
+
+ unless friends.empty?
+ m.reply _("%{user} has %{total} friends; %{friends}") %
+ { :user => user, :total => friends.size, :friends => friends.join(", ") }
+ else
+ m.reply _("%{user} has no friends :(") % { :user => user }
+ end
+ 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..4].to_enum(:each_with_index).collect { |e,i| (i % 2).zero? ? Underline+e+Underline : e }
+
+ unless loved.empty?
+ m.reply _("%{user} has loved %{total} tracks, including %{tracks} %{uri}") % {
+ :user => user,
+ :total => loved.size,
+ :tracks => loved_prep.join(", "),
+ :uri => "http://www.last.fm/user/#{CGI.escape user}/library/loved"
+ }
+ else
+ m.reply _("%{user} has not loved any tracks") % { :user => user }
+ end
+ when :neighbours
+ nbrs = doc.root.get_elements("neighbours/user").map do |u|
+ u.elements["name"].text
+ end
+
+ unless nbrs.empty?
+ m.reply _("%{user}'s musical neighbors include %{nbrs} %{uri}") % {
+ :user => user,
+ :nbrs => nbrs.shuffle[0..9].join(", "),
+ :uri => "http://www.last.fm/user/#{CGI.escape user}/neighbours"
+ }
+ else
+ m.reply _("no one seems to share %{user}'s musical taste") % { :user => user }
+ end
+ when :recenttracks
+ tracks = doc.root.get_elements("recenttracks/track").map do |track|
+ [track.elements["artist"].text, track.elements["name"].text].join(" - ")
+ end
+ tracks_prep = tracks.to_enum(:each_with_index).collect { |e,i| (i % 2).zero? ? Underline+e+Underline : e }
+
+ unless tracks.empty?
+ m.reply _("%{user} has recently played %{tracks}") %
+ { :user => user, :tracks => tracks_prep.join(", ") }
+ else
+ m.reply _("%{user} hasn't played anything recently") % { :user => user }
+ end
+ when :shouts
+ shouts = doc.root.get_elements("shouts/shout")
+ unless shouts.empty?
+ shouts[0..4].each do |shout|
+ m.reply _("<%{author}> %{body}") % {
+ :body => shout.elements["body"].text,
+ :author => shout.elements["author"].text,
+ }
+ end
+ else
+ m.reply _("there are no shouts for %{user}") % { :user => user }
+ 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
+ m.reply items[0..9].join(", ")