+ require "shorturl"
+ maps_uri = ShortURL.shorten(CGI.escape(maps_uri))
+ rescue LoadError => e
+ error e
+ end
+
+ reply << _(" and %{maps} for maps") % { :maps => maps_uri, :b => Bold }
+ m.reply reply
+ 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
+
+ def lastfm(m, params)
+ action = case params[:action]
+ when "neighbors" then "neighbours"
+ when "recentracks", "recent" then "recenttracks"
+ when "loved" then "lovedtracks"
+ when /^weekly(track|album|artist)s$/
+ "weekly#{$1}chart"
+ when "events"
+ find_events(m, params)
+ return
+ else
+ params[:action]
+ end.to_sym
+
+ if action == :shouts
+ num = params[:num] || @bot.config['lastfm.default_shouts']
+ num = num.to_i.clip(1, @bot.config['lastfm.max_shouts'])
+ else
+ num = params[:num] || @bot.config['lastfm.default_user_data']
+ num = num.to_i.clip(1, @bot.config['lastfm.max_user_data'])
+ end
+
+ user = resolve_username(m, params[:user])
+ 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
+
+ begin
+ res = @bot.httputil.get_response(uri)
+ raise _("no response body") unless res.body
+ rescue Exception => e
+ m.reply _("I had problems accessing last.fm: %{e}") % {:e => e.message}
+ 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
+
+ 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}")
+ reply << seemore
+ end
+ m.reply reply % {
+ :user => user,
+ :total => friends.size,
+ :friends => friends.shuffle[0, num].join(", "),
+ :uri => "http://www.last.fm/user/#{CGI.escape user}/friends"
+ }
+ 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}")
+ reply << seemore
+ end
+ m.reply reply % {
+ :user => user,
+ :total => loved.size,
+ :tracks => loved_prep.join(", "),
+ :uri => "http://www.last.fm/user/#{CGI.escape user}/library/loved"
+ }
+ 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}")
+ reply << seemore
+ end
+ m.reply reply % {
+ :user => user,
+ :nbrs => nbrs.shuffle[0, num].join(", "),
+ :uri => "http://www.last.fm/user/#{CGI.escape user}/neighbours"
+ }
+ 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 => tracks_prep.join(", ") }
+ 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(", ")