2024-01-30 20:01:27 +00:00
|
|
|
import { load } from 'cheerio';
|
|
|
|
import fetch from 'node-fetch';
|
2024-01-30 20:54:24 +00:00
|
|
|
import DB from 'better-sqlite3';
|
|
|
|
|
|
|
|
const db = DB('games.db');
|
|
|
|
db.pragma('journal_mode = WAL');
|
2024-01-30 20:01:27 +00:00
|
|
|
|
|
|
|
export function extract_user_forsale(page) {
|
|
|
|
|
|
|
|
const $ = load(page);
|
|
|
|
|
|
|
|
const games = [];
|
|
|
|
|
|
|
|
const data = $('item').map( function (i,item) {
|
|
|
|
const data = {};
|
2024-01-30 20:54:24 +00:00
|
|
|
data.bgg_id = $(this).attr('objectid');
|
|
|
|
data.name = $(this).find('name').text();
|
2024-01-30 20:01:27 +00:00
|
|
|
data.thumbnail = $(this).find('thumbnail').text();
|
|
|
|
data.notes = $(this).find('conditiontext').text();
|
|
|
|
const find_price = data.notes.match(/\$(\d+)/);
|
|
|
|
if(find_price ) data.price = find_price[1];
|
|
|
|
|
|
|
|
games.push(data);
|
|
|
|
})
|
|
|
|
|
|
|
|
return games;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetch_user_forsale(username,n=1) {
|
|
|
|
if(n>5) throw new Error("couldn't get collection");
|
|
|
|
|
|
|
|
const res = await fetch(`https://boardgamegeek.com/xmlapi2/collection?trade=1&username=${username}`);
|
|
|
|
|
|
|
|
if ( res.status === 202 ) {
|
|
|
|
return new Promise( (accept,reject) => {
|
|
|
|
setTimeout(()=> {
|
|
|
|
fetch_user_forsale(username,n+1).then(accept)
|
|
|
|
}, 2000)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if( res.status === 200 )
|
|
|
|
return extract_user_forsale( await res.text() );
|
|
|
|
|
|
|
|
throw new Error("couldn't get the collection");
|
|
|
|
}
|
|
|
|
|
2024-01-30 20:54:24 +00:00
|
|
|
async function update_user_games(username) {
|
|
|
|
const games = await fetch_user_forsale(username);
|
|
|
|
|
|
|
|
const insert = db.prepare( 'INSERT into game (username, bgg_id, name, thumbnail, notes) VALUES(@username,@bgg_id,@name,@thumbnail,@notes)' );
|
|
|
|
|
|
|
|
db.prepare('DELETE FROM game where username = @username').run({username});
|
|
|
|
|
|
|
|
for ( let game of games ) {
|
|
|
|
insert.run({
|
|
|
|
username,
|
|
|
|
...game
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log( await update_user_games('yenzie') );
|