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,
|
||||
id: result.id,
|
||||
name: result.title,
|
||||
url: result.link,
|
||||
streamUrl: result.album.link,
|
||||
purchaseUrl: null,
|
||||
artwork: artwork,
|
||||
artist: {
|
||||
name: result.artist.name
|
||||
|
|
|
@ -88,13 +88,15 @@ module.exports.parseUrl = function(url) {
|
|||
|
||||
module.exports.search = function(data) {
|
||||
var deferred = Q.defer();
|
||||
var query;
|
||||
var query, albumClean;
|
||||
var type = data.type;
|
||||
|
||||
if (type == "album") {
|
||||
query = data.artist.name + " " + data.name;
|
||||
albumClean = data.name.match(/([^\(\[]+)/)[0];
|
||||
} else if (type == "track") {
|
||||
query = data.artist.name + " " + data.album.name + " " + data.name;
|
||||
albumClean = data.album.name.match(/([^\(\[]+)/)[0];
|
||||
}
|
||||
|
||||
rdio.api("", "", {
|
||||
|
@ -105,9 +107,9 @@ module.exports.search = function(data) {
|
|||
var results = JSON.parse(results).result.results;
|
||||
|
||||
var result = results.filter(function(result) {
|
||||
if (type == "album" && result.name == data.name) {
|
||||
if (type == "album" && result.name.match(/([^\(\[]+)/)[0] == albumClean) {
|
||||
return result;
|
||||
} else if (type == "track" && result.album == data.album.name) {
|
||||
} else if (type == "track" && result.album.match(/([^\(\[]+)/)[0] == albumClean) {
|
||||
return result;
|
||||
}
|
||||
}).shift();
|
||||
|
|
|
@ -14,6 +14,7 @@ require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach
|
|||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = function(req, res) {
|
||||
var serviceId = req.params.service;
|
||||
var type = req.params.type;
|
||||
|
@ -26,9 +27,10 @@ module.exports = function(req, res) {
|
|||
}
|
||||
|
||||
services[serviceId].lookupId(itemId, type).then(function(item) {
|
||||
|
||||
for (var id in services) {
|
||||
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") {
|
||||
return result.value;
|
||||
}
|
||||
}).filter(function(result) {
|
||||
return result || false;
|
||||
});
|
||||
|
||||
items.sort(function(a, b) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue