Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 0.0.7-pre2

Signed-off-by: Diwank Singh Tomer <diwank.singh@gmail.com>
  • Loading branch information...
commit b98ecda997fc60c7a71c2872eb8591973b3b6d35 1 parent 6bc3ff6
@creatorrr authored
View
4 package.json
@@ -1,6 +1,6 @@
{
"name": "hubot-rdio",
- "version": "0.0.6",
+ "version": "0.0.7",
"description": "Rdio controller for hubot.",
"main": "lib/index.js",
"scripts": {
@@ -25,5 +25,5 @@
"license": "BSD",
"readmeFilename": "README.md",
"devDependencies": {},
- "gitHead": "6864ea08fe2c779a9736e41593ddef09f61e201b"
+ "gitHead": "773de77dc4ef7351b2078e2ddd38fe77c648739c"
}
View
3  src/globals.coffee
@@ -17,8 +17,9 @@ misc =
DOMAIN: process.env.DOMAIN or process.env.HEROKU_URL
CALLBACK: 'auth'
-# Globals
+# Export globals
module.exports = globals = extend {}, rdio, misc
+# Check for valid Rdio credentials.
unless globals.RDIO_CONSUMER and globals.RDIO_SECRET
throw new Error 'Invalid rdio credentials'
View
19 src/index.coffee
@@ -26,23 +26,6 @@ module.exports = (robot) ->
robot.respond /pause( music){0,1}/i, listeners.pause
robot.router.get '/', routes.home
+ robot.router.get '/login', routes.login
robot.router.get "/#{ CALLBACK }", routes.auth
robot.router.get '/player', routes.player
-
-# rdio init
-# get accesstoken and store init
-
-# rdio play me x
-# get song id
-# send it to client
-
-# /
-# serve page with playback_token
-# listen to socket.io
-# - on 'connect'
-# * on 'play', data -> play
-# * on 'pause' -> pause
-
-# /auth
-# get verifier and exchange for access playback_token
-# store it.
View
19 src/listeners.coffee
@@ -6,15 +6,20 @@ Rdio = require 'node-rdio'
{RDIO_CONSUMER, RDIO_SECRET, DOMAIN, CALLBACK} = require './globals'
# Helpers
+# Returns true if obj is a string.
isString = (obj) ->
Object::toString.call(obj) is '[object String]'
+# Returns a random integer between min and max.
random = (min, max) -> min + Math.floor Math.random()*(max - min + 1)
-getRandom = (arr) -> arr[random 0, arr.length]
+# Get random element from arr.
+getRandom = (arr) -> arr[random 0, arr.length-1]
+# Return capitalized string.
capitalize = (str) -> (str.charAt 0).toUpperCase() + str[1..]
+# Capture robot instance in a closure and return interface.
module.exports = listeners = (robot) ->
getRdio = ->
accessToken = robot.brain.get 'RdioAccessToken'
@@ -81,17 +86,17 @@ module.exports = listeners = (robot) ->
msg.send "Playing track #{ track.name }"
play: (msg) ->
- mode = capitalize msg.match[0].toLowerCase()
- query = msg.match[1]
+ mode = capitalize msg.match[1].toLowerCase()
+ query = msg.match[2]
rdio = getRdio()
- rdio.call 'search', {type: mode, query: query}, (error, data) ->
+ rdio.call 'search', { types: mode, query: query }, (error, {result}) ->
if error
robot.logger.debug "Error: #{ error }"
return msg.send "Error: #{ error }"
- msg.send inspect data
+ track = result.results[0]
+ robot.emit 'player:send', 'play', track
- # track = getRandom result
- # robot.emit 'player:send', 'play', track
+ msg.send "Playing track #{ track.name }"
}
View
32 src/pages.coffee
@@ -1,18 +1,6 @@
{compile} = require 'coffeecup'
-helpers =
- layout: (partial) ->
- doctype 5
- html ->
- head ->
- script src: '/socket.io/socket.io.js'
- script src: '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'
- script src: '//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'
-
- body ->
- partial()
-
-pages =
+templates =
home: ->
layout =>
h1 @title
@@ -67,5 +55,19 @@ pages =
$('#play_key').val track.key
$('#play').click()
-# Precompile pages and export.
-module.exports[name] = compile page, hardcode: helpers for name, page of pages
+# Helpers
+helpers =
+ layout: (partial) ->
+ doctype 5
+ html ->
+ head ->
+ script src: '/socket.io/socket.io.js'
+ script src: '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'
+ script src: '//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'
+
+ body ->
+ partial()
+
+# Precompile templates and export.
+for name, template of templates
+ module.exports[name] = compile template, hardcode: helpers
View
23 src/routes.coffee
@@ -6,7 +6,7 @@ Rdio = require 'node-rdio'
pages = require './pages'
# Load globals
-{RDIO_CONSUMER, RDIO_SECRET, DOMAIN} = require './globals'
+{RDIO_CONSUMER, RDIO_SECRET, DOMAIN, CALLBACK} = require './globals'
module.exports = routes = (robot) ->
home: (req, res) ->
@@ -16,6 +16,27 @@ module.exports = routes = (robot) ->
res.end pages.home
title: 'Pataku?'
+ login: (req, res) ->
+ rdio = new Rdio [
+ RDIO_CONSUMER
+ RDIO_SECRET
+ ]
+
+ rdio.beginAuthentication DOMAIN+CALLBACK, (error, authUrl) ->
+ if error
+ robot.logger.debug error
+ return msg.send "Error: #{ error }"
+
+ requestToken = rdio.token[0]
+ requestSecret = rdio.token[1]
+
+ robot.brain
+ .set('RdioRequestToken', requestToken)
+ .set("RdioRequestSecret-#{requestToken}", requestSecret)
+ .save()
+
+ res.redirect authUrl
+
auth: (req, res) ->
res.writeHead 200,
'Content-Type': 'text/html'
View
15 src/socket-controller.coffee
@@ -1,3 +1,11 @@
+# Helpers
+# Extend dest object with sources.
+extend = (dest, sources...) ->
+ for src in sources
+ dest[key] = value for own key, value of src
+
+ dest
+
module.exports = (robot) ->
new ->
@sockets = []
@@ -12,10 +20,3 @@ module.exports = (robot) ->
# Return instance
this
-
-# Helpers
-extend = (dest, sources...) ->
- for src in sources
- dest[key] = value for own key, value of src
-
- dest
Please sign in to comment.
Something went wrong with that request. Please try again.