Permalink
Browse files

Added routes for getting playback token.

Signed-off-by: Diwank Singh Tomer <diwank.singh@gmail.com>
  • Loading branch information...
1 parent 6239409 commit cd33029ea833095db6ee80fc06a9673255a6880e @creatorrr committed Mar 29, 2013
Showing with 54 additions and 13 deletions.
  1. +3 −1 src/globals.coffee
  2. +5 −8 src/index.coffee
  3. +2 −2 src/listeners.coffee
  4. +3 −0 src/pages.coffee
  5. +41 −2 src/routes.coffee
View
@@ -13,7 +13,9 @@ extend = (dest, sources...) ->
# Get config
rdio = pick process.env, 'RDIO_CONSUMER', 'RDIO_SECRET', 'HEROKU_URL'
-misc = CALLBACK: 'auth'
+misc =
+ DOMAIN: process.env.DOMAIN or process.env.HEROKU_URL
+ CALLBACK: 'auth'
# Globals
module.exports = globals = extend {}, rdio, misc
View
@@ -13,23 +13,20 @@ module.exports = (robot) ->
robot.io = sockets robot
# Load modules
- pages = require './pages'
routes = (require './routes') robot
listeners = (require './listeners') robot
# Initialize this thing.
robot.respond /init rdio/i, listeners.init
robot.respond /test rdio/i, listeners.test
+ # robot.respond /play (song|artist|album) (["'\w: \-_]+).*$/i, listeners.play
+ # robot.respond /play whatever/i, listeners.playWhatever
+ # robot.respond /pause( music){0,1}/i, listeners.pause
+
robot.router.get '/', routes.home
robot.router.get "/#{ CALLBACK }", routes.auth
-
- # client.post "#{ RDIO_API_ENDPOINT }",
- # accessToken,
- # accessSecret,
- # 'method=getOfflineTracks',
- # 'application/x-www-form-urlencoded',
- # (error, data) -> msg.send error, data
+ robot.router.get '/player', routes.player
# rdio init
# get accesstoken and store init
View
@@ -2,7 +2,7 @@
Rdio = require 'node-rdio'
# Load globals
-{RDIO_CONSUMER, RDIO_SECRET, HEROKU_URL, CALLBACK} = require './globals'
+{RDIO_CONSUMER, RDIO_SECRET, DOMAIN, CALLBACK} = require './globals'
module.exports = listeners = (robot) ->
init: (msg) ->
@@ -11,7 +11,7 @@ module.exports = listeners = (robot) ->
RDIO_SECRET
]
- rdio.beginAuthentication HEROKU_URL+CALLBACK, (error, authUrl) ->
+ rdio.beginAuthentication DOMAIN+CALLBACK, (error, authUrl) ->
if error
robot.logger.debug error
return msg.send "Error: #{ error }"
View
@@ -29,5 +29,8 @@ pages =
setTimeout redirect, 3000
+ player: ->
+ div -> @playbackToken
+
# Precompile pages and export.
module.exports[name] = compile page for name, page of pages
View
@@ -1,8 +1,11 @@
# External libraries
Rdio = require 'node-rdio'
+# Modules
+pages = require './pages'
+
# Load globals
-{RDIO_CONSUMER, RDIO_SECRET} = require './globals'
+{RDIO_CONSUMER, RDIO_SECRET, DOMAIN} = require './globals'
module.exports = routes = (robot) ->
home: (req, res) ->
@@ -54,6 +57,42 @@ module.exports = routes = (robot) ->
.set("RdioAccessSecret-#{accessToken}", accessSecret)
.save()
+ res.end pages.redirect
+ message: "Yay! Your access token is #{ accessToken }"
+ redirect: '/'
+
+ player: (req, res) ->
+ res.writeHead 200,
+ 'Content-Type': 'text/html'
+
+ accessToken = robot.brain.get 'RdioAccessSecret'
+ accessSecret = robot.brain.get "RdioAccessSecret-#{accessToken}"
+
+ unless accessToken and accessSecret
+ return res.end pages.redirect
+ message: 'Please authorize rdio first.'
+ redirect: '/'
+
+ rdio = new Rdio [
+ RDIO_CONSUMER
+ RDIO_SECRET
+ ], [
+ accessToken
+ accessSecret
+ ]
+
+ rdio.call 'currentUser', (error) ->
+ if error
res.end pages.redirect
- message: "Yay! Your access token is #{ accessToken }"
+ message: 'Please authorize rdio first.'
redirect: '/'
+
+ else
+ rdio.call 'getPlaybackToken', {domain: DOMAIN}, (error, data) ->
+ if error
+ res.end pages.error
+ message: "Error: #{ error }"
+
+ else
+ res.end pages.player
+ playbackToken: data

0 comments on commit cd33029

Please sign in to comment.