lastfm plugin: initial work on artist/track/album retrieval (only artist so far)
[rbot] / data / rbot / plugins / lastfm.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: lastfm plugin for rbot
5 #
6 # Author:: Jeremy Voorhis
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 #
9 # Copyright:: (C) 2005 Jeremy Voorhis
10 # Copyright:: (C) 2007 Giuseppe Bilotta
11 #
12 # License:: GPL v2
13
14 require 'open-uri'
15
16 class LastFmPlugin < Plugin
17
18   LASTFM = "http://www.last.fm"
19
20   def help(plugin, topic="")
21     case topic.intern
22     when :artist, :group
23       "lastfm artist <name> => show information on artist/group <name> from last.fm"
24     when :song, :track
25       "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
26     when :album
27       "lastfm album <name> => show information on album <name> from last.fm"
28     else
29       "lastfm <function> <user> => lastfm data for <user> on last.fm where <function> in [recenttracks, topartists, topalbums, toptracks, tags, friends, neighbors]. other topics: artist, group, song, track, album"
30     end
31   end
32
33   def lastfm(m, params)
34     action = params[:action].intern
35     action = :neighbours if action == :neighbors
36     what = params[:what]
37     case action
38     when :artist, :group
39       artist = what.to_s
40       begin
41         esc = URI.escape(artist)
42         page = @bot.httputil.get "#{LASTFM}/music/#{esc}"
43         if page
44           if page.match(/<h1 class="h1artist"><a href="([^"]+)">(.*?)<\/a><\/h1>/)
45             url = LASTFM + $1
46             title = $2
47           else
48             raise "No URL/Title found for #{artist}"
49           end
50
51           wiki = page.match(/<div class="wikiAbstract">(.*?)<\/div>/m)[1].ircify_html
52           m.reply "%s : %s\n%s" % [title, url, wiki]
53         else
54           m.reply "no data found on #{artist}"
55         end
56       rescue
57         m.reply "I had problems looking for #{artist}"
58         debug page
59         return
60       end
61     when :song, :track
62       m.reply "not implemented yet, sorry"
63     when :album
64       m.reply "not implemented yet, sorry"
65     else
66       return usage unless what.length == 1
67       user = what.first
68       begin
69         data = open("http://ws.audioscrobbler.com/1.0/user/#{user}/#{action}.txt")
70         m.reply "#{action} for #{user}:"
71         m.reply data.to_a[0..3].map{|l| l.split(',',2)[-1].chomp}.join(", ")
72       rescue
73         m.reply "could not find #{action} for #{user} (is #{user} a user?)"
74       end
75     end
76   end
77 end
78
79 plugin = LastFmPlugin.new
80 plugin.map 'lastfm :action *what'