+ error $!
+ m.reply _("last.fm parsing failed")
+ end
+ else
+ m.reply _("track %{a} not found") % {:a => track}
+ end
+ end
+
+ def find_venue(m, params)
+ venue = params[:venue].to_s
+ venues = search_venue_by(:name => venue, :limit => 1)
+ venue = venues.last
+
+ if venues.empty?
+ m.reply "sorry, can't find such venue"
+ return
+ end
+
+ reply = _("%{b}%{name}%{b}, %{street}, %{u}%{city}%{u}, %{country}, see %{url} for more info") % {
+ :u => Underline, :b => Bold, :name => venue.name, :city => venue.city, :street => venue.street,
+ :country => venue.country, :url => venue.url
+ }
+
+ if venue.street && venue.city
+ maps_uri = "http://maps.google.com/maps?q=#{venue.street},+#{venue.city}"
+ maps_uri << ",+#{venue.postal}" if venue.postal
+ elsif venue.lat && venue.long
+ maps_uri = "http://maps.google.com/maps?q=#{venue.lat},+#{venue.long}"
+ else
+ m.reply reply
+ return
+ end
+
+ maps_uri << "+(#{venue.name.gsub(" ", "%A0")})"
+
+ begin
+ 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