combine.fm/routes/search.js
2015-05-17 19:10:30 +01:00

44 lines
1.4 KiB
JavaScript

"use strict";
var parse = require("url").parse;
var lookup = require("../lib/lookup");
var services = require("../lib/services");
module.exports = function(req, res) {
var url = parse(req.body.url);
if (!url.host) {
return res.json({error: {message: "You need to submit a url."}});
}
var promise = lookup(req.body.url);
if (!promise) {
return res.json({error: {message: "No supported music found at that link :("}});
}
promise.then(function(item) {
if (!item) {
return res.json({error: {message: "No supported music found at that link :("}});
}
item.matched_at = new Date(); // eslint-disable-line camelcase
var matches = {};
matches[item.service] = item;
services.forEach(function(service) {
if (service.id === item.service) {
return;
}
matches[service.id] = {service: service.id};
service.search(item).then(function(match) {
match.matched_at = new Date(); // eslint-disable-line camelcase
var update = {};
update["services." + match.service] = match;
req.db.matches.update({_id: item.service + "$$" + item.id}, {"$set": update});
});
});
return req.db.matches.save({_id: item.service + "$$" + item.id, "created_at": new Date(), services: matches}).then(function() {
res.json(item);
});
}, function(error) {
console.log(error.stack);
res.json({error: {message: "No matches found for this link, sorry :("}});
});
};