Skip to content
Browse files

Working test case.

New method: getPlaybackToken
  • Loading branch information...
1 parent 131e416 commit c2d5c44e5a49b64daf7798c1385f03b32be8606e @dawnerd committed Jun 25, 2011
View
14 rdio.js
@@ -27,7 +27,7 @@ module.exports = function(config, oauth) {
config.rdio_api = 'http://api.rdio.com/1/';
//setup oauth
- var oa = new OAuth(
+ var oa = new oauth(
config.rdio_oauth_request,
config.rdio_oauth_access,
config.rdio_api_key,
@@ -42,6 +42,18 @@ module.exports = function(config, oauth) {
getAccessToken: function(auth_token, auth_token_secret, oauth_verifier, callback) {
oa.getOAuthAccessToken(auth_token, auth_token_secret, oauth_verifier, callback);
},
+ getPlaybackToken: function(auth_token, auth_token_secret, host, callback) {
+ this.api(
+ auth_token,
+ auth_token_secret,
+ {
+ method: 'getPlaybackToken',
+ domain: encodeURIComponent(host)
+ },
+ callback
+ );
+ },
+
api: function(auth_token, auth_token_secret, data, callback) {
oa.post(
config.rdio_api,
View
23 test/.gitignore
@@ -0,0 +1,23 @@
+#CrunchTune Specific
+#config/config.js
+
+
+.*.sw[a-z]
+*.un~
+Session.vim
+
+.DS_Store
+Icon?
+
+# Thumbnails
+._*
+
+# Files that might appear on external disk
+.Spotlight-V100
+.Trashes
+
+# Windows image file caches
+Thumbs.db
+
+# Folder config file
+Desktop.ini
View
25 test/README.md
@@ -0,0 +1,25 @@
+# CrunchTune
+
+Have problems deciding what music to listen to? Of course you do. CrunchTune takes away the need to actually think and make a decision. Just load the app and relax.
+
+## Current Features
+* Randomly plays popular music
+
+## Future Features
+* Genre Selection
+* Similar to artist
+
+## The Requirements
+* Node 0.4.7
+* npm
+* Express (npm install express) https://github.com/visionmedia/express
+* Jade (npm install jade) http://jade-lang.com/
+* Rdio API key.
+
+This project is curringly running on Joyents no.de service and is being developed on Mac OSX. Other platforms should work but have not been tested.
+
+## Running your own copy
+1. Install node, npm, express
+2. Clone repo into whatever directory you want. It's really up to you here, seriously.
+3. Create a copy of config/config-template.js and change settings as needed.
+4. node ./server.js
View
77 test/app.js
@@ -1,77 +0,0 @@
-
-/**
- * Module dependencies.
- */
-
-var express = require('express');
-
-var app = module.exports = express.createServer();
-
-// Configuration
-
-app.configure(function(){
- app.set('views', __dirname + '/views');
- app.set('view engine', 'jade');
- app.use(express.bodyParser());
- app.use(express.methodOverride());
- app.use(app.router);
- app.use(express.static(__dirname + '/public'));
-});
-
-app.configure('development', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
-});
-
-app.configure('production', function(){
- app.use(express.errorHandler());
-});
-
-app.use(express.logger());
-app.use(express.bodyParser());
-app.use(express.cookieParser());
-app.use(express.session({
- secret: "98yKGKgkdrg94tnkfdh"
-}));
-
-app.dynamicHelpers({
- session: function(req, res){
- return req.session;
- }
-});
-
-//load rdio
-var rdio = require('rdio')({
- rdio_api_key: '8nfujzs8m5td6wy5cquap8vb',
- rdio_api_shared: 'FajApfbYva',
- callback_url: 'http://localhost:3000/oauth/callback'
-})
-
-// Routes
-app.get('/', function(req, res){
- res.render('index', {
- title: 'node-rdio'
- });
-});
-
-app.get('/oauth/login', function(req, res, params) {
- if (!req.session.oauth_access_token) {
- oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results) {
- if (error) {
- console.log('error');
- console.log(error);
- } else {
- // store the tokens in the session
- req.session.oauth_token = oauth_token;
- req.session.oauth_token_secret = oauth_token_secret;
-
- // redirect the user to authorize the token
- res.redirect(ct.config.rdio_oauth_auth + oauth_token);
- }
- });
- } else {
- res.redirect("/");
- }
-});
-
-app.listen(3000);
-console.log("To test node-rdio, open a browser to http://localhost:%d", app.address().port);
View
11 test/app/ct.js
@@ -0,0 +1,11 @@
+/*
+ CrunchTune
+ omnomnom - tasty tunes
+
+ File: cct.js
+ Author: Troy Whiteley
+ License: CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+*/
+
+var config = require('../config/config.js').config;
+exports.config = config;
View
22 test/config/config-template.js
@@ -0,0 +1,22 @@
+/*
+ CrunchTune
+ omnomnom - tasty tunes
+
+ File: config-template.js
+ Author: Troy Whiteley
+ License: CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+
+ v.0.0.3
+*/
+exports.config = {
+ //Server settings
+ port: 3000,
+ host: 'localhost',
+
+ //Rdio settings
+ rdio_oauth_request: 'http://api.rdio.com/oauth/request_token',
+ rdio_oauth_access: 'http://api.rdio.com/oauth/access_token',
+ rdio_oauth_auth: 'https://www.rdio.com/oauth/authorize?oauth_token=',
+ rdio_api_key: '',
+ rdio_api_shared: '',
+};
View
22 test/config/config.js
@@ -0,0 +1,22 @@
+/*
+ CrunchTune
+ omnomnom - tasty tunes
+
+ File: config-template.js
+ Author: Troy Whiteley
+ License: CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+*/
+exports.config = {
+ //Server settings
+ port: 3000,
+ host: 'http://localhost',
+
+ //Rdio settings
+ rdio_oauth_request: 'http://api.rdio.com/oauth/request_token',
+ rdio_oauth_access: 'http://api.rdio.com/oauth/access_token',
+ rdio_oauth_auth: 'https://www.rdio.com/oauth/authorize?oauth_token=',
+ rdio_api: 'http://api.rdio.com/1/',
+ rdio_api_key: '8nfujzs8m5td6wy5cquap8vb',
+ rdio_api_shared: 'FajApfbYva',
+ songs_to_grab: 10,
+};
View
8 test/package.json
@@ -1,8 +0,0 @@
-{
- "name": "node-rdio-test"
- , "version": "0.0.1"
- , "private": true
- , "dependencies": {
- "jade": ">= 0.0.1"
- }
-}
View
8 test/public/stylesheets/style.css
@@ -1,8 +0,0 @@
-body {
- padding: 50px;
- font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
-}
-
-a {
- color: #00B7FF;
-}
View
BIN test/public/ui/images/controls-sprite.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN test/public/ui/images/ct-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN test/public/ui/images/ct-pause.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN test/public/ui/images/ct-play.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
142 test/public/ui/js/narwhal.js
@@ -0,0 +1,142 @@
+var last_tagline = 0,
+ last_songs = [],
+ update_freq = 5000,
+ last_state = null,
+ tag_lines = [
+ "Om nom nom",
+ "Crunchy",
+ "Tastes like chicken",
+ "Narwhal approved",
+ "I'll be back",
+ "Wooooooooo",
+ "Get your music on!",
+ "Soylent Green is people!",
+ "Don't give me that look",
+ "Bond. James Bond",
+ "Gulp",
+ "Buuuuuurp",
+ "Say hello to my little friend!",
+ "Yummy",
+ "MMMMMMMMMMMMMM",
+ "Toto, I've a feeling we're not in Kansas anymore",
+ "Better than chicken tacos",
+ "Like ham on a biscuit",
+ "Shaken, not stirred",
+ "Yo dawg...",
+ "You've just been erased!",
+ "Hungry yet?",
+ "You can't handle the truth!",
+ "Taco Bell should be paying us for this",
+ "I like turtles",
+ "These beats are tasty",
+ "May the Force be with you",
+ "I like you, John",
+ "99% of this site's development was creating witty messages",
+ "Put that cookie down, NOW!",
+ "You're hot stuff",
+ ">:3",
+ "^:) <-- Narwhal",
+ "You're awesome",
+ "Awesomesause",
+ "Open the pod bay doors, HAL",
+ "Badger badger badger badger...",
+ "Houston, we have a problem",
+ "Mushroom mushroom!!!",
+ "This is SPARTA!",
+ "I'm gonna make him an offer he can't refuse",
+ "90% Javascript. 10% Pepsi",
+ "Probably in beta",
+ "NO U!",
+ "Hi Reddit",
+ "Just like old times",
+ "Now with more awesome",
+ "You know you can do other things while listening?",
+ "I should wash my car",
+ "Noooooooooooooooo",
+ "And my axe!",
+ "FFFFFFFUUUUUUUUUUUU"
+ ];
+
+function update_tagline(){
+ do {
+ var rand_n = Math.floor(Math.random()*tag_lines.length);
+ } while(last_tagline==rand_n);
+ last_tagline = rand_n;
+ var tag_line = tag_lines[rand_n];
+ $('#tagline').fadeOut('',function(){$('#tagline').html(tag_line).fadeIn()});
+}
+var tagTime = setInterval("update_tagline()",update_freq);
+
+$("#ctrl-play").hide();
+$("#ctrl-pause").hide();
+
+
+/*
+ Rdio stuff
+*/
+function pickRandomSong() {
+ do {
+ var rand_s = Math.floor(Math.random()*songs.length);
+ } while(!$.inArray(rand_s, last_songs));
+ if(last_songs.length == (songs.length-1)) last_songs = [];
+ last_songs.push(rand_s);
+ return songs[rand_s];
+}
+function changeSong() {
+ //start the song
+ last_state = null;
+ var song = pickRandomSong();
+ player.rdio_play(song.key);
+
+ $("#song-name").html('<a href="'+song.shortUrl+'">'+song.name+'</a>');
+ $("#band-name").text(song.albumArtist);
+ $("#album-image").html($('<img/>').attr("src", song.icon));
+}
+var player;
+var rdioListener = {
+ ready: function() {
+ player = document.getElementById("CTplayer");
+ changeSong();
+ },
+ playStateChanged: function(state) {
+ if(state == 0 || state == 2 || state == 4) {
+ $("#ctrl-play").show();
+ $("#ctrl-pause").hide();
+ } else {
+ $("#ctrl-play").hide();
+ $("#ctrl-pause").show();
+ }
+
+ if(state == 2 && last_state !== null) {
+ changeSong();
+ }
+
+ last_state = state;
+ }
+};
+var flashvars = {
+ playbackToken: playbackToken,
+ domain: encodeURIComponent(document.domain),
+ listener: 'rdioListener'
+};
+var params = {
+ 'allowScriptAccess': 'always'
+};
+swfobject.embedSWF("http://www.rdio.com/api/swf/", "CTplayer", "1", "1", "9.0.0","", flashvars, params);
+
+//listeners
+$("#ctrl-play").click(function(e){
+ e.preventDefault();
+ player.rdio_play();
+ return false;
+});
+$("#ctrl-pause").click(function(e){
+ e.preventDefault();
+ player.rdio_pause();
+ return false;
+});
+$("#next-song").click(function(e){
+ e.preventDefault();
+ changeSong();
+ return false;
+});
View
24 test/public/ui/layout.css
@@ -0,0 +1,24 @@
+html,body{padding:0;margin:0;}html{font-size:1em;}body{font-size:100%;}a img,:link img,:visited img{border:0;}
+p{padding:0;margin:0;font-family:Tahoma,Geneva,sans-serif;color:#515151;margin-bottom:10px;}
+a{color:#00b4ff;text-decoration:none;outline:none;}
+a:hover{text-decoration:underline;}
+.hidden{display:none;}
+.right{float:right;}
+#mc{width:524px;margin:50px auto 0;}
+#f{position:absolute;bottom:0;width:100%;background:#eee;border-top:1px solid #e5e5e5;}
+#fc{margin:30px auto;width:524px;}
+#fc p{color:#9b9b9b;font-size:.75em;}
+#fc p.right{text-align:right;}
+#tagline{text-align:right;font-size:.75em;}
+.uh-oh{margin-top:75px;}
+
+#controls{margin-top:75px;height:52px;overflow:hidden;width:100%;position:relative;}
+#ctrl-play,#ctrl-pause{text-indent:-9999px;height:52px;width:51px;cursor:pointer;background:url('/ui/images/controls-sprite.png') no-repeat;}
+#ctrl-play{background-position:0 0;}
+#ctrl-pause{background-position:0 -52px;}
+.band-info{position:absolute;left:60px;top:5px;width:370px;}
+.band-info p{width:370px;overflow:hidden;white-space:nowrap;display:block;}
+#album-image { float:left; margin-right: 10px; }
+ #album-image img { height: 42px; }
+.band-more{position:absolute;right:0;top:5px;}
+.band-more p{text-align:right; font-size: 10px;}
View
72 test/routes/site.js
@@ -0,0 +1,72 @@
+/*
+ CrunchTune
+ omnomnom - tasty tunes
+
+ File: site.js
+ Author: Troy Whiteley
+ License: CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+*/
+
+var ct = require('../app/ct'),
+ url = require('url');
+
+module.exports = function(app, rdio){
+ app.get('/', function(req, res){
+
+ rdio.api(
+ req.session.oauth_access_token,
+ req.session.oauth_access_token_secret,
+ {
+ method: 'getTopCharts',
+ type: 'Track',
+ count: 10
+ }, function(err, data, response) {
+ if(err) throw new Error(err);
+ var songs = JSON.parse(data);
+
+ rdio.getPlaybackToken(
+ req.session.oauth_access_token,
+ req.session.oauth_access_token_secret,
+ ct.config.host+":"+ct.config.port+,
+ function(err, data, response) {
+ if(err) throw new Error(err);
+ res.render('index', {
+ playbackToken: JSON.parse(data).result,
+ songs: songs.result
+ });
+ }
+ );
+ }
+ );
+ });
+
+ app.get ('/oauth/login', function(req, res, params) {
+ if(!req.session.oauth_access_token) {
+ rdio.getRequestToken(function(error, oauth_token, oauth_token_secret, results){
+ if(error) {
+ throw new Error(error);
+ } else {
+ // store the tokens in the session
+ req.session.oauth_token = oauth_token;
+ req.session.oauth_token_secret = oauth_token_secret;
+
+ // redirect the user to authorize the token
+ res.redirect(ct.config.rdio_oauth_auth+oauth_token);
+ }
+ });
+ } else {
+ res.redirect("/");
+ }
+ });
+
+ app.get ('/oauth/callback', function(req, res, params) {
+ var parsedUrl = url.parse(req.url, true);
+ rdio.getAccessToken(parsedUrl.query.oauth_token, req.session.oauth_token_secret, parsedUrl.query.oauth_verifier,
+ function(error, oauth_access_token, oauth_access_token_secret, results) {
+ req.session.oauth_access_token = oauth_access_token;
+ req.session.oauth_access_token_secret = oauth_access_token_secret;
+ res.redirect("/");
+ }
+ )
+ });
+};
View
60 test/server.js
@@ -0,0 +1,60 @@
+/*
+ CrunchTune
+ omnomnom - tasty tunes
+
+ File: server.js
+ Author: Troy Whiteley
+ License: CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+*/
+
+var express = require('express'),
+ ct = require('./app/ct.js'),
+ OAuth = require('oauth').OAuth,
+ querystring = require('querystring');
+
+var app = express.createServer();
+
+app.set('views', __dirname + '/views');
+app.set('view engine', 'jade');
+
+//oauth setup
+app.use(express.logger());
+app.use(express.bodyParser());
+app.use(express.cookieParser());
+app.use(express.session({
+ secret: "98yKGKgkdrg94tnkfdh"
+}));
+
+app.dynamicHelpers({
+ base: function(){
+ return '/' == app.route ? '' : app.route;
+ },
+ session: function(req, res){
+ return req.session;
+ }
+});
+
+// Middleware
+app.configure(function(){
+ app.use(express.logger('\x1b[33m:method\x1b[0m \x1b[32m:url\x1b[0m :response-time'));
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+//setup rdio
+var rdio = require('rdio')({
+ rdio_api_key: ct.config.rdio_api_key,
+ rdio_api_shared: ct.config.rdio_api_shared,
+ callback_url: ct.config.host+":"+ct.config.port+"/oauth/callback"
+});
+
+// Routes
+require('./routes/site')(app, rdio);
+
+if (!module.parent) {
+ app.listen(ct.config.port);
+ console.log('Server started on port '+ct.config.port);
+}
View
21 test/test/app.test.js
@@ -1,21 +0,0 @@
-
-// Run $ expresso
-
-/**
- * Module dependencies.
- */
-
-var app = require('../app')
- , assert = require('assert');
-
-
-module.exports = {
- 'GET /': function(){
- assert.response(app,
- { url: '/' },
- { status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }},
- function(res){
- assert.includes(res.body, '<title>Express</title>');
- });
- }
-};
View
54 test/views/index.jade
@@ -1,4 +1,50 @@
-- if(!session.oauth_access_token)
- a(href: '/oauth/login') Click here to login
-- else
-
+#mc
+ img(src='/ui/images/ct-logo.png', alt='CrunchTune')
+ p#tagline Omnomnom
+
+ noscript
+ .uh-oh
+ p Oh no, what are you doing?! You can't use this site without JavaScript!
+
+ - if(!session.oauth_access_token)
+ p
+ a(href='/oauth/login') Subscribe to Rdio? Login
+
+ #controls
+ #ctrl-play play
+ #ctrl-pause pause
+ .band-info
+ p
+ span#album-image
+ span#song-name
+ br
+ | By:
+ span#band-name
+ .band-more
+ p
+ a#next-song('data-action'='new',href="#") Next Song
+
+ - if(!session.oauth_access_token)
+ .uh-oh
+ p <a href="http://click.linksynergy.com/fs-bin/click?id=FkOvMBnEzh8&offerid=221756.12&type=4&subid=0"><IMG alt="Unlimited Music, Everywhere. Try Rdio for Free." border="0" src="http://rdio.com/media/images/affiliate/generic_type3_468x60.jpg"></a><IMG border="0" width="1" height="1" src="http://ad.linksynergy.com/fs-bin/show?id=FkOvMBnEzh8&bids=221756.12&type=4&subid=0">
+
+ #CTplayer
+
+#f
+ #fc
+ p.right Powered by:
+ br
+ a(href='http://rdio.com') Rdio
+ | &amp; pure awesome
+ p CrunchTune is copyright
+ a(href='http://troywhiteley.com') Troy Whiteley
+ | but is 100% open-source.
+ br
+ a(href='https://github.com/dawnerd/CrunchTune') Fork your copy of CrunchTune over at GitHub.
+
+script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js')
+script(src='https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js')
+script
+ var playbackToken = '#{playbackToken}';
+ var songs = !{JSON.stringify(songs)};
+script(src='/ui/js/narwhal.js')
View
10 test/views/layout.jade
@@ -1,6 +1,6 @@
-!!!
+!!! 5
html
- head
- title= title
- link(rel='stylesheet', href='/stylesheets/style.css')
- body!= body
+ head
+ title CrunchTune - omnomnom, tasty.
+ link(rel='stylesheet', href=base+'/ui/layout.css')
+ body!= body

0 comments on commit c2d5c44

Please sign in to comment.
Something went wrong with that request. Please try again.