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
67 m.reply "No events found in #{location}, sorry"
69 pre_events.each { |day, month, year, url_who, who, url_where, where, how_many|
70 date = Time.utc(year.to_i, month.to_i, day.to_i)
71 url = LASTFM + url_who
72 artist = who.ircify_html
73 loc = where.ircify_html
74 attendance = how_many.ircify_html
75 events << LastFmEvent.new(url, date, artist, loc, attendance)
77 # debug events.inspect
79 events[0..2].each { |event|
80 disp_events << "%s %s @ %s (%s) %s" % [event.date.strftime("%a %b, %d %Y"), event.artist, event.location, event.attendance, event.url]
82 m.reply disp_events.join(' | ')
84 m.reply "No events found in #{location}"
88 m.reply "I had problems looking for events #{what.to_s}"
90 debug e.backtrace.join("\n")
91 debug page[0...10*1024] if page
98 esc = URI.escape(artist)
99 page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
101 if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
103 title = $2.ircify_html
105 raise "No URL/Title found for #{artist}"
108 wiki = "This #{action} doesn't have a description yet. You can help by writing it: #{url}/+wiki?action=edit"
109 if page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)
110 wiki = $1.ircify_html
113 m.reply "%s : %s\n%s" % [title, url, wiki]
115 m.reply "no data found on #{artist}"
118 rescue Exception => e
119 m.reply "I had problems looking for #{artist}"
121 debug e.backtrace.join("\n")
122 debug page[0...10*1024] if page
126 m.reply "not implemented yet, sorry"
128 m.reply "not implemented yet, sorry"
130 return usage(m) unless what.length == 1
133 data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
134 m.reply "#{action} for #{user}:"
135 m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
137 m.reply "could not find #{action} for #{user} (is #{user} a user?)"
143 plugin = LastFmPlugin.new
144 plugin.map 'lastfm :action *what'