This commit is contained in:
ntr committed 2022-07-25 01:09:22 +10:00
1 parent f0e204d70b
commit 76dc840cf7
11 files changed
+629 -40

No files matched your search

+41
View File
@@ -1,8 +1,23 @@
import fs from 'fs/promises';
import axios from 'axios';
import cheerio from 'cheerio';
import sellers from './../sellers';
export async function exists(path) {
try {
await fs.stat(path);
return true;
} catch (e) {
return false;
}
}
export default function discogs() {
const TOKEN = 'wRTXuYZGOPsIQgKRGKCAkUkidGtvpSfQgPWkYIpD';
const apiBase = 'https://api.discogs.com'
const discogsBase = 'https://www.discogs.com'
const api = axios.create({
headers: {
@@ -20,8 +35,34 @@ export default function discogs() {
return res.data.listings;
}
async function listings(release: number) {
const res = await api.get(`https://www.discogs.com/sell/release/${release}`);
const $ = cheerio.load(res.data);
const ids = $('.item_description_title[href]')
.map(function getListingId() {
const id = Number.parseInt(this.attribs.href.replace('/sell/item/', ''), 10);
return id;
})
.get();
return Promise.all(ids.map(id => listing(id)));
}
async function listing(id) {
const path = `./data/listings/${id}.json`;
if (await exists(path)) {
// console.log('cached', id);
return JSON.parse(await fs.readFile(path, { encoding: 'utf8' }));
}
// console.log('fetching', id);
const res = await api.get(`${apiBase}/marketplace/listings/${id}`);
await fs.writeFile(path, JSON.stringify(res.data, null, 4));
return res.data;
}
return {
wantlist,
inventory,
listings,
listing,
};
}
+4
View File
@@ -0,0 +1,4 @@
export default function createPg() {
return {
};
}