diff --git a/.gitignore b/.gitignore index 3c3629e..fd4f2b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.DS_Store diff --git a/lib/beats.js b/lib/services/beats.js similarity index 72% rename from lib/beats.js rename to lib/services/beats.js index a2599a5..e69a976 100644 --- a/lib/beats.js +++ b/lib/services/beats.js @@ -1,9 +1,13 @@ "use strict"; var parse = require('url').parse; var request = require('superagent'); +var Q = require('q'); + +module.exports.id = "beats"; if (!process.env.BEATS_KEY || !process.env.BEATS_SECRET) { - throw new Error("You need to set BEATS_KEY and BEATS_SECRET environment variables"); + console.warn("BEATS_KEY or BEATS_SECRET environment variables not found, deactivating Beats."); + return; } var credentials = { @@ -13,13 +17,19 @@ var credentials = { var apiRoot = "https://partner.api.beatsmusic.com/v1/api"; -module.exports.lookupId = function(id, next) { +module.exports.match = function(url, type) { + var parsed = parse(url); + return parsed.host.match(/beatsmusic\.com$/); +}; + +module.exports.lookupId = function(id) { + var deferred = Q.defer(); if (id.substr(0,2) == "al") { request.get(apiRoot + "/albums/" + id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).end(function(res) { var artwork = res.headers.location; request.get(apiRoot + "/albums/" + id + "?client_id=" + credentials.key, function(res) { var result = res.body.data; - next({ + deferred.resolve({ service: "beats", type: "album", id: result.id, @@ -37,7 +47,7 @@ module.exports.lookupId = function(id, next) { var result = res.body.data; request.get(apiRoot + "/albums/" + result.refs.album.id + "/images/default?size=medium&client_id=" + credentials.key).redirects(0).end(function(res) { var artwork = res.headers.location; - next({ + deferred.resolve({ service: "beats", type: "track", id: result.id, @@ -54,9 +64,11 @@ module.exports.lookupId = function(id, next) { }); }); } + return deferred.promise; }; -module.exports.search = function(data, next) { +module.exports.search = function(data) { + var deferred = Q.defer(); var query; var type = data.type; @@ -69,19 +81,22 @@ module.exports.search = function(data, next) { var path = "/search?q=" + encodeURIComponent(query) + "&type=" + type + "&client_id=" + credentials.key; request.get(apiRoot + path, function(res) { if (!res.body.data[0]) { - next({service: "beats"}); + deferred.resolve({service: "beats"}); } else { - module.exports.lookupId(res.body.data[0].id, next); + module.exports.lookupId(res.body.data[0].id).then(deferred.resolve); } }); + return deferred.promise; }; -module.exports.parseUrl = function(url, next) { +module.exports.parseUrl = function(url) { + var deferred = Q.defer(); var matches = parse(url).path.match(/\/albums[\/]+([^\/]+)(\/tracks\/)?([^\/]+)?/); if (matches && matches[3]) { - module.exports.lookupId(matches[3], next); + module.exports.lookupId(matches[3]).then(deferred.resolve); } else if (matches && matches[1]) { - module.exports.lookupId(matches[1], next); + module.exports.lookupId(matches[1]).then(deferred.resolve); } + return deferred.promise; } diff --git a/lib/services/deezer.js b/lib/services/deezer.js new file mode 100644 index 0000000..955c8f1 --- /dev/null +++ b/lib/services/deezer.js @@ -0,0 +1,86 @@ +"use strict"; +var parse = require('url').parse; +var request = require('superagent'); +var Q = require('q'); + +module.exports.id = "deezer"; + +var apiRoot = "https://api.deezer.com"; + +module.exports.match = function(url, type) { + var parsed = parse(url); + return parsed.host.match(/deezer\.com$/); +}; + +module.exports.lookupId = function(id, type) { + var deferred = Q.defer(); + var path = "/" + type + "/" + id; + + request.get(apiRoot + path, function(res) { + var result = res.body; + + if (type == "album") { + deferred.resolve({ + service: "deezer", + type: type, + id: result.id, + name: result.title, + url: result.link, + artwork: result.cover, + artist: { + name: result.artist.name + }, + }); + } else if (type == "track") { + deferred.resolve({ + service: "deezer", + type: type, + id: result.id, + name: result.title, + url: result.link, + artwork: result.album.cover, + artist: { + name: result.artist.name + }, + album: { + name: result.album.title + } + }); + }; + }); + return deferred.promise; +}; + +module.exports.search = function(data, next) { + var deferred = Q.defer(); + var query; + var type = data.type; + + if (type == "album") { + query = data.artist.name + " " + data.name; + } else if (type == "track") { + query = data.artist.name + " " + data.album.name + " " + data.name; + } + + var path = "/search/" + type + "?q=" + encodeURIComponent(query); + request.get(apiRoot + path, function(res) { + if (!res.body.data[0]) { + deferred.resolve({service: "deezer"}); + } else { + module.exports.lookupId(res.body.data[0].id, type).then(deferred.resolve); + } + }); + return deferred.promise; +}; + +module.exports.parseUrl = function(url, next) { + var deferred = Q.defer(); + var matches = parse(url).path.match(/\/albums[\/]+([^\/]+)(\/tracks\/)?([^\/]+)?/); + + if (matches && matches[3]) { + module.exports.lookupId(matches[3]).then(deferred.resolve); + } else if (matches && matches[1]) { + module.exports.lookupId(matches[1]).then(deferred.resolve); + } + return deferred.promise; +} diff --git a/lib/googleplaymusic.js b/lib/services/googleplaymusic.js similarity index 75% rename from lib/googleplaymusic.js rename to lib/services/googleplaymusic.js index 3889b37..e8e6dfe 100644 --- a/lib/googleplaymusic.js +++ b/lib/services/googleplaymusic.js @@ -2,18 +2,28 @@ var parse = require("url").parse; var PlayMusic = require('playmusic'); var pm = new PlayMusic(); +var Q = require('q'); + +module.exports.id = "google"; if (!process.env.GOOGLE_EMAIL || !process.env.GOOGLE_PASSWORD) { - throw new Error("You need to set GOOGLE_EMAIL and GOOGLE_PASSWORD environment variables"); + console.warn("GOOGLE_EMAIL or GOOGLE_PASSWORD environment variables not found, deactivating Rdio."); + return; } // It's probably ok to not wait for this to finish pm.init({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}, function() {}); +module.exports.match = function(url, type) { + var parsed = parse(url); + return parsed.host.match(/play\.google\.com$/); +}; + module.exports.lookupId = function(id, type, next) { + var deferred = Q.defer(); if (type == "album") { pm.getAlbum(id, true, function(album) { - next({ + deferred.resolve({ service: "googleplaymusic", type: "album", id: album.albumId, @@ -27,7 +37,7 @@ module.exports.lookupId = function(id, type, next) { }); } else if (type == "track") { pm.getAllAccessTrack(id, function(track) { - next({ + deferred.resolve({ service: "googleplaymusic", type: "track", id: track.nid, @@ -43,9 +53,11 @@ module.exports.lookupId = function(id, type, next) { }); }); } + return deferred.promise; } -module.exports.search = function(data, next) { +module.exports.search = function(data) { + var deferred = Q.defer(); var query = ""; var type = data.type; @@ -63,7 +75,7 @@ module.exports.search = function(data, next) { }).shift(); if (!result.album && !result.track) { - next({service:"googleplaymusic"}); + deferred.resolve({service:"googleplaymusic"}); } var id; @@ -73,11 +85,13 @@ module.exports.search = function(data, next) { id = result.track.nid; } - module.exports.lookupId(id, type, next); + module.exports.lookupId(id, type).then(deferred.resolve); }); + return deferred.promise; } module.exports.parseUrl = function(url, next) { + var deferred = Q.defer(); var parsed = parse(url.replace(/\+/g, "%20")); var path = parsed.path; var hash = parsed.hash; @@ -89,13 +103,14 @@ module.exports.parseUrl = function(url, next) { var album = decodeURIComponent(parts[4]); if (id.length > 0) { - return next({id: id, type: type}); + deferred.resolve({id: id, type: type}); } else { - module.exports.search({type: type, name:album, artist: {name: artist}}, next); + module.exports.search({type: type, name:album, artist: {name: artist}}).then(deferred.resolve); } } else if(path) { var matches = path.match(/\/music\/m\/([\w]+)/); var type = matches[1][0] == "T" ? "track" : "album"; - module.exports.lookupId(matches[1], type, next); + module.exports.lookupId(matches[1], type).then(deferred.resolve); } + return deferred.promise; } diff --git a/lib/rdio.js b/lib/services/rdio.js similarity index 80% rename from lib/rdio.js rename to lib/services/rdio.js index 6e1724e..ddea295 100644 --- a/lib/rdio.js +++ b/lib/services/rdio.js @@ -1,17 +1,26 @@ "use strict"; var parse = require('url').parse; +var Q = require('q'); + +module.exports.id = "rdio"; if (!process.env.RDIO_API_KEY || !process.env.RDIO_API_SHARED) { - throw new Error("You need to set RDIO_API_KEY and RDIO_API_SHARED environment variables"); + console.warn("RDIO_API_KEY or RDIO_API_SHARED environment variables not found, deactivating Rdio."); + return; } - var rdio = require('rdio')({ rdio_api_key: process.env.RDIO_API_KEY, rdio_api_shared: process.env.RDIO_API_SHARED, }); -module.exports.lookupId = function(id, next) { +module.exports.match = function(url, type) { + var parsed = parse(url); + return parsed.host.match(/rd\.io$/) || parsed.host.match(/rdio\.com$/); +}; + +module.exports.lookupId = function(id) { + var deferred = Q.defer(); rdio.api("", "", { method: 'getObjectFromShortCode', short_code: id, @@ -20,7 +29,7 @@ module.exports.lookupId = function(id, next) { var parsed = parse(result.shortUrl) var id = parsed.path.replace("/x/", "").replace("/", ""); var type = result.album ? "track" : "album"; - next({ + deferred.resolve({ service: "rdio", type: type, id: id, @@ -32,9 +41,11 @@ module.exports.lookupId = function(id, next) { } }); }); + return deferred.promise; }; -module.exports.lookupUrl = function(url, next) { +module.exports.parseUrl = function(url) { + var deferred = Q.defer(); var parsed = parse(url); var data; @@ -58,7 +69,7 @@ module.exports.lookupUrl = function(url, next) { var parsed = parse(result.shortUrl) var id = parsed.path.replace("/x/", "").replace("/", ""); var type = result.album ? "track" : "album"; - next({ + deferred.resolve({ service: "rdio", type: type, id: id, @@ -70,9 +81,11 @@ module.exports.lookupUrl = function(url, next) { } }); }); + return deferred.promise; }; -module.exports.search = function(data, next) { +module.exports.search = function(data) { + var deferred = Q.defer(); var query; var type = data.type; @@ -102,7 +115,7 @@ module.exports.search = function(data, next) { } var parsed = parse(result.shortUrl) var id = parsed.path.replace("/x/", "").replace("/", ""); - next({ + deferred.resolve({ service: "rdio", type: type, id: id, @@ -114,4 +127,5 @@ module.exports.search = function(data, next) { } }); }); + return deferred.promise; }; diff --git a/lib/spotify.js b/lib/services/spotify.js similarity index 71% rename from lib/spotify.js rename to lib/services/spotify.js index abb05c1..bea5a63 100644 --- a/lib/spotify.js +++ b/lib/services/spotify.js @@ -1,8 +1,17 @@ "use strict"; var parse = require('url').parse; var spotify = require('spotify'); +var Q = require('q'); -module.exports.lookupId = function(id, type, next) { +module.exports.id = "spotify"; + +module.exports.match = function(url, type) { + var parsed = parse(url); + return parsed.host.match(/spotify\.com$/); +}; + +module.exports.lookupId = function(id, type) { + var deferred = Q.defer(); spotify.lookup({id: id, type: type}, function(err, data) { if ( err ) { console.log('Error occurred: ' + err); @@ -12,7 +21,7 @@ module.exports.lookupId = function(id, type, next) { var artist = data.artists[0]; if (type == "album") { - next({ + deferred.resolve({ service: "spotify", type: type, id: data.id, @@ -24,7 +33,7 @@ module.exports.lookupId = function(id, type, next) { } }); } else if (type == "track") { - next({ + deferred.resolve({ service: "spotify", type: type, id: data.id, @@ -40,9 +49,11 @@ module.exports.lookupId = function(id, type, next) { }) } }); + return deferred.promise; } -module.exports.search = function(data, next) { +module.exports.search = function(data) { + var deferred = Q.defer(); var query = ""; var type = data.type; @@ -59,19 +70,22 @@ module.exports.search = function(data, next) { } if (!data[type + "s"].items[0]) { - next({service:"spotify"}); + deferred.resolve({service:"spotify"}); } var item = data[type + "s"].items[0]; - module.exports.lookupId(item.id, type, next); + module.exports.lookupId(item.id, type).then(deferred.resolve); }); + return deferred.promise; } -module.exports.parseUrl = function(url, next) { +module.exports.parseUrl = function(url) { + var deferred = Q.defer(); var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/); if (matches && matches[2]) { - module.exports.lookupId(matches[2], matches[1], next); + module.exports.lookupId(matches[2], matches[1]).then(deferred.resolve); } + return deferred.promise; } diff --git a/package.json b/package.json index 567dffb..cedcbe1 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "start": "node ./bin/www", - "test": "./node_modules/mocha/bin/mocha --timeout=5000" + "test": "./node_modules/mocha/bin/mocha test/**/*.js --timeout=5000" }, "engines": { "node": "0.10.x" @@ -20,6 +20,7 @@ "helmet": "^0.5.2", "morgan": "~1.3.0", "playmusic": "^1.1.0", + "q": "^1.1.2", "rdio": "^1.5.2", "serve-favicon": "~2.1.3", "spotify": "^0.3.0", diff --git a/public/images/deezer.png b/public/images/deezer.png new file mode 100644 index 0000000..d752a8b Binary files /dev/null and b/public/images/deezer.png differ diff --git a/routes/index.js b/routes/index.js index 14e12ca..e5b8ab3 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,83 +1,45 @@ "use strict"; var parse = require('url').parse; +var path = require('path'); var express = require('express'); var router = express.Router(); +var Q = require('q'); -var googleplaymusic = require('../lib/googleplaymusic'); -var spotify = require('../lib/spotify'); -var rdio = require('../lib/rdio'); -var beats = require('../lib/beats'); +var services = {}; + +require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach(function(file) { + var service = require("../lib/services/" + file); + if (service.search) { + services[service.id] = service; + } +}); var cache = {googleplaymusic:{}, spotify:{},rdio:{}}; router.get('/:service/:type/:id', function(req, res) { - var service = req.params.service; + var serviceId = req.params.service; var type = req.params.type; - var id = req.params.id; - var items = []; + var itemId = req.params.id; + var promises = []; - switch(service) { - case "spotify": - spotify.lookupId(id, type, function(result) { - items.push(result); - googleplaymusic.search(result, function(item) { - items.push(item); - rdio.search(result, function(item) { - items.push(item); - beats.search(result, function(item) { - items.push(item); - res.render(result.type, {items: items}); - }); - }); - }); + services[serviceId].lookupId(itemId, type).then(function(item) { + for (var id in services) { + if (id != serviceId) { + promises.push(services[id].search(item)); + } + } + + Q.allSettled(promises).then(function(results) { + var items = results.map(function(result) { + if (result.state == "fulfilled") { + return result.value; + } }); - break; - case "google": - googleplaymusic.lookupId(id, type, function(result) { - items.push(result); - spotify.search(result, function(item) { - items.push(item); - rdio.search(result, function(item) { - items.push(item); - beats.search(result, function(item) { - items.push(item); - res.render(result.type, {items: items}); - }); - }); - }); - }); - break; - case "rdio": - rdio.lookupId(id, function(result) { - items.push(result); - googleplaymusic.search(result, function(item) { - items.push(item); - spotify.search(result, function(item) { - items.push(item); - beats.search(result, function(item) { - items.push(item); - res.render(result.type, {items: items}); - }); - }); - }); - }); - break; - case "beats": - beats.lookupId(id, function(result) { - items.push(result); - googleplaymusic.search(result, function(item) { - items.push(item); - spotify.search(result, function(item) { - items.push(item); - rdio.search(result, function(item) { - items.push(item); - res.render(result.type, {items: items}); - }); - }); - }); - }); - break; - } + items.unshift(item); + + res.render(type, {items: items}); + }); + }); }); router.post('/search', function(req, res) { @@ -89,43 +51,22 @@ router.post('/search', function(req, res) { return; } - if (url.host.match(/rd\.io$/) || url.host.match(/rdio\.com$/)) { - rdio.lookupUrl(url.href, function(result) { - if (!result.id) { - req.flash('search-error', 'No match found for this link'); - res.redirect('/'); - } - res.redirect("/rdio/" + result.type + "/" + result.id); - }); - } else if (url.host.match(/spotify\.com$/)) { - spotify.parseUrl(url.href, function(result) { - if (!result.id) { - req.flash('search-error', 'No match found for this link'); - res.redirect('/'); - } - res.redirect("/spotify/" + result.type + "/" + result.id); - }); - } else if (url.host.match(/play\.google\.com$/)) { - googleplaymusic.parseUrl(url.href, function(result) { - if (!result) { - req.flash('search-error', 'No match found for this link'); - res.redirect('/'); - } else { - res.redirect("/google/" + result.type + "/" + result.id); - } - }); - } else if (url.host.match(/beatsmusic\.com$/)) { - beats.parseUrl(url.href, function(result) { - if (!result.id) { - req.flash('search-error', 'No match found for this link'); - res.redirect('/'); - } - res.redirect("/beats/" + result.type + "/" + result.id); - }); - } else { - req.flash('search-error', 'No match found for this link'); - res.redirect('/'); + for (var id in services) { + var matched = services[id].match(req.body.url); + if (matched) { + services[id].parseUrl(req.body.url).then(function(result) { + if (!result.id) { + req.flash('search-error', 'No match found for this link'); + res.redirect('/'); + } + res.redirect("/" + id + "/" + result.type + "/" + result.id); + }) + return; + } } + + req.flash('search-error', 'No match found for this link'); + res.redirect('/'); }); /* GET home page. */ diff --git a/test/services/beats.js b/test/services/beats.js new file mode 100644 index 0000000..c5776ba --- /dev/null +++ b/test/services/beats.js @@ -0,0 +1,57 @@ +"use strict"; +var assert = require("assert"); +var should = require('should'); + +var beats = require("../../lib/services/beats"); + +describe('Beats Music', function(){ + describe('lookupId', function(){ + it('should find album by ID', function(done){ + beats.lookupId("al920431").then(function(result) { + result.name.should.equal("Deftones"); + done(); + }); + }); + + it('should find track by ID', function(done){ + beats.lookupId("tr6910289").then(function(result) { + result.name.should.equal("Californication"); + done(); + }); + }); + }); + + describe('search', function(){ + it('should find album by search', function(done){ + beats.search({type: "album", artist: {name: "Deftones"}, name: "Deftones"}).then(function(result) { + result.name.should.equal("Deftones"); + done(); + }); + }); + + it('should find track by search', function(done){ + beats.search({type: "track", artist: {name: "Deftones"}, album: {name: "Deftones"}, name: "Hexagram"}).then(function(result) { + result.name.should.equal("Hexagram"); + done(); + }); + }); + }); + + describe('lookupUrl', function(){ + describe('parseUrl', function(){ + it('should parse album url into ID', function(done){ + beats.parseUrl("https://listen.beatsmusic.com/albums/al920431").then(function(result) { + result.id.should.equal("al920431"); + done(); + }); + }); + + it('should parse track url into ID', function(done){ + beats.parseUrl("https://listen.beatsmusic.com/albums/al6910269/tracks/tr6910289").then(function(result) { + result.id.should.equal("tr6910289"); + done(); + }); + }); + }); + }); +}); diff --git a/test/services/deezer.js b/test/services/deezer.js new file mode 100644 index 0000000..e69de29 diff --git a/test/services/googleplaymusic.js b/test/services/googleplaymusic.js new file mode 100644 index 0000000..a16f1fb --- /dev/null +++ b/test/services/googleplaymusic.js @@ -0,0 +1,48 @@ +"use strict"; +var assert = require("assert"); +var should = require('should'); + +var googleplaymusic = require("../../lib/services/googleplaymusic"); + +describe('Google Play Music', function(){ + describe('lookupId', function(){ + it('should find album by ID', function(done){ + googleplaymusic.lookupId("Byp6lvzimyf74wxi5634ul4tgam", "album").then(function(result) { + result.name.should.equal("Listen (Deluxe)"); + done(); + }); + }); + }); + + describe('search', function(){ + it('should find album by search', function(done){ + googleplaymusic.search({type: "album", artist: {name: "David Guetta"}, name: "Listen (Deluxe)"}).then(function(result) { + result.name.should.equal("Listen (Deluxe)"); + done(); + }); + }); + }); + + describe('lookupUrl', function(){ + it('should parse regular url into album ID', function(done){ + googleplaymusic.parseUrl("https://play.google.com/music/listen#/album/Byp6lvzimyf74wxi5634ul4tgam/David+Guetta/Listen+(Deluxe)").then(function(result) { + result.id.should.equal("Byp6lvzimyf74wxi5634ul4tgam"); + done(); + }); + }); + + it('should parse url without ID into album ID', function(done){ + googleplaymusic.parseUrl("https://play.google.com/music/listen#/album//David+Guetta/Listen+(Deluxe)").then(function(result) { + result.id.should.equal("Byp6lvzimyf74wxi5634ul4tgam"); + done(); + }); + }); + + it('should parse share url into album ID', function(done){ + googleplaymusic.parseUrl("https://play.google.com/music/m/Byp6lvzimyf74wxi5634ul4tgam").then(function(result) { + result.id.should.equal("Byp6lvzimyf74wxi5634ul4tgam"); + done(); + }); + }); + }); +}); diff --git a/test/services/rdio.js b/test/services/rdio.js new file mode 100644 index 0000000..81994bd --- /dev/null +++ b/test/services/rdio.js @@ -0,0 +1,41 @@ +"use strict"; +var assert = require("assert"); +var should = require('should'); + +var rdio = require("../../lib/services/rdio"); + +describe('Rdio', function(){ + describe('lookupId', function(){ + it('should find album by ID', function(done){ + rdio.lookupId("Qj4NXr0").then(function(result) { + result.name.should.equal("Listen (Deluxe)"); + done(); + }); + }); + }); + + describe('search', function(){ + it('should find album by search', function(done){ + rdio.search({type: "album", artist: {name: "David Guetta"}, name: "Listen (Deluxe)"}).then(function(result) { + result.name.should.equal("Listen (Deluxe)"); + done(); + }); + }); + }); + + describe('parseUrl', function(){ + it('should parse regular url into album object', function(done){ + rdio.parseUrl("https://www.rdio.com/artist/David_Guetta/album/Listen_(Deluxe)/").then(function(result) { + result.name.should.equal("Listen (Deluxe)"); + done(); + }); + }); + + it('should parse short url into album object', function(done){ + rdio.parseUrl("http://rd.io/x/Qj4NXr0/").then(function(result) { + result.name.should.equal("Listen (Deluxe)"); + done(); + }); + }); + }); +}); diff --git a/test/services/spotify.js b/test/services/spotify.js new file mode 100644 index 0000000..8fa6701 --- /dev/null +++ b/test/services/spotify.js @@ -0,0 +1,34 @@ +"use strict"; +var assert = require("assert"); +var should = require('should'); + +var spotify = require("../../lib/services/spotify"); + +describe('Spotify', function(){ + describe('lookupId', function(){ + it('should find album by ID', function(done){ + spotify.lookupId("77UW17CZFyCaRLHdHeofZu", "album").then(function(result) { + result.name.should.equal("Listen (Deluxe)"); + done(); + }); + }); + }); + + describe('search', function(){ + it('should find album by search', function(done){ + spotify.search({type: "album", artist: {name: "David Guetta"}, name: "Listen (Deluxe)"}).then(function(result) { + result.name.should.equal("Listen (Deluxe)"); + done(); + }); + }); + }); + + describe('parseUrl', function(){ + it('should parse url into ID', function(done){ + spotify.parseUrl("https://play.spotify.com/album/77UW17CZFyCaRLHdHeofZu").then(function(result) { + result.id.should.equal("77UW17CZFyCaRLHdHeofZu"); + done(); + }); + }); + }); +}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 12bcbc9..0000000 --- a/test/test.js +++ /dev/null @@ -1,167 +0,0 @@ -"use strict"; -var assert = require("assert"); -var should = require('should'); -var spotify = require("../lib/spotify"); -var rdio = require("../lib/rdio"); -var googleplaymusic = require("../lib/googleplaymusic"); -var beats = require("../lib/beats"); - -describe('Spotify', function(){ - describe('lookupId', function(){ - it('should find album by ID', function(done){ - spotify.lookupId("77UW17CZFyCaRLHdHeofZu", "album", function(result) { - result.name.should.equal("Listen (Deluxe)"); - done(); - }); - }); - }); - - describe('search', function(){ - it('should find album by search', function(done){ - spotify.search({type: "album", artist: {name: "David Guetta"}, name: "Listen (Deluxe)"}, function(result) { - result.name.should.equal("Listen (Deluxe)"); - done(); - }); - }); - }); - - describe('parseUrl', function(){ - it('should parse url into ID', function(done){ - spotify.parseUrl("https://play.spotify.com/album/77UW17CZFyCaRLHdHeofZu", function(result) { - result.id.should.equal("77UW17CZFyCaRLHdHeofZu"); - done(); - }); - }); - }); -}); - -describe('Rdio', function(){ - describe('lookupId', function(){ - it('should find album by ID', function(done){ - rdio.lookupId("Qj4NXr0", function(result) { - result.name.should.equal("Listen (Deluxe)"); - done(); - }); - }); - }); - - describe('search', function(){ - it('should find album by search', function(done){ - rdio.search({type: "album", artist: {name: "David Guetta"}, name: "Listen (Deluxe)"}, function(result) { - result.name.should.equal("Listen (Deluxe)"); - done(); - }); - }); - }); - - describe('lookupUrl', function(){ - it('should parse regular url into album object', function(done){ - rdio.lookupUrl("https://www.rdio.com/artist/David_Guetta/album/Listen_(Deluxe)/", function(result) { - result.name.should.equal("Listen (Deluxe)"); - done(); - }); - }); - - it('should parse short url into album object', function(done){ - rdio.lookupUrl("http://rd.io/x/Qj4NXr0/", function(result) { - result.name.should.equal("Listen (Deluxe)"); - done(); - }); - }); - }); -}); - -describe('Google Play Music', function(){ - describe('lookupId', function(){ - it('should find album by ID', function(done){ - googleplaymusic.lookupId("Byp6lvzimyf74wxi5634ul4tgam", "album", function(result) { - result.name.should.equal("Listen (Deluxe)"); - done(); - }); - }); - }); - - describe('search', function(){ - it('should find album by search', function(done){ - googleplaymusic.search({type: "album", artist: {name: "David Guetta"}, name: "Listen (Deluxe)"}, function(result) { - result.name.should.equal("Listen (Deluxe)"); - done(); - }); - }); - }); - - describe('lookupUrl', function(){ - it('should parse regular url into album ID', function(done){ - googleplaymusic.parseUrl("https://play.google.com/music/listen#/album/Byp6lvzimyf74wxi5634ul4tgam/David+Guetta/Listen+(Deluxe)", function(result) { - result.id.should.equal("Byp6lvzimyf74wxi5634ul4tgam"); - done(); - }); - }); - - it('should parse url without ID into album ID', function(done){ - googleplaymusic.parseUrl("https://play.google.com/music/listen#/album//David+Guetta/Listen+(Deluxe)", function(result) { - result.id.should.equal("Byp6lvzimyf74wxi5634ul4tgam"); - done(); - }); - }); - - it('should parse share url into album ID', function(done){ - googleplaymusic.parseUrl("https://play.google.com/music/m/Byp6lvzimyf74wxi5634ul4tgam", function(result) { - result.id.should.equal("Byp6lvzimyf74wxi5634ul4tgam"); - done(); - }); - }); - }); -}); - -describe('Beats Music', function(){ - describe('lookupId', function(){ - it('should find album by ID', function(done){ - beats.lookupId("al920431", function(result) { - result.name.should.equal("Deftones"); - done(); - }); - }); - - it('should find track by ID', function(done){ - beats.lookupId("tr6910289", function(result) { - result.name.should.equal("Californication"); - done(); - }); - }); - }); - - describe('search', function(){ - it('should find album by search', function(done){ - beats.search({type: "album", artist: {name: "Deftones"}, name: "Deftones"}, function(result) { - result.name.should.equal("Deftones"); - done(); - }); - }); - - it('should find track by search', function(done){ - beats.search({type: "track", artist: {name: "Deftones"}, album: {name: "Deftones"}, name: "Hexagram"}, function(result) { - result.name.should.equal("Hexagram"); - done(); - }); - }); - }); - - describe('lookupUrl', function(){ - describe('parseUrl', function(){ - it('should parse album url into ID', function(done){ - beats.parseUrl("https://listen.beatsmusic.com/albums/al920431", function(result) { - result.id.should.equal("al920431"); - done(); - }); - }); - - it('should parse track url into ID', function(done){ - beats.parseUrl("https://listen.beatsmusic.com/albums/al6910269/tracks/tr6910289", function(result) { - result.id.should.equal("tr6910289"); - done(); - }); - }); - }); - }); -});