2015-08-20 23:22:57 +01:00
|
|
|
import { parse } from 'url';
|
2015-06-03 21:45:54 -07:00
|
|
|
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* () {
|
2015-08-20 23:22:57 +01:00
|
|
|
const url = parse(this.request.body.url);
|
2015-06-03 21:45:54 -07:00
|
|
|
this.assert(url.host, 400, {error: {message: 'You need to submit a url.'}});
|
|
|
|
|
2015-08-20 23:22:57 +01:00
|
|
|
const item = yield lookup(this.request.body.url);
|
2015-06-03 21:45:54 -07:00
|
|
|
|
|
|
|
this.assert(item, 400, {error: {message: 'No supported music found at that link :('}});
|
|
|
|
|
|
|
|
item.matched_at = new Date(); // eslint-disable-line camelcase
|
2015-08-20 23:22:57 +01:00
|
|
|
const matches = {};
|
2015-06-03 21:45:54 -07:00
|
|
|
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-01-12 17:38:42 +00:00
|
|
|
}
|
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-08-20 23:22:57 +01:00
|
|
|
const match = yield service.search(item);
|
2015-06-03 21:45:54 -07:00
|
|
|
match.matched_at = new Date(); // eslint-disable-line camelcase
|
2015-08-20 23:22:57 +01:00
|
|
|
const update = {};
|
2015-06-03 21:45:54 -07:00
|
|
|
update['services.' + match.service] = match;
|
|
|
|
yield this.db.matches.updateOne({_id: item.service + '$$' + item.id}, {'$set': update});
|
|
|
|
}
|
|
|
|
}.bind(this)));
|
2014-12-04 22:50:30 +00:00
|
|
|
};
|