import fs from 'fs/promises'; import _ from 'lodash'; import Promise from 'bluebird'; import { PrismaClient } from '@prisma/client' import discogsService from './src/discogs'; import pgService from './src/pg'; import { logAxiosError } from './src/utils'; async function start() { const prisma = new PrismaClient(); const pg = pgService(prisma); const discogs = discogsService(pg); const usernames = [ 'margh', 'jmcdermid1', 'vanderbabe', ]; async function fetchListings(w) { return w.map(discogs.listings) } async function populateUserListings(user) { console.log(user); const wants = await discogs.wantlist(user); console.log(user.discogs, 'has', wants.length, 'wants'); for (const want of wants) { const listings = await discogs.listings(want); const populated = listings.filter(l => l); console.log(want.id, 'has', populated.length, '/', listings.length, 'listings'); } // const listings = await Promise // .all(releases.map(async want => { // const listings = (await getListings(want)).filter(l => l); // const insertions = await Promise.all(listings.map(async l => { // const listing = await pg.listingUpsert(l, want); // console.log(listing.id, listing.data.release.description, want.user, listing.seller); // return listing; // })); // console.log(insertions.length, want.id); // })); // return listings; } for (const u of usernames) { const user = await discogs.user(u) await populateUserListings(user); } // return Promise.map(releases, async w => { // try { // const l = await discogs.listings(w.id) // return l; // } catch (e) { // // console.error(e); // return null; // } // }) // .then(listings => _ // .chain(listings) // .filter(l => l) // .flatten() // // .map(l => ({ // // id: l.id, // // description: l.release.description, // // seller_id: l.seller.id, // // seller_name: l.seller.username, // // })) // // .groupBy(l => l.seller_id) // .value() // ); } function run() { start() .then(r => { console.log(r); setTimeout(run, 60 * 1000); }) .catch(e => { console.error(e); if (e.message === 'rate limited') { return setTimeout(run, 60 * 1000); } process.exit(1); }); } run();