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
139
lib/services/itunes/index.js
Normal file
139
lib/services/itunes/index.js
Normal file
|
@ -0,0 +1,139 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var querystring = require('querystring');
|
||||
var request = require('superagent');
|
||||
var Q = require('q');
|
||||
|
||||
module.exports.id = "itunes";
|
||||
|
||||
var apiRoot = "https://itunes.apple.com";
|
||||
|
||||
module.exports.match = require('./url').match;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
var deferred = Q.defer();
|
||||
var parsed = parse(url);
|
||||
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) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
if (id.match(/^[a-z]{2}/)) {
|
||||
cc = id.substr(0,2);
|
||||
id = id.substr(2);
|
||||
}
|
||||
|
||||
var path = "/lookup?id=" + id;
|
||||
if (cc) {
|
||||
path = "/" + cc + path;
|
||||
}
|
||||
|
||||
request.get(apiRoot + path, function(res) {
|
||||
var data = JSON.parse(res.text);
|
||||
|
||||
if (!data.results || data.resultCount == 0 || !data.results[0].collectionId) {
|
||||
var error = new Error("Not Found");
|
||||
error.status = 404;
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
var result = data.results[0];
|
||||
|
||||
var item = {
|
||||
service: "itunes",
|
||||
type: type,
|
||||
id: cc + id,
|
||||
name: result.trackName ? result.trackName : result.collectionName,
|
||||
streamUrl: null,
|
||||
purchaseUrl: result.collectionViewUrl,
|
||||
artwork: "https://match.audio/itunes/" + result.artworkUrl100.replace("100x100", "200x200").replace("http://", ""),
|
||||
artist: {
|
||||
name: result.artistName
|
||||
}
|
||||
};
|
||||
|
||||
if (type == "track") {
|
||||
item.album = {
|
||||
name: result.collectionName
|
||||
};
|
||||
}
|
||||
|
||||
deferred.resolve(item);
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
module.exports.search = function(data) {
|
||||
var deferred = Q.defer();
|
||||
var query, album, entity;
|
||||
var type = data.type;
|
||||
|
||||
if (type == "album") {
|
||||
query = data.artist.name + " " + data.name;
|
||||
album = data.name;
|
||||
entity = "album";
|
||||
} else if (type == "track") {
|
||||
query = data.artist.name + " " + data.album.name + " " + data.name;
|
||||
album = data.album.name;
|
||||
entity = "musicTrack";
|
||||
}
|
||||
|
||||
var path = "/search?term=" + encodeURIComponent(query) + "&media=music&entity=" + entity;
|
||||
request.get(apiRoot + path, function(res) {
|
||||
var result = JSON.parse(res.text);
|
||||
|
||||
if (!result.results[0]) {
|
||||
var matches = album.match(/^[^\(\[]+/);
|
||||
if (matches[0] && matches[0] != album) {
|
||||
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();
|
||||
}
|
||||
module.exports.search(cleanedData).then(deferred.resolve);
|
||||
} else {
|
||||
deferred.resolve({service: "itunes"});
|
||||
}
|
||||
} else {
|
||||
var result = result.results[0];
|
||||
|
||||
var item = {
|
||||
service: "itunes",
|
||||
type: type,
|
||||
id: "us" + result.collectionId,
|
||||
name: result.trackName ? result.trackName : result.collectionName,
|
||||
streamUrl: null,
|
||||
purchaseUrl: result.collectionViewUrl,
|
||||
artwork: "https://match.audio/itunes/" + result.artworkUrl100.replace("100x100", "200x200").replace("http://", ""),
|
||||
artist: {
|
||||
name: result.artistName
|
||||
}
|
||||
};
|
||||
|
||||
if (type == "track") {
|
||||
item.album = {
|
||||
name: result.collectionName
|
||||
};
|
||||
}
|
||||
deferred.resolve(item);
|
||||
}
|
||||
});
|
||||
|
||||
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];
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue