2015-08-20 23:22:57 +01:00
|
|
|
import { parse } from 'url';
|
2017-10-23 17:58:23 +01:00
|
|
|
import kue from 'kue';
|
2017-10-23 22:47:14 +01:00
|
|
|
import debuglog from 'debug';
|
2016-10-03 13:31:29 +01:00
|
|
|
|
2015-06-03 21:45:54 -07:00
|
|
|
import lookup from '../lib/lookup';
|
|
|
|
import services from '../lib/services';
|
2017-10-23 19:26:49 +01:00
|
|
|
import { find, create } from '../lib/share';
|
2015-05-17 19:10:30 +01:00
|
|
|
|
2017-10-23 22:47:14 +01:00
|
|
|
const debug = debuglog('combine.fm:search');
|
|
|
|
|
2017-10-23 17:58:23 +01:00
|
|
|
const queue = kue.createQueue({
|
2017-10-23 19:26:49 +01:00
|
|
|
redis: process.env.REDIS_URL,
|
2017-10-23 17:58:23 +01:00
|
|
|
});
|
2015-08-21 18:33:50 +01:00
|
|
|
|
2016-10-03 13:31:29 +01:00
|
|
|
export default function* () {
|
2015-08-20 23:22:57 +01:00
|
|
|
const url = parse(this.request.body.url);
|
2017-10-23 22:47:14 +01:00
|
|
|
debug(`URL ${url}`);
|
2016-10-03 13:31:29 +01:00
|
|
|
this.assert(url.host, 400, { error: { message: 'You need to submit a url.' } });
|
2015-06-03 21:45:54 -07:00
|
|
|
|
2016-10-03 13:31:29 +01:00
|
|
|
const music = yield lookup(this.request.body.url);
|
2015-06-03 21:45:54 -07:00
|
|
|
|
2016-10-03 13:31:29 +01:00
|
|
|
this.assert(music, 400, { error: { message: 'No supported music found at that link :(' } });
|
2015-05-17 19:10:30 +01:00
|
|
|
|
2016-10-03 13:31:29 +01:00
|
|
|
let share = yield find(music);
|
2015-05-17 19:10:30 +01:00
|
|
|
|
2016-10-03 13:31:29 +01:00
|
|
|
if (!share) {
|
|
|
|
share = yield create(music);
|
2017-10-23 17:58:23 +01:00
|
|
|
|
2017-10-23 21:05:58 +01:00
|
|
|
for (const service of services) {
|
|
|
|
if (service.id === share.service) {
|
|
|
|
continue; // eslint-disable-line no-continue
|
|
|
|
}
|
2017-10-24 12:59:58 +01:00
|
|
|
const job = queue.create('search', { share: share, service: service }).save((err) => {
|
2017-10-23 21:05:58 +01:00
|
|
|
if (!err) console.log(job.id);
|
|
|
|
});
|
|
|
|
}
|
2015-06-03 21:45:54 -07:00
|
|
|
}
|
|
|
|
|
2016-10-03 13:31:29 +01:00
|
|
|
share = share.toJSON();
|
|
|
|
|
2017-05-07 22:55:42 +01:00
|
|
|
share.id = share.externalId;
|
|
|
|
|
2016-10-03 13:31:29 +01:00
|
|
|
const unmatched = services.filter(service =>
|
|
|
|
!share.matches.find(match => match.service === service.id));
|
|
|
|
|
|
|
|
share.matches = share.matches.concat(unmatched.map((service) => {
|
|
|
|
return {
|
|
|
|
service: service.id,
|
|
|
|
matching: true,
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
|
|
|
|
share.matches = share.matches.sort(a => !!a.externalId);
|
|
|
|
|
|
|
|
this.body = share;
|
|
|
|
}
|