4 # :title: lastfm plugin for rbot
6 # Author:: Jeremy Voorhis
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 # Copyright:: (C) 2005 Jeremy Voorhis
10 # Copyright:: (C) 2007 Giuseppe Bilotta
17 attr_accessor :url, :date, :artist, :location, :attendance
18 def initialize(url, date, artist, location, attendance)
23 @attendance = attendance
27 class LastFmPlugin < Plugin
29 LASTFM = "http://www.last.fm"
31 def help(plugin, topic="")
34 "lastfm events in <location> => show information on events in or near <location> from last.fm"
36 "lastfm artist <name> => show information on artist/group <name> from last.fm"
38 "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
40 "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
42 "lastfm <function> <user> => lastfm data for <user> on last.fm where <function> in [recenttracks, topartists, topalbums, toptracks, tags, friends, neighbors]. other topics: events, artist, group, song, track, album"
47 action = params[:action].intern
48 action = :neighbours if action == :neighbors
54 location = what.to_s.sub(/^in\s+/,'')
55 raise "wrong location #{location}" if location.empty?
56 esc = URI.escape(location)
57 page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
60 disp_events = Array.new
63 # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
64 pre_events = page.scan(/<tr class="vevent\s+\w+\s+\S+?-(\d\d)-(\d\d)-(\d\d\d\d)\s*">.*?<a class="url summary" href="(\/event\/\d+)">(.*?)<\/a>.*?<a href="(\/venue\/\d+)">(.*?)<\/a>.*?<td class="attendance">(.*?)<\/td>\s+<\/tr>/m)
65 debug pre_events.inspect
66 pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
67 date = Time.utc(year.to_i, month.to_i, day.to_i)
68 url = LASTFM + url_who
69 artist = who.ircify_html
70 loc = where.ircify_html
71 attendance = how_many.ircify_html
72 events << LastFmEvent.new(url, date, artist, loc, attendance)
76 events[0..2].each { |event|
77 disp_events << "%s %s @ %s (%s) %s" % [event.date.strftime("%a %b, %d %Y"), event.artist, event.location, event.attendance, event.url]
79 m.reply disp_events.join(' | ')
81 m.reply "No events found in #{location}"
85 m.reply "I had problems looking for events #{what.to_s}"
87 debug e.backtrace.join("\n")
88 debug page[0...10*1024] if page
95 esc = URI.escape(artist)
96 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
98 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
100 title = $2.ircify_html
102 raise "No URL/Title found for #{artist}"
105 wiki = "This #{action} doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
106 if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
107 wiki = $1.ircify_html
110 m.reply "%s : %s\n%s" % [title, url, wiki]
112 m.reply "no data found on #{artist}"
115 rescue Exception => e
116 m.reply "I had problems looking for #{artist}"
118 debug e.backtrace.join("\n")
119 debug page[0...10*1024] if page
123 m.reply "not implemented yet, sorry"
125 m.reply "not implemented yet, sorry"
127 return usage(m) unless what.length == 1
130 data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
131 m.reply "#{action} for #{user}:"
132 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
134 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
140 plugin = LastFmPlugin.new
141 plugin.map 'lastfm :action *what'