diff --git a/lib/services/rdio/index.js b/lib/services/rdio/index.js index 606b0fa..9ab70f6 100644 --- a/lib/services/rdio/index.js +++ b/lib/services/rdio/index.js @@ -1,181 +1,164 @@ -"use strict"; +'use strict'; var parse = require('url').parse; var Promise = require('bluebird'); -module.exports.id = "rdio"; +module.exports.id = 'rdio'; -if (!process.env.RDIO_API_KEY || !process.env.RDIO_API_SHARED) { - console.warn("RDIO_API_KEY or RDIO_API_SHARED environment variables not found, deactivating Rdio."); +if (!process.env.RDIO_CLIENT_ID || !process.env.RDIO_CLIENT_SECRET || !process.env.RDIO_REFRESH_TOKEN) { + console.warn('RDIO_CLIENT_ID, RDIO_REFRESH_TOKEN or RDIO_CLIENT_SECRET environment variables not found, deactivating Rdio.'); } else { -var rdio = require('rdio')({ - rdio_api_key: process.env.RDIO_API_KEY, - rdio_api_shared: process.env.RDIO_API_SHARED, +var Rdio = require('rdio'); +var rdio = new Rdio({ + clientId: process.env.RDIO_CLIENT_ID, + clientSecret: process.env.RDIO_CLIENT_SECRET, + refreshToken: process.env.RDIO_REFRESH_TOKEN }); var rdio = Promise.promisifyAll(rdio); module.exports.match = require('./url').match; -module.exports.lookupId = function(id) { - return rdio.apiAsync("", "", {method: 'getObjectFromShortCode', short_code: id}).then(function(results) { - if (!JSON.parse(results[0]).result) { - var error = new Error("Not Found"); - error.status = 404; - throw error; - } - var result = JSON.parse(results[0]).result; - var parsed = parse(result.shortUrl) - var id = parsed.path.replace("/x/", "").replace("/", ""); - var type = result.album ? "track" : "album"; +module.exports.lookupId = function*(id) { + yield rdio.loginAsync(); + var result = yield rdio.callAsync('getObjectFromShortCode', {'short_code': id}); + var parsedShortUrl = parse(result.shortUrl); + var rid = parsedShortUrl.path.replace('/x/', '').replace('/', ''); + var type = result.album ? 'track' : 'album'; + var item = { + service: 'rdio', + type: type, + id: rid, + name: result.name, + streamUrl: result.shortUrl, + purchaseUrl: null, + artwork: { + small: result.icon.replace('square-200', 'square-250').replace('http:', 'https:'), + large: result.icon.replace('square-200', 'square-600').replace('http:', 'https:') + }, + artist: { + name: result.artist + } + }; + if (type === 'track') { + item.album = { + name: result.album + }; + } + return item; +}; + +module.exports.parseUrl = function *(url) { + var parsedUrl = parse(url); + + var method; + var args; + + if (parsedUrl.host === 'rd.io') { + method = 'getObjectFromShortCode'; + args = {'short_code': parsedUrl.path.replace('/x/', '').replace('/', '')}; + } else if (parsedUrl.host.match(/rdio\.com$/)) { + method = 'getObjectFromUrl'; + args = {url: parsedUrl.path}; + } else { + var error = new Error('Not Found'); + error.status = 404; + throw error; + } + + yield rdio.loginAsync(); + var result = yield rdio.callAsync(method, args); + var parsedShortUrl = parse(result.shortUrl); + var id = parsedShortUrl.path.replace('/x/', '').replace('/', ''); + var type = result.album ? 'track' : 'album'; + var item = { + service: 'rdio', + type: type, + id: id, + name: result.name, + streamUrl: result.shortUrl, + purchaseUrl: null, + artwork: { + small: result.icon.replace('square-200', 'square-250').replace('http:', 'https:'), + large: result.icon.replace('square-200', 'square-600').replace('http:', 'https:') + }, + artist: { + name: result.artist + } + }; + if (type === 'track') { + item.album = { + name: result.album + }; + } + return item; +}; + +module.exports.search = function *(data) { + var query, albumClean; + var type = data.type; + + if (type === 'album') { + query = data.artist.name + ' ' + data.name; + albumClean = data.name.match(/([^\(\[]+)/)[0]; + } else if (type === 'track') { + query = data.artist.name + ' ' + data.album.name + ' ' + data.name; + try { + albumClean = data.album.name.match(/([^\(\[]+)/)[0]; + } catch(e) { + albumClean = ''; + } + } + + yield rdio.loginAsync(); + var response = yield rdio.callAsync('search', {query: query, types: type}); + var result = response.results.filter(function(item) { + if (type === 'album' && item.name.match(/([^\(\[]+)/)[0] === albumClean) { + return item; + } else if (type === 'track' && (item.album.match(/([^\(\[]+)/)[0] === albumClean || !albumClean)) { + return item; + } + }).shift(); + + if (!result) { + var matches = albumClean.match(/^[^\(\[]+/); + if (matches && matches[0] && matches[0] !== albumClean) { + var cleanedData = JSON.parse(JSON.stringify(data)); + if (type === 'album') { + cleanedData.name = matches[0].trim(); + } else if (type === 'track') { + cleanedData.album.name = matches[0].trim(); + } + return module.exports.search(cleanedData); + } else { + return {service: 'rdio'}; + } + } else { + var parsedShortUrl = parse(result.shortUrl); + var id = parsedShortUrl.path.replace('/x/', '').replace('/', ''); var item = { - service: "rdio", + service: 'rdio', type: type, id: id, name: result.name, streamUrl: result.shortUrl, purchaseUrl: null, artwork: { - small: result.icon.replace("square-200", "square-250").replace("http:", "https:"), - large: result.icon.replace("square-200", "square-600").replace("http:", "https:") + small: result.icon.replace('square-200', 'square-250').replace('http:', 'https:'), + large: result.icon.replace('square-200', 'square-600').replace('http:', 'https:') }, artist: { name: result.artist } }; - if (type == "track") { + if (type === 'track') { item.album = { name: result.album }; } return item; - }); -}; - -module.exports.parseUrl = function(url) { - var parsed = parse(url); - - var data; - - if (parsed.host == "rd.io") { - data = { - method: 'getObjectFromShortCode', - short_code: parsed.path.replace("/x/", "").replace("/", ""), - }; - } else if (parsed.host.match(/rdio\.com$/)) { - data = { - method: 'getObjectFromUrl', - url: parsed.path, - }; - } else { - var error = new Error("Not Found"); - error.status = 404; - throw error; } - - return rdio.apiAsync("", "", data).then(function(results) { - var results = JSON.parse(results[0]); - var result = results.result; - if (!result || results.status != "ok") { - var error = new Error("Not Found"); - error.status = 404; - throw error; - } else { - var parsed = parse(result.shortUrl) - var id = parsed.path.replace("/x/", "").replace("/", ""); - var type = result.album ? "track" : "album"; - var item = { - service: "rdio", - type: type, - id: id, - name: result.name, - streamUrl: result.shortUrl, - purchaseUrl: null, - artwork: { - small: result.icon.replace("square-200", "square-250").replace("http:", "https:"), - large: result.icon.replace("square-200", "square-600").replace("http:", "https:") - }, - artist: { - name: result.artist - } - }; - if (type == "track") { - item.album = { - name: result.album - }; - } - return item; - } - }); }; -module.exports.search = function(data) { - var query, albumClean; - var type = data.type; - - if (type == "album") { - query = data.artist.name + " " + data.name; - albumClean = data.name.match(/([^\(\[]+)/)[0]; - } else if (type == "track") { - query = data.artist.name + " " + data.album.name + " " + data.name; - try { - albumClean = data.album.name.match(/([^\(\[]+)/)[0]; - } catch(e) { - albumClean = ""; - } - } - - return rdio.apiAsync("", "", {query: query, method: 'search', types: type}).then(function(results) { - var results = JSON.parse(results[0]).result.results; - - var result = results.filter(function(result) { - if (type == "album" && result.name.match(/([^\(\[]+)/)[0] == albumClean) { - return result; - } else if (type == "track" && (result.album.match(/([^\(\[]+)/)[0] == albumClean || !albumClean)) { - return result; - } - }).shift(); - - if (!result) { - var matches = albumClean.match(/^[^\(\[]+/); - if (matches && matches[0] && matches[0] != albumClean) { - var cleanedData = JSON.parse(JSON.stringify(data)); - if (type == "album") { - cleanedData.name = matches[0].trim(); - } else if (type == "track") { - cleanedData.album.name = matches[0].trim(); - } - return module.exports.search(cleanedData); - } else { - return {service: "rdio"}; - } - } else { - var parsed = parse(result.shortUrl) - var id = parsed.path.replace("/x/", "").replace("/", ""); - var item = { - service: "rdio", - type: type, - id: id, - name: result.name, - streamUrl: result.shortUrl, - purchaseUrl: null, - artwork: { - small: result.icon.replace("square-200", "square-250").replace("http:", "https:"), - large: result.icon.replace("square-200", "square-600").replace("http:", "https:") - }, - artist: { - name: result.artist - } - }; - if (type == "track") { - item.album = { - name: result.album - }; - } - return item; - } - }); -}; - -} \ No newline at end of file +} diff --git a/package.json b/package.json index 43b5e2b..e61c32c 100644 --- a/package.json +++ b/package.json @@ -3,16 +3,16 @@ "version": "0.0.0", "private": true, "scripts": { + "build": "babel -d public/views views", + "postinstall": "jspm install", "start": "node -r 'babel/register' app.js", "test": "mocha --require co-mocha --compilers js:babel/register test/**/*.js --timeout=10000", - "build": "babel -d public/views views", "watch": "parallelshell \"npm run watch-js\" \"npm run watch-server\"", - "watch-server": "nodemon -x \"node -r 'babel/register'\" -e js,jsx -i public/ app.js", "watch-js": "babel --modules system -wd public/views views", - "postinstall": "jspm install" + "watch-server": "nodemon -x \"node -r 'babel/register'\" -e js,jsx -i public/ app.js" }, "engines": { - "iojs": "2.3.0" + "iojs": "~2.3.0" }, "dependencies": { "babel": "^5.5.8", @@ -21,7 +21,7 @@ "browserify": "^10.1.3", "co": "^4.5.4", "debug": "^2.1.1", - "jspm": "^0.16.0-beta", + "jspm": "^0.16.0-beta.3", "koa": "^0.21.0", "koa-bodyparser": "^2.0.0", "koa-compress": "^1.0.8", @@ -33,7 +33,7 @@ "mongodb-promisified": "^1.0.2", "node-uuid": "^1.4.2", "playmusic": "^2.0.0", - "rdio": "^1.5.2", + "rdio": "^2.0.0", "react": "^0.13.3", "react-google-analytics": "^0.2.0", "react-router": "^0.13.3", @@ -44,12 +44,12 @@ }, "devDependencies": { "co-mocha": "^1.1.0", - "eslint": "^0.22.1", + "eslint": "^0.24.0", "eslint-plugin-react": "^2.5.1", "mocha": "^2.1.0", "nodemon": "^1.3.7", "parallelshell": "^1.1.1", - "should": "^6.0.3", + "should": "^7.0.1", "spdy": "^1.32.0" }, "jspm": {