Class: Robut::Plugin::Rdio
- Inherits:
-
Object
- Object
- Robut::Plugin::Rdio
- Includes:
- Robut::Plugin
- Defined in:
- lib/robut-rdio.rb,
lib/server/server.rb
Overview
A plugin that hooks into Rdio, allowing you to queue songs from HipChat. key
and secret
must be set before we can deal with any Rdio commands. Additionally, you must call start_server
in your Chatfile to start the Rdio web server.
Defined Under Namespace
Classes: Server
Class Attribute Summary collapse
-
.domain ⇒ Object
The domain associated with
token
. -
.host ⇒ Object
The host the Rdio web player will run on.
-
.key ⇒ Object
Your Rdio API Key.
-
.port ⇒ Object
The port the Rdio web player will run on.
-
.secret ⇒ Object
Your Rdio API app secret.
-
.token ⇒ Object
The playback token for
domain
.
Class Method Summary collapse
-
.start_server ⇒ Object
Starts a Robut::Plugin::Rdio::Server server for communicating with the actual Rdio web player.
Instance Method Summary collapse
- #command?(request) ⇒ Boolean
-
#establish_server_callbacks! ⇒ Object
Because an instance of this plugin is not created until the Robut client has recieved at least one message.
-
#handle(time, sender_nick, message) ⇒ Object
Queues songs into the Rdio web player.
-
#parse_tracks_to_play(track_request) ⇒ Array
Track numbers that were identified.
- #play?(request) ⇒ Boolean
- #play_results_regex ⇒ Boolean
-
#results ⇒ Object
As the plugin is initialized each time a request is made, the plugin maintains the state of the results from the last search request to ensure that it will be available when someone makes another request.
- #search?(request) ⇒ Boolean
- #search_and_play?(request) ⇒ Boolean
-
#search_regex ⇒ Regex
That is used to match searches for their parameters.
- #skip_album?(message) ⇒ Boolean
-
#usage ⇒ Object
Returns a description of how to use this plugin.
Class Attribute Details
.domain ⇒ Object
The domain associated with token
. Defaults to localhost.
37 38 39 |
# File 'lib/robut-rdio.rb', line 37 def domain @domain end |
.host ⇒ Object
The host the Rdio web player will run on. Defaults to localhost.
23 24 25 |
# File 'lib/robut-rdio.rb', line 23 def host @host end |
.key ⇒ Object
Your Rdio API Key
14 15 16 |
# File 'lib/robut-rdio.rb', line 14 def key @key end |
.port ⇒ Object
The port the Rdio web player will run on. Defaults to 4567.
20 21 22 |
# File 'lib/robut-rdio.rb', line 20 def port @port end |
.secret ⇒ Object
Your Rdio API app secret
17 18 19 |
# File 'lib/robut-rdio.rb', line 17 def secret @secret end |
.token ⇒ Object
The playback token for domain
. If you’re accessing Rdio on localhost, you shouldn’t need to change this. Otherwise, download the rdio-python plugin:
https://github.com/rdio/rdio-python
and generate a new token for your domain:
./rdio-call --consumer-key=YOUR_CONSUMER_KEY --consumer-secret=YOUR_CONSUMER_SECRET getPlaybackToken domain=YOUR_DOMAIN
34 35 36 |
# File 'lib/robut-rdio.rb', line 34 def token @token end |
Class Method Details
.start_server ⇒ Object
Starts a Robut::Plugin::Rdio::Server server for communicating with the actual Rdio web player. You must call this in the Chatfile if you plan on using this gem.
43 44 45 46 47 |
# File 'lib/robut-rdio.rb', line 43 def self.start_server @server = Thread.new { Server.run! :host => (host || "localhost"), :port => (port || 4567) } Server.token = self.token || "GAlNi78J_____zlyYWs5ZG02N2pkaHlhcWsyOWJtYjkyN2xvY2FsaG9zdEbwl7EHvbylWSWFWYMZwfc=" Server.domain = self.domain || "localhost" end |
Instance Method Details
#command?(request) ⇒ Boolean
145 146 147 |
# File 'lib/robut-rdio.rb', line 145 def command?(request) Array(request).join(' ') =~ /^(?:play|(?:un)?pause|next|restart|back|clear)$/ end |
#establish_server_callbacks! ⇒ Object
Because an instance of this plugin is not created until the Robut client has recieved at least one message. The server callbacks need to be created during the #handle request. This allows for the server to communicate back through the Robut communication channel that it receives the messages.
55 56 57 58 |
# File 'lib/robut-rdio.rb', line 55 def establish_server_callbacks! Server.reply_callback ||= lambda{|| reply(, :room)} Server.state_callback ||= lambda{|| reply("/me #{}", :room)} end |
#handle(time, sender_nick, message) ⇒ Object
Queues songs into the Rdio web player. @nick play search query will queue the first search result matching ‘search query’ into the web player. It can be an artist, album, or song.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/robut-rdio.rb', line 160 def handle(time, sender_nick, ) establish_server_callbacks! words = words() if sent_to_me?() if play?(words) play_result *parse_tracks_to_play(words) elsif search_and_play?(words) search_and_play_criteria = words[1..-1].join(" ") unless search_and_play_result search_and_play_criteria reply("I couldn't find '#{search_and_play_criteria}' on Rdio.") end elsif search?(words) find words.join(' ')[search_regex,-1] elsif skip_album?() run_command("next_album") else command?(words) run_command(words.join("_")) end end end |
#parse_tracks_to_play(track_request) ⇒ Array
Returns track numbers that were identified.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/robut-rdio.rb', line 105 def parse_tracks_to_play(track_request) if Array(track_request).join(' ') =~ /play all/ [ 'all' ] else Array(track_request).join(' ')[play_results_regex,-1].to_s.split(/(?:\s|,\s?)/).map do |track| tracks = track.split("-") (tracks.first.to_i..tracks.last.to_i).to_a end.flatten end end |
#play?(request) ⇒ Boolean
86 87 88 |
# File 'lib/robut-rdio.rb', line 86 def play?(request) Array(request).join(' ') =~ play_results_regex end |
#play_results_regex ⇒ Boolean
78 79 80 |
# File 'lib/robut-rdio.rb', line 78 def play_results_regex /^(?:play)?\s?(?:result)?\s?((?:\d[\s,-]*)+|all)$/ end |
#results ⇒ Object
As the plugin is initialized each time a request is made, the plugin maintains the state of the results from the last search request to ensure that it will be available when someone makes another request.
203 204 205 |
# File 'lib/robut-rdio.rb', line 203 def results @@results end |
#search?(request) ⇒ Boolean
128 129 130 |
# File 'lib/robut-rdio.rb', line 128 def search?(request) Array(request).join(' ') =~ search_regex end |
#search_and_play?(request) ⇒ Boolean
137 138 139 |
# File 'lib/robut-rdio.rb', line 137 def search_and_play?(request) Array(request).join(' ') =~ /^play\b[^\b]+/ end |
#search_regex ⇒ Regex
Returns that is used to match searches for their parameters.
120 121 122 |
# File 'lib/robut-rdio.rb', line 120 def search_regex /(find|do you have(\sany)?)\s?(.+[^?])\??/ end |
#skip_album?(message) ⇒ Boolean
153 154 155 |
# File 'lib/robut-rdio.rb', line 153 def skip_album?() =~ /(next|skip) album/ end |
#usage ⇒ Object
Returns a description of how to use this plugin
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/robut-rdio.rb', line 61 def usage [ "#{at_nick} play <song> - queues <song> for playing", "#{at_nick} play album <album> - queues <album> for playing", "#{at_nick} play track <track> - queues <track> for playing", "#{at_nick} play/unpause - unpauses the track that is currently playing", "#{at_nick} next - move to the next track", "#{at_nick} next|skip album - skip all tracks in the current album group", "#{at_nick} restart - restart the current track" ] end |