Class: Robut::Plugin::Rdio

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.domainObject

The domain associated with token. Defaults to localhost.



37
38
39
# File 'lib/robut-rdio.rb', line 37

def domain
  @domain
end

.hostObject

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

.keyObject

Your Rdio API Key



14
15
16
# File 'lib/robut-rdio.rb', line 14

def key
  @key
end

.portObject

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

.secretObject

Your Rdio API app secret



17
18
19
# File 'lib/robut-rdio.rb', line 17

def secret
  @secret
end

.tokenObject

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_serverObject

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

Parameters:

  • request (String, Array)

    that is being evaluated as a command request

Returns:

  • (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{|message| reply(message, :room)}
  Server.state_callback ||= lambda{|message| reply("/me #{message}", :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, message)
  
  establish_server_callbacks!
  
  words = words(message)
  
  if sent_to_me?(message)

    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?(message)

      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.

Examples:

Requesting multiple tracks


"play 1"
"play 1 2"
"play 1,2"
"play 1-3"
"play 1, 2 4-6"
"play all"

Parameters:

  • track_request (Array, String)

    the play request that is going to be parsed for available tracks.

Returns:

  • (Array)

    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

Parameters:

  • request (String, Array)

    that is being evaluated as a play request

Returns:

  • (Boolean)


86
87
88
# File 'lib/robut-rdio.rb', line 86

def play?(request)
  Array(request).join(' ') =~ play_results_regex
end

#play_results_regexBoolean

Parameters:

  • request (String, Array)

    that is being evaluated as a play request

Returns:

  • (Boolean)


78
79
80
# File 'lib/robut-rdio.rb', line 78

def play_results_regex
  /^(?:play)?\s?(?:result)?\s?((?:\d[\s,-]*)+|all)$/
end

#resultsObject

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

Parameters:

  • request (String, Array)

    that is being evaluated as a search request

Returns:

  • (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

Parameters:

  • request (String, Array)

    that is being evaluated as a search and playback request

Returns:

  • (Boolean)


137
138
139
# File 'lib/robut-rdio.rb', line 137

def search_and_play?(request)
  Array(request).join(' ') =~ /^play\b[^\b]+/
end

#search_regexRegex

Returns that is used to match searches for their parameters.

Returns:

  • (Regex)

    that is used to match searches for their parameters

See Also:



120
121
122
# File 'lib/robut-rdio.rb', line 120

def search_regex
  /(find|do you have(\sany)?)\s?(.+[^?])\??/
end

#skip_album?(message) ⇒ Boolean

Parameters:

  • request (String, Array)

    that is being evaluated as a skip album request

Returns:

  • (Boolean)


153
154
155
# File 'lib/robut-rdio.rb', line 153

def skip_album?(message)
  message =~ /(next|skip) album/
end

#usageObject

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