Use promises, split tests, make music services modular, add deezer.
This commit is contained in:
parent
7cc68d1968
commit
2b65c0632a
15 changed files with 407 additions and 307 deletions
|
@ -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;
|
||||
}
|
86
lib/services/deezer.js
Normal file
86
lib/services/deezer.js
Normal file
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
};
|
|
@ -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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue