import { load } from 'cheerio'; import fetch from 'node-fetch'; export function extract_user_forsale(page) { const $ = load(page); const games = []; const data = $('item').map( function (i,item) { const data = {}; data.game_id = $(this).attr('objectid'); data.game_name = $(this).find('name').text(); 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"); } console.log( await fetch_user_forsale('yenzie') );