Major refactor of service libs, more tests
This commit is contained in:
parent
cda69ea472
commit
ce3ff0442c
19 changed files with 288 additions and 118 deletions
|
@ -17,18 +17,33 @@ var credentials = {
|
|||
|
||||
var apiRoot = "https://partner.api.beatsmusic.com/v1/api";
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
var parsed = parse(url);
|
||||
return parsed.host.match(/beatsmusic\.com$/);
|
||||
};
|
||||
module.exports.match = require("./url").match;
|
||||
|
||||
module.exports.lookupId = function(id) {
|
||||
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], "track").then(deferred.resolve);
|
||||
} else if (matches && matches[1]) {
|
||||
module.exports.lookupId(matches[1], "album").then(deferred.resolve);
|
||||
} else {
|
||||
deferred.reject();
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
module.exports.lookupId = function(id, type) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
if (id.substr(0,2) == "al") {
|
||||
if (type == "album") {
|
||||
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) {
|
||||
if (!res.body.data) {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
return deferred.reject(error);
|
||||
}
|
||||
var result = res.body.data;
|
||||
deferred.resolve({
|
||||
service: "beats",
|
||||
|
@ -44,8 +59,13 @@ module.exports.lookupId = function(id) {
|
|||
});
|
||||
});
|
||||
});
|
||||
} else if (id.substr(0,2) == "tr") {
|
||||
} else if (type == "track") {
|
||||
request.get(apiRoot + "/tracks/" + id + "?client_id=" + credentials.key, function(res) {
|
||||
if (!res.body.data) {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
return deferred.reject(error);
|
||||
}
|
||||
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;
|
||||
|
@ -103,20 +123,8 @@ module.exports.search = function(data) {
|
|||
deferred.resolve({service: "beats"});
|
||||
}
|
||||
} else {
|
||||
module.exports.lookupId(res.body.data[0].id).then(deferred.resolve);
|
||||
module.exports.lookupId(res.body.data[0].id, type).then(deferred.resolve);
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
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]).then(deferred.resolve);
|
||||
} else if (matches && matches[1]) {
|
||||
module.exports.lookupId(matches[1]).then(deferred.resolve);
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
11
lib/services/beats/url.js
Normal file
11
lib/services/beats/url.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
|
||||
module.exports.match = function(url) {
|
||||
var parsed = parse(url);
|
||||
if (!parsed.host.match(/beatsmusic\.com$/)) {
|
||||
return false;
|
||||
}
|
||||
var matches = parsed.path.match(/\/albums[\/]+([^\/]+)(\/tracks\/)?([^\/]+)?/);
|
||||
return matches.length > 1;
|
||||
};
|
|
@ -7,10 +7,19 @@ 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.match = require('./url').match;
|
||||
|
||||
module.exports.parseUrl = function(url, next) {
|
||||
var deferred = Q.defer();
|
||||
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
||||
|
||||
if (matches && matches[2]) {
|
||||
module.exports.lookupId(matches[2], matches[1]).then(deferred.resolve);
|
||||
} else {
|
||||
deferred.reject();
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
module.exports.lookupId = function(id, type) {
|
||||
var deferred = Q.defer();
|
||||
|
@ -96,13 +105,3 @@ module.exports.search = function(data, next) {
|
|||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
module.exports.parseUrl = function(url, next) {
|
||||
var deferred = Q.defer();
|
||||
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
||||
|
||||
if (matches && matches[2]) {
|
||||
module.exports.lookupId(matches[2], matches[1]).then(deferred.resolve);
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
11
lib/services/deezer/url.js
Normal file
11
lib/services/deezer/url.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
|
||||
module.exports.match = function(url) {
|
||||
var parsed = parse(url);
|
||||
if (!parsed.host.match(/deezer\.com$/)) {
|
||||
return false;
|
||||
}
|
||||
var matches = parsed.path.match(/\/(album|track)[\/]+([^\/]+)/);
|
||||
return matches.length > 1;
|
||||
};
|
|
@ -17,10 +17,34 @@ pm.init({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}
|
|||
ready.resolve();
|
||||
});
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
var parsed = parse(url);
|
||||
return parsed.host.match(/play\.google\.com$/);
|
||||
};
|
||||
module.exports.match = require('./url').match;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
var deferred = Q.defer();
|
||||
ready.promise.then(function() {
|
||||
var parsed = parse(url.replace(/\+/g, "%20"));
|
||||
var path = parsed.path;
|
||||
var hash = parsed.hash;
|
||||
if (hash) {
|
||||
var parts = hash.split("/");
|
||||
var type = parts[1];
|
||||
var id = parts[2];
|
||||
var artist = decodeURIComponent(parts[3]);
|
||||
var album = decodeURIComponent(parts[4]);
|
||||
|
||||
if (id.length > 0) {
|
||||
deferred.resolve({id: id, type: type});
|
||||
} else {
|
||||
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).then(deferred.resolve);
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
module.exports.lookupId = function(id, type, next) {
|
||||
var deferred = Q.defer();
|
||||
|
@ -28,7 +52,7 @@ module.exports.lookupId = function(id, type, next) {
|
|||
if (type == "album") {
|
||||
pm.getAlbum(id, false, function(album) {
|
||||
deferred.resolve({
|
||||
service: "googleplaymusic",
|
||||
service: "google",
|
||||
type: "album",
|
||||
id: album.albumId,
|
||||
name: album.name,
|
||||
|
@ -45,7 +69,7 @@ module.exports.lookupId = function(id, type, next) {
|
|||
} else if (type == "track") {
|
||||
pm.getAllAccessTrack(id, function(track) {
|
||||
deferred.resolve({
|
||||
service: "googleplaymusic",
|
||||
service: "google",
|
||||
type: "track",
|
||||
id: track.nid,
|
||||
name: track.title,
|
||||
|
@ -104,7 +128,7 @@ module.exports.search = function(data) {
|
|||
}).shift();
|
||||
|
||||
if (!result) {
|
||||
deferred.resolve({service: "googleplaymusic"});
|
||||
deferred.resolve({service: "google"});
|
||||
} else {
|
||||
var id;
|
||||
if (type == "album") {
|
||||
|
@ -119,30 +143,3 @@ module.exports.search = function(data) {
|
|||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
module.exports.parseUrl = function(url, next) {
|
||||
var deferred = Q.defer();
|
||||
ready.promise.then(function() {
|
||||
var parsed = parse(url.replace(/\+/g, "%20"));
|
||||
var path = parsed.path;
|
||||
var hash = parsed.hash;
|
||||
if (hash) {
|
||||
var parts = hash.split("/");
|
||||
var type = parts[1];
|
||||
var id = parts[2];
|
||||
var artist = decodeURIComponent(parts[3]);
|
||||
var album = decodeURIComponent(parts[4]);
|
||||
|
||||
if (id.length > 0) {
|
||||
deferred.resolve({id: id, type: type});
|
||||
} else {
|
||||
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).then(deferred.resolve);
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
30
lib/services/google/url.js
Normal file
30
lib/services/google/url.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
|
||||
module.exports.match = function(url) {
|
||||
var parsed = parse(url.replace(/\+/g, "%20"));
|
||||
if (!parsed.host.match(/play\.google\.com$/)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var path = parsed.path;
|
||||
var hash = parsed.hash;
|
||||
|
||||
if (hash) {
|
||||
var parts = hash.split("/");
|
||||
var id = parts[2];
|
||||
var artist = parts[3];
|
||||
|
||||
if (id.length > 0) {
|
||||
return true;
|
||||
} else if (artist.length > 0) {
|
||||
return true;
|
||||
}
|
||||
} else if(path) {
|
||||
var matches = path.match(/\/music\/m\/([\w]+)/);
|
||||
if (matches[1]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
};
|
|
@ -1,5 +1,6 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var querystring = require('querystring');
|
||||
var request = require('superagent');
|
||||
var Q = require('q');
|
||||
|
||||
|
@ -7,9 +8,26 @@ module.exports.id = "itunes";
|
|||
|
||||
var apiRoot = "https://itunes.apple.com";
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
module.exports.match = require('./url').match;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
var deferred = Q.defer();
|
||||
var parsed = parse(url);
|
||||
return parsed.host.match(/itunes.apple\.com$/);
|
||||
var matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
|
||||
var query = querystring.parse(parsed.query);
|
||||
|
||||
if (matches) {
|
||||
var type = "album";
|
||||
var id = matches[3].substr(2);
|
||||
if (query.i) {
|
||||
type = "track";
|
||||
id = query.i;
|
||||
}
|
||||
module.exports.lookupId(id, type, matches[1] || "us").then(deferred.resolve, deferred.reject);
|
||||
} else {
|
||||
deferred.reject();
|
||||
}
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
module.exports.lookupId = function(id, type, cc) {
|
||||
|
@ -38,7 +56,7 @@ module.exports.lookupId = function(id, type, cc) {
|
|||
var item = {
|
||||
service: "itunes",
|
||||
type: type,
|
||||
id: cc + result.collectionId,
|
||||
id: cc + id,
|
||||
name: result.trackName ? result.trackName : result.collectionName,
|
||||
streamUrl: null,
|
||||
purchaseUrl: result.collectionViewUrl,
|
||||
|
@ -58,7 +76,7 @@ module.exports.lookupId = function(id, type, cc) {
|
|||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.search = function(data) {
|
||||
var deferred = Q.defer();
|
||||
|
@ -118,16 +136,4 @@ module.exports.search = function(data) {
|
|||
});
|
||||
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
var deferred = Q.defer();
|
||||
var matches = parse(url).path.match(/[\/]?([\/]?[a-z]{2}?)?\/(album|track)[\/]+([^\/]+)[\/]+([^\?]+)/);
|
||||
|
||||
if (matches && matches[4]) {
|
||||
module.exports.lookupId(matches[4].substr(2), matches[2], matches[1]).then(deferred.resolve, deferred.reject);
|
||||
} else if (matches[3]) {
|
||||
module.exports.lookupId(matches[3].substr(2), matches[1], "").then(deferred.resolve, deferred.reject);
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
||||
};
|
15
lib/services/itunes/url.js
Normal file
15
lib/services/itunes/url.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
var parsed = parse(url);
|
||||
|
||||
if (!parsed.host.match(/itunes.apple\.com$/)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var matches = parsed.path.match(/[\/]?([\/]?[a-z]{2}?)?[\/]+album[\/]+([^\/]+)[\/]+([^\?]+)/);
|
||||
var query = querystring.parse(parsed.query);
|
||||
|
||||
return !!matches[3];
|
||||
};
|
|
@ -14,10 +14,7 @@ var rdio = require('rdio')({
|
|||
rdio_api_shared: process.env.RDIO_API_SHARED,
|
||||
});
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
var parsed = parse(url);
|
||||
return parsed.host.match(/rd\.io$/) || parsed.host.match(/rdio\.com$/);
|
||||
};
|
||||
module.exports.match = require('./url').match;
|
||||
|
||||
module.exports.lookupId = function(id) {
|
||||
var deferred = Q.defer();
|
||||
|
@ -76,7 +73,7 @@ module.exports.parseUrl = function(url) {
|
|||
} else {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
deferred.reject(error);
|
||||
return deferred.reject(error);
|
||||
}
|
||||
|
||||
rdio.api("", "", data, function(err, results) {
|
||||
|
@ -85,7 +82,7 @@ module.exports.parseUrl = function(url) {
|
|||
if (!result || results.status != "ok") {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
deferred.reject(error);
|
||||
return deferred.reject(error);
|
||||
} else {
|
||||
var parsed = parse(result.shortUrl)
|
||||
var id = parsed.path.replace("/x/", "").replace("/", "");
|
11
lib/services/rdio/url.js
Normal file
11
lib/services/rdio/url.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
|
||||
module.exports.match = function(url) {
|
||||
var parsed = parse(url);
|
||||
if (!parsed.host.match(/rd\.io$/) && !parsed.host.match(/rdio\.com$/)) {
|
||||
return false;
|
||||
}
|
||||
var matches = parsed.path.match(/[\/]*artist[\/]*([^\/]*)[\/]*album[\/]*([^\/]*)[\/]*([track]*)?[\/]*([^\/]*)/);
|
||||
return !!matches[2];
|
||||
};
|
|
@ -5,10 +5,17 @@ var Q = require('q');
|
|||
|
||||
module.exports.id = "spotify";
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
var parsed = parse(url);
|
||||
return parsed.host.match(/spotify\.com$/);
|
||||
};
|
||||
module.exports.match = require('./url').match;
|
||||
|
||||
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]).then(deferred.resolve);
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
module.exports.lookupId = function(id, type) {
|
||||
var deferred = Q.defer();
|
||||
|
@ -95,13 +102,3 @@ module.exports.search = function(data) {
|
|||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
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]).then(deferred.resolve);
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
12
lib/services/spotify/url.js
Normal file
12
lib/services/spotify/url.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
var parsed = parse(url);
|
||||
if (!parsed.host.match(/spotify\.com$/)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
|
||||
return !!matches[2];
|
||||
};
|
|
@ -16,9 +16,7 @@ var credentials = {
|
|||
|
||||
var apiRoot = "https://www.googleapis.com/youtube/v3";
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
return false;
|
||||
};
|
||||
module.exports.match = require('./url').match;
|
||||
|
||||
module.exports.search = function(data) {
|
||||
var deferred = Q.defer();
|
||||
|
@ -33,7 +31,7 @@ module.exports.search = function(data) {
|
|||
album = data.album.name
|
||||
}
|
||||
|
||||
var path = "/search?part=snippet&q=" + encodeURIComponent(query) + "&type=video&videoCaption=any&key=" + credentials.key;
|
||||
var path = "/search?part=snippet&q=" + encodeURIComponent(query) + "&type=video&videoCaption=any&videoCategoryId=10&key=" + credentials.key;
|
||||
|
||||
request.get(apiRoot + path, function(res) {
|
||||
var result = res.body.items[0];
|
5
lib/services/youtube/url.js
Normal file
5
lib/services/youtube/url.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
module.exports.match = function(url, type) {
|
||||
return false;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue