(.*?)<\/a><\/h1>/)
- url = LASTFM + $1
- title = $2.ircify_html
- else
- raise "No URL/Title found for #{artist}"
- end
-
- wiki = "This artist doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
- if page.match(/(.*?)<\/div>/m)
- wiki = $1.ircify_html
- end
-
- m.reply "%s : %s\n%s" % [title, url, wiki], :overlong => :truncate
- else
- m.reply "no data found on #{artist}"
- return
- end
- rescue Exception => e
- m.reply "I had problems looking for #{artist}"
- error e.inspect
- debug e.backtrace.join("\n")
- debug page[0...10*1024] if page
+ 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 find_track(m, params)
- m.reply "not implemented yet, sorry"
+ 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)
- m.reply "not implemented yet, sorry"
+ 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 #{nick} is #{user} at last.fm"
+ 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] then
+ if params[:who]
nick = params[:who].to_s
else
nick = m.sourcenick
end
- if @registry.has_key?( nick ) then
+ 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 #{nick} is at last.fm perhaps you need to: lastfm set "
+ m.reply _("Sorry, I don't know who %{n} is at last.fm perhaps you need to: lastfm set ") % {:n => nick}
end
end
@@ -270,7 +347,7 @@ class LastFmPlugin < Plugin
user = nil
if params[:user] then
user = params[:user].to_s
- elsif @registry.has_key? ( m.sourcenick ) then
+ 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."
@@ -288,19 +365,20 @@ class LastFmPlugin < Plugin
end
plugin = LastFmPlugin.new
-plugin.map 'lastfm [:num] event[s] in *location', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
-plugin.map 'lastfm [:num] event[s] by *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
-plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_event, :requirements => { :num => /\d+/ }, :thread => true
-plugin.map 'lastfm artist *who', :action => :find_artist, :thread => true
-plugin.map 'lastfm group *who', :action => :find_artist, :thread => true
-plugin.map 'lastfm now *who', :action => :now_playing, :thread => true
+plugin.map 'lastfm [:num] event[s] in *location', :action => :find_events, :requirements => { :num => /\d+/ }, :thread => true
+plugin.map 'lastfm [:num] event[s] by *who', :action => :find_events, :requirements => { :num => /\d+/ }, :thread => true
+plugin.map 'lastfm [:num] event[s] [for] *who', :action => :find_events, :requirements => { :num => /\d+/ }, :thread => true
+plugin.map 'lastfm now :who', :action => :now_playing, :thread => true
plugin.map 'lastfm now', :action => :now_playing, :thread => true
-plugin.map 'lastfm track *dunno', :action => :find_track
-plugin.map 'lastfm song *dunno', :action => :find_track
-plugin.map 'lastfm album *dunno', :action => :find_album
-plugin.map 'lastfm set *who', :action => :set_user, :thread => true
-plugin.map 'lastfm who *who', :action => :get_user, :thread => true
+plugin.map 'np :who', :action => :now_playing, :thread => true
+plugin.map 'lastfm artist *artist', :action => :find_artist, :thread => true
+plugin.map 'lastfm album *album [by *artist]', :action => :find_album
+plugin.map 'lastfm set nick :who', :action => :set_user, :thread => true
+plugin.map 'lastfm set verb :present :past', :action => :set_verb, :thread => true
+plugin.map 'lastfm who :who', :action => :get_user, :thread => true
plugin.map 'lastfm who', :action => :get_user, :thread => true
-plugin.map 'lastfm :action *user', :thread => true
-plugin.map 'lastfm :action', :thread => true
+plugin.map 'lastfm compare :user1 :user2', :action => :tasteometer, :thread => true
+#plugin.map 'lastfm :action :user', :thread => true
+#plugin.map 'lastfm :action', :thread => true
+plugin.map 'np', :action => :now_playing, :thread => true
plugin.map 'lastfm', :action => :now_playing, :thread => true