combine.fm/fixmissing.js

74 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

import co from 'co';
2018-04-08 22:39:50 +01:00
import kue from 'kue';
import debuglog from 'debug';
import models from './models';
import services from './lib/services';
const debug = debuglog('combine.fm:fixmissing');
2018-04-08 22:39:50 +01:00
const queue = kue.createQueue({
redis: process.env.REDIS_URL,
});
debug('Fixing missing');
const serviceIds = [];
2018-04-30 22:20:01 +01:00
services.forEach((service) => {
serviceIds.push(service.id);
2018-04-30 22:20:01 +01:00
});
const query = {
include: [
{ model: models.artist },
{ model: models.match },
],
order: [
2018-04-08 17:22:19 +01:00
['createdAt', 'DESC'],
],
};
2018-04-08 22:39:50 +01:00
function search(data) {
2018-04-30 22:20:01 +01:00
const { share } = data;
const service = services.find(item => data.service.id === item.id);
2017-11-13 18:40:40 +00:00
debug(`Matching ${share.name} on ${data.service.id}`);
2018-04-08 22:39:50 +01:00
const job = queue.create('search-backlog', { title: `Matching ${share.name} on ${service.id}`, share, service })
.attempts(3)
.backoff({ type: 'exponential' })
.save((err) => {
debug(err || `JobID: ${job.id}`);
});
}
2017-11-13 18:40:40 +00:00
function* find(model) {
const items = yield models[model].findAll(query);
2018-04-30 22:20:01 +01:00
items.forEach((item) => {
2017-11-13 18:40:40 +00:00
let unmatched = serviceIds;
2018-04-30 22:20:01 +01:00
item.matches.forEach((match) => {
unmatched = unmatched.filter(id => match.service !== id);
2018-04-30 22:20:01 +01:00
});
if (unmatched.length > 0) {
debug(`Matching ${unmatched.join(', ')}`);
2018-04-30 22:20:01 +01:00
unmatched.forEach((toMatch) => {
2018-04-08 22:39:50 +01:00
search({ share: item, service: { id: toMatch } });
2018-04-30 22:20:01 +01:00
});
} else {
2017-11-13 18:40:40 +00:00
debug(`No broken matches for ${item.name}`);
}
2018-04-30 22:20:01 +01:00
});
2018-04-08 22:39:50 +01:00
return Promise.resolve();
2017-11-13 18:40:40 +00:00
}
co(function* main() {
yield find('album');
yield find('track');
2018-04-08 22:39:50 +01:00
setTimeout(() => {
process.exit(0);
}, 1000);
2017-11-13 18:40:40 +00:00
}).catch((err) => {
debug(err.stack);
});