full blown pg

This commit is contained in:
ntr committed 2022-07-26 00:42:47 +10:00
1 parent 245616acd4
commit dde9646179
13 files changed
+323 -175

No files matched your search

+83 -33
View File
@@ -3,41 +3,91 @@ import _ from 'lodash';
import Promise from 'bluebird';
import discogsService from './src/discogs';
import pgService from './src/pg';
import { logAxiosError } from './src/utils';
async function start() {
const discogs = discogsService();
const wants = JSON.parse(await fs.readFile('./data/margh.wantlist.json', { encoding: 'utf8' }))
console.log('wants', wants.length);
const pg = pgService();
const discogs = discogsService(pg);
return Promise.map(wants, 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)
.sortBy(s => s.length)
.reverse()
.head()
.value());
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;
}
return Promise.resolve(usernames)
.map(u => discogs.user(u))
.map(populateUserListings);
// 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()
// );
}
start()
.then(r => console.log(r))
.catch(e => {
console.error(e);
process.exit(1);
});
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();