]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/spotify.rb
spotify: handle errors
[user/henk/code/ruby/rbot.git] / 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 help(plugin, topic)
14     _("spotify plugin - usage: spotify <spotify>, spotify artist <artist>, spotify album <album>")
15   end
16
17   def search(m, params)
18     method = params[:method] || 'track'
19     begin
20       result = Spotify.search(method, params[:query].to_s)
21     rescue
22       m.reply "problems connecting to Spotify"
23     end
24
25     if result.nil?
26       m.reply "no results"
27       return
28     end
29
30     case method
31     when 'track'
32       reply = _("%{b}%{artist}%{b} – %{track}") % {
33         :artist => result.artist.name,
34         :track => result.name,
35         :b => Bold
36       }
37
38       if result.album.released
39         reply << _(" [%{u}%{album}%{u}, %{released}]") % {
40           :released => result.album.released,
41           :album => result.album.name,
42           :u => Underline
43         }
44       else
45         reply << _(" [%{u}%{album}%{u}]") % { :album => result.album.name, :u => Underline }
46       end
47
48       reply << _(" — %{url}") % { :url => result.url }
49     when 'artist'
50       reply = _("%{b}%{artist}%{b} — %{url}") % {
51         :b => Bold,
52         :artist => result.name,
53         :url => result.url
54       }
55     when 'album'
56       reply = _("%{b}%{artist}%{b} – %{u}%{album}%{u} — %{url}") % {
57         :b => Bold,
58         :u => Underline,
59         :artist => result.artist.name,
60         :album => result.name,
61         :url => result.url
62       }
63     end
64
65     m.reply reply
66   end
67 end
68
69 plugin = SpotifyPlugin.new
70 plugin.map 'spotify [:method] *query', :action => :search, :requirements => { :method => /track|artist|album/ }