combine.fm/routes/search.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

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