Improve resiliency against slow api calls, rdio and brackets
This commit is contained in:
parent
2be3660765
commit
f7b1fcd53f
3 changed files with 12 additions and 5 deletions
|
@ -40,7 +40,8 @@ module.exports.lookupId = function(id, type) {
|
||||||
type: type,
|
type: type,
|
||||||
id: result.id,
|
id: result.id,
|
||||||
name: result.title,
|
name: result.title,
|
||||||
url: result.link,
|
streamUrl: result.album.link,
|
||||||
|
purchaseUrl: null,
|
||||||
artwork: artwork,
|
artwork: artwork,
|
||||||
artist: {
|
artist: {
|
||||||
name: result.artist.name
|
name: result.artist.name
|
||||||
|
|
|
@ -88,13 +88,15 @@ module.exports.parseUrl = function(url) {
|
||||||
|
|
||||||
module.exports.search = function(data) {
|
module.exports.search = function(data) {
|
||||||
var deferred = Q.defer();
|
var deferred = Q.defer();
|
||||||
var query;
|
var query, albumClean;
|
||||||
var type = data.type;
|
var type = data.type;
|
||||||
|
|
||||||
if (type == "album") {
|
if (type == "album") {
|
||||||
query = data.artist.name + " " + data.name;
|
query = data.artist.name + " " + data.name;
|
||||||
|
albumClean = data.name.match(/([^\(\[]+)/)[0];
|
||||||
} else if (type == "track") {
|
} else if (type == "track") {
|
||||||
query = data.artist.name + " " + data.album.name + " " + data.name;
|
query = data.artist.name + " " + data.album.name + " " + data.name;
|
||||||
|
albumClean = data.album.name.match(/([^\(\[]+)/)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
rdio.api("", "", {
|
rdio.api("", "", {
|
||||||
|
@ -105,9 +107,9 @@ module.exports.search = function(data) {
|
||||||
var results = JSON.parse(results).result.results;
|
var results = JSON.parse(results).result.results;
|
||||||
|
|
||||||
var result = results.filter(function(result) {
|
var result = results.filter(function(result) {
|
||||||
if (type == "album" && result.name == data.name) {
|
if (type == "album" && result.name.match(/([^\(\[]+)/)[0] == albumClean) {
|
||||||
return result;
|
return result;
|
||||||
} else if (type == "track" && result.album == data.album.name) {
|
} else if (type == "track" && result.album.match(/([^\(\[]+)/)[0] == albumClean) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}).shift();
|
}).shift();
|
||||||
|
|
|
@ -14,6 +14,7 @@ require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
module.exports = function(req, res) {
|
module.exports = function(req, res) {
|
||||||
var serviceId = req.params.service;
|
var serviceId = req.params.service;
|
||||||
var type = req.params.type;
|
var type = req.params.type;
|
||||||
|
@ -26,9 +27,10 @@ module.exports = function(req, res) {
|
||||||
}
|
}
|
||||||
|
|
||||||
services[serviceId].lookupId(itemId, type).then(function(item) {
|
services[serviceId].lookupId(itemId, type).then(function(item) {
|
||||||
|
|
||||||
for (var id in services) {
|
for (var id in services) {
|
||||||
if (id != serviceId) {
|
if (id != serviceId) {
|
||||||
promises.push(services[id].search(item));
|
promises.push(Q.timeout(services[id].search(item), 5000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,6 +39,8 @@ module.exports = function(req, res) {
|
||||||
if (result.state == "fulfilled") {
|
if (result.state == "fulfilled") {
|
||||||
return result.value;
|
return result.value;
|
||||||
}
|
}
|
||||||
|
}).filter(function(result) {
|
||||||
|
return result || false;
|
||||||
});
|
});
|
||||||
|
|
||||||
items.sort(function(a, b) {
|
items.sort(function(a, b) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue