2014-12-04 22:50:30 +00:00
|
|
|
"use strict";
|
|
|
|
var path = require('path');
|
2014-12-13 00:10:41 +00:00
|
|
|
var Promise = require('bluebird');
|
2014-12-04 22:50:30 +00:00
|
|
|
|
|
|
|
var services = {};
|
|
|
|
|
|
|
|
require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach(function(file) {
|
|
|
|
var service = require("../lib/services/" + file);
|
|
|
|
if (service.search) {
|
|
|
|
services[service.id] = service;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-05 13:44:42 +00:00
|
|
|
|
2014-12-05 17:40:07 +00:00
|
|
|
module.exports = function(req, res, next) {
|
2014-12-04 22:50:30 +00:00
|
|
|
var serviceId = req.params.service;
|
|
|
|
var type = req.params.type;
|
|
|
|
var itemId = req.params.id;
|
|
|
|
var promises = [];
|
|
|
|
|
2014-12-05 17:40:07 +00:00
|
|
|
if (!services[serviceId] || (type != "album" && type != "track")) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
req.db.matches.findOne({_id:serviceId + itemId}).then(function(doc) {
|
2014-12-05 16:26:01 +00:00
|
|
|
if (doc) {
|
2014-12-06 14:42:01 +00:00
|
|
|
res.render(type, {
|
|
|
|
page: type,
|
|
|
|
title: doc.items[0].name + " by " + doc.items[0].artist.name,
|
|
|
|
items: doc.items,
|
2014-12-06 14:46:30 +00:00
|
|
|
thisUrl: req.userProtocol + '://' + req.get('host') + req.originalUrl
|
2014-12-06 14:42:01 +00:00
|
|
|
});
|
2014-12-05 16:26:01 +00:00
|
|
|
} else {
|
2014-12-13 00:10:41 +00:00
|
|
|
services[serviceId].lookupId(itemId, type).timeout(10000).then(function(item) {
|
2014-12-05 16:26:01 +00:00
|
|
|
for (var id in services) {
|
|
|
|
if (id != serviceId) {
|
2014-12-13 00:10:41 +00:00
|
|
|
promises.push(services[id].search(item).timeout(10000));
|
2014-12-05 16:26:01 +00:00
|
|
|
}
|
2014-12-04 22:50:30 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 00:10:41 +00:00
|
|
|
Promise.settle(promises).then(function(results) {
|
2014-12-05 16:26:01 +00:00
|
|
|
var items = results.map(function(result) {
|
2014-12-13 00:10:41 +00:00
|
|
|
if (result.isFulfilled()) {
|
|
|
|
return result.value();
|
2014-12-05 16:26:01 +00:00
|
|
|
}
|
|
|
|
}).filter(function(result) {
|
|
|
|
return result || false;
|
|
|
|
});
|
|
|
|
|
|
|
|
items.sort(function(a, b) {
|
|
|
|
return !a.id || !b.id;
|
|
|
|
}).sort(function(a, b) {
|
|
|
|
return !a.streamUrl || b.streamUrl;
|
|
|
|
}).sort(function(a, b) {
|
|
|
|
return a.type == "video" && b.type != "video";
|
|
|
|
});
|
|
|
|
|
|
|
|
items.unshift(item);
|
2014-12-05 17:40:07 +00:00
|
|
|
req.db.matches.save({_id:serviceId + itemId, items:items});
|
2014-12-06 14:42:01 +00:00
|
|
|
res.render(type, {
|
|
|
|
page: type,
|
|
|
|
title: item.name + " by " + item.artist.name,
|
|
|
|
items: items,
|
2014-12-06 14:46:30 +00:00
|
|
|
thisUrl: req.userProtocol + '://' + req.get('host') + req.originalUrl
|
2014-12-06 14:42:01 +00:00
|
|
|
});
|
2014-12-05 16:26:01 +00:00
|
|
|
});
|
2014-12-05 18:03:45 +00:00
|
|
|
}, function(error) {
|
|
|
|
if (error.code == "ETIMEDOUT") {
|
2014-12-05 17:40:07 +00:00
|
|
|
error = new Error("Error talking to music service");
|
|
|
|
error.status = "502";
|
2014-12-05 18:03:45 +00:00
|
|
|
} else if (!error.status) {
|
|
|
|
error = new Error("An unexpected error happenend");
|
|
|
|
error.status = 500;
|
2014-12-05 17:40:07 +00:00
|
|
|
}
|
|
|
|
next(error);
|
2014-12-04 22:50:30 +00:00
|
|
|
});
|
2014-12-05 16:26:01 +00:00
|
|
|
}
|
2014-12-04 22:50:30 +00:00
|
|
|
});
|
|
|
|
};
|