75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
import fs from 'fs/promises';
|
|
import _ from 'lodash';
|
|
import Bunyan from 'bunyan';
|
|
import { PrismaClient } from '@prisma/client'
|
|
import Promise from "bluebird";
|
|
|
|
import discogsService from './src/discogs';
|
|
import pgService from './src/pg';
|
|
import { logAxiosError } from './src/utils';
|
|
import createLog from './src/log';
|
|
import createApi from './src/api';
|
|
|
|
async function start(log) {
|
|
const prisma = new PrismaClient();
|
|
const pg = pgService(log, prisma);
|
|
const discogs = discogsService(log, pg);
|
|
const api = createApi(log, pg);
|
|
|
|
const usernames = [
|
|
'vanderbabe',
|
|
'margh',
|
|
'jmcdermid1',
|
|
];
|
|
|
|
async function populateUserListings(user) {
|
|
log.info(user);
|
|
const wants = await discogs.wantlist(user);
|
|
|
|
// const latest = await discogs.wantlistLatest(user);
|
|
// if (wants.length !== latest.length)
|
|
log.info({ username: user.discogs, wantCount: wants.length });
|
|
|
|
return Promise.mapSeries(wants, async want => {
|
|
const listings = await discogs.listings(want);
|
|
const populated = listings.filter(l => l);
|
|
log.info({ id: want.id, release: want.release, populated: populated.length, total: listings.length });
|
|
return populated;
|
|
});
|
|
}
|
|
|
|
// await Promise.mapSeries(usernames, async u => {
|
|
// const user = await discogs.user(u)
|
|
// return populateUserListings(user);
|
|
// });
|
|
|
|
await api.listen();
|
|
|
|
// return getBundles();
|
|
}
|
|
|
|
async function run() {
|
|
const log = createLog();
|
|
|
|
process.on('SIGTERM', () => {
|
|
log.debug('SIGTERM received, exiting');
|
|
process.exit(0);
|
|
});
|
|
|
|
try {
|
|
const r = await start(log)
|
|
// log.info('done syncing', { totalListings: r.flat().length });
|
|
log.info(r);
|
|
setTimeout(run, 60 * 1000);
|
|
} catch (e) {
|
|
if (e.message === 'rate limited') {
|
|
log.info('rate limited, trying again in 60s');
|
|
return setTimeout(run, 60 * 1000);
|
|
}
|
|
log.error(e);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
run();
|