combine.fm/routes/search.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-06-03 21:45:54 -07:00
import {parse} from 'url';
import co from 'co';
import lookup from '../lib/lookup';
import services from '../lib/services';
2015-05-17 19:10:30 +01:00
2015-06-03 21:45:54 -07:00
module.exports = function* () {
let url = parse(this.request.body.url);
this.assert(url.host, 400, {error: {message: 'You need to submit a url.'}});
let item = yield lookup(this.request.body.url);
this.assert(item, 400, {error: {message: 'No supported music found at that link :('}});
item.matched_at = new Date(); // eslint-disable-line camelcase
let matches = {};
matches[item.service] = item;
2015-05-17 19:10:30 +01:00
2015-06-03 21:45:54 -07:00
for (let service of services) {
if (service.id === item.service) {
continue;
}
2015-06-03 21:45:54 -07:00
matches[service.id] = {service: service.id};
}
yield this.db.matches.save({_id: item.service + '$$' + item.id, 'created_at': new Date(), services: matches});
this.body = item;
process.nextTick(co.wrap(function* (){
for (let service of services) {
2015-05-17 19:10:30 +01:00
if (service.id === item.service) {
2015-06-03 21:45:54 -07:00
continue;
2015-01-06 12:58:57 +00:00
}
matches[service.id] = {service: service.id};
2015-06-03 21:45:54 -07:00
let match = yield service.search(item);
match.matched_at = new Date(); // eslint-disable-line camelcase
let update = {};
update['services.' + match.service] = match;
yield this.db.matches.updateOne({_id: item.service + '$$' + item.id}, {'$set': update});
}
}.bind(this)));
};