Skip to content

Commit

Permalink
Version 0.0.7-pre2
Browse files Browse the repository at this point in the history
Signed-off-by: Diwank Singh Tomer <diwank.singh@gmail.com>
  • Loading branch information
creatorrr committed Apr 1, 2013
1 parent 6bc3ff6 commit b98ecda
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 51 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
{ {
"name": "hubot-rdio", "name": "hubot-rdio",
"version": "0.0.6", "version": "0.0.7",
"description": "Rdio controller for hubot.", "description": "Rdio controller for hubot.",
"main": "lib/index.js", "main": "lib/index.js",
"scripts": { "scripts": {
Expand All @@ -25,5 +25,5 @@
"license": "BSD", "license": "BSD",
"readmeFilename": "README.md", "readmeFilename": "README.md",
"devDependencies": {}, "devDependencies": {},
"gitHead": "6864ea08fe2c779a9736e41593ddef09f61e201b" "gitHead": "773de77dc4ef7351b2078e2ddd38fe77c648739c"
} }
3 changes: 2 additions & 1 deletion src/globals.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ misc =
DOMAIN: process.env.DOMAIN or process.env.HEROKU_URL DOMAIN: process.env.DOMAIN or process.env.HEROKU_URL
CALLBACK: 'auth' CALLBACK: 'auth'


# Globals # Export globals
module.exports = globals = extend {}, rdio, misc module.exports = globals = extend {}, rdio, misc


# Check for valid Rdio credentials.
unless globals.RDIO_CONSUMER and globals.RDIO_SECRET unless globals.RDIO_CONSUMER and globals.RDIO_SECRET
throw new Error 'Invalid rdio credentials' throw new Error 'Invalid rdio credentials'
19 changes: 1 addition & 18 deletions src/index.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,23 +26,6 @@ module.exports = (robot) ->
robot.respond /pause( music){0,1}/i, listeners.pause robot.respond /pause( music){0,1}/i, listeners.pause


robot.router.get '/', routes.home robot.router.get '/', routes.home
robot.router.get '/login', routes.login
robot.router.get "/#{ CALLBACK }", routes.auth robot.router.get "/#{ CALLBACK }", routes.auth
robot.router.get '/player', routes.player 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.
19 changes: 12 additions & 7 deletions src/listeners.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ Rdio = require 'node-rdio'
{RDIO_CONSUMER, RDIO_SECRET, DOMAIN, CALLBACK} = require './globals' {RDIO_CONSUMER, RDIO_SECRET, DOMAIN, CALLBACK} = require './globals'


# Helpers # Helpers
# Returns true if obj is a string.
isString = (obj) -> isString = (obj) ->
Object::toString.call(obj) is '[object String]' 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) 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..] capitalize = (str) -> (str.charAt 0).toUpperCase() + str[1..]


# Capture robot instance in a closure and return interface.
module.exports = listeners = (robot) -> module.exports = listeners = (robot) ->
getRdio = -> getRdio = ->
accessToken = robot.brain.get 'RdioAccessToken' accessToken = robot.brain.get 'RdioAccessToken'
Expand Down Expand Up @@ -81,17 +86,17 @@ module.exports = listeners = (robot) ->
msg.send "Playing track #{ track.name }" msg.send "Playing track #{ track.name }"


play: (msg) -> play: (msg) ->
mode = capitalize msg.match[0].toLowerCase() mode = capitalize msg.match[1].toLowerCase()
query = msg.match[1] query = msg.match[2]


rdio = getRdio() rdio = getRdio()
rdio.call 'search', {type: mode, query: query}, (error, data) -> rdio.call 'search', { types: mode, query: query }, (error, {result}) ->
if error if error
robot.logger.debug "Error: #{ error }" robot.logger.debug "Error: #{ error }"
return msg.send "Error: #{ error }" return msg.send "Error: #{ error }"


msg.send inspect data track = result.results[0]
robot.emit 'player:send', 'play', track


# track = getRandom result msg.send "Playing track #{ track.name }"
# robot.emit 'player:send', 'play', track
} }
32 changes: 17 additions & 15 deletions src/pages.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,18 +1,6 @@
{compile} = require 'coffeecup' {compile} = require 'coffeecup'


helpers = templates =
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 =
home: -> home: ->
layout => layout =>
h1 @title h1 @title
Expand Down Expand Up @@ -67,5 +55,19 @@ pages =
$('#play_key').val track.key $('#play_key').val track.key
$('#play').click() $('#play').click()


# Precompile pages and export. # Helpers
module.exports[name] = compile page, hardcode: helpers for name, page of pages 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
23 changes: 22 additions & 1 deletion src/routes.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Rdio = require 'node-rdio'
pages = require './pages' pages = require './pages'


# Load globals # Load globals
{RDIO_CONSUMER, RDIO_SECRET, DOMAIN} = require './globals' {RDIO_CONSUMER, RDIO_SECRET, DOMAIN, CALLBACK} = require './globals'


module.exports = routes = (robot) -> module.exports = routes = (robot) ->
home: (req, res) -> home: (req, res) ->
Expand All @@ -16,6 +16,27 @@ module.exports = routes = (robot) ->
res.end pages.home res.end pages.home
title: 'Pataku?' 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) -> auth: (req, res) ->
res.writeHead 200, res.writeHead 200,
'Content-Type': 'text/html' 'Content-Type': 'text/html'
Expand Down
15 changes: 8 additions & 7 deletions src/socket-controller.coffee
Original file line number Original file line Diff line number Diff line change
@@ -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) -> module.exports = (robot) ->
new -> new ->
@sockets = [] @sockets = []
Expand All @@ -12,10 +20,3 @@ module.exports = (robot) ->


# Return instance # Return instance
this this

# Helpers
extend = (dest, sources...) ->
for src in sources
dest[key] = value for own key, value of src

dest

0 comments on commit b98ecda

Please sign in to comment.