[core] unicode plugin that sets server encoding
[rbot] / data / rbot / plugins / spotify.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: spotify plugin for rbot
5 #
6 # Author:: Raine Virta <raine.virta@gmail.com>
7 #
8 # Copyright:: (C) 2009 Raine Virta
9 #
10 # License:: GPL v2
11
12 class SpotifyPlugin < Plugin
13   def initialize
14     super
15
16     unless Object.const_defined?('Spotify')
17       raise 'Spotify module not found (lib_spotify plugin probably not enabled)'
18     end
19   end
20
21   def help(plugin, topic)
22     _("spotify plugin - usage: spotify <spotify>, spotify artist <artist>, spotify album <album>")
23   end
24
25   def search(m, params)
26     method = params[:method] || 'track'
27     begin
28       result = Spotify.search(method, params[:query].to_s)
29     rescue
30       m.reply "problems connecting to Spotify"
31     end
32
33     if result.nil?
34       m.reply "no results"
35       return
36     end
37
38     case method
39     when 'track'
40       reply = _("%{b}%{artist}%{b} – %{track}") % {
41         :artist => result.artist.name,
42         :track => result.name,
43         :b => Bold
44       }
45
46       if result.album.released
47         reply << _(" [%{u}%{album}%{u}, %{released}]") % {
48           :released => result.album.released,
49           :album => result.album.name,
50           :u => Underline
51         }
52       else
53         reply << _(" [%{u}%{album}%{u}]") % { :album => result.album.name, :u => Underline }
54       end
55
56       reply << _(" — %{url}") % { :url => result.url }
57     when 'artist'
58       reply = _("%{b}%{artist}%{b} — %{url}") % {
59         :b => Bold,
60         :artist => result.name,
61         :url => result.url
62       }
63     when 'album'
64       reply = _("%{b}%{artist}%{b} – %{u}%{album}%{u} — %{url}") % {
65         :b => Bold,
66         :u => Underline,
67         :artist => result.artist.name,
68         :album => result.name,
69         :url => result.url
70       }
71     end
72
73     m.reply reply
74   end
75 end
76
77 plugin = SpotifyPlugin.new
78 plugin.map 'spotify [:method] *query', :action => :search, :requirements => { :method => /track|artist|album/ }