diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c07ea0f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +pnpm-lock.yaml diff --git a/NOTES b/NOTES new file mode 100644 index 0000000..c5729c2 --- /dev/null +++ b/NOTES @@ -0,0 +1,10 @@ + +Root Path + +The root path for all requests in the XMLAPI2 are prefixed as follows. These queries can be made on any domain and are interchangeable. Example paths: + + https://boardgamegeek.com/xmlapi2/ + https://rpggeek.com/xmlapi2/ + https://videogamegeek.com/xmlapi2/ + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..a76f885 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "type":"module", + "dependencies": { + "cheerio": "1.0.0-rc.12", + "node-fetch": "^3.3.2", + "vitest": "^1.2.2" + } +} diff --git a/src/collection/example.js b/src/collection/example.js new file mode 100644 index 0000000..796da49 --- /dev/null +++ b/src/collection/example.js @@ -0,0 +1,18 @@ +const collection_example = ` + + One Deck Dungeon + 2016 https://cf.geekdo-images.com/quvyGpE7kwbd2Mt1H5iLwQ__original/img/pjwQVzsCHiXg8qFTZpVoJYx6SOE=/0x0/filters:format(jpeg)/pic3019101.jpg + https://cf.geekdo-images.com/quvyGpE7kwbd2Mt1H5iLwQ__thumb/img/Vf5PvYElsfOG28LqJimwPC6rqw4=/fit-in/200x150/filters:strip_icc()/pic3019101.jpg + + 6 text for trade goes here (testing) + + One Deck Dungeon: Forest of Shadows + 2017 https://cf.geekdo-images.com/rfkpP2id0JwDWw5694PRQQ__original/img/Od_smbHwsk-3Qw2HntZbU-oUg4I=/0x0/filters:format(jpeg)/pic3496794.jpg + https://cf.geekdo-images.com/rfkpP2id0JwDWw5694PRQQ__thumb/img/5A9VoUaZ1B5Wx1t0n74mfLBHW2o=/fit-in/200x150/filters:strip_icc()/pic3496794.jpg + + 1 + + +`; + +export default collection_example; diff --git a/src/fetchCollection.js b/src/fetchCollection.js new file mode 100644 index 0000000..af761f9 --- /dev/null +++ b/src/fetchCollection.js @@ -0,0 +1,44 @@ +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') ); diff --git a/src/fetchCollection.test.js b/src/fetchCollection.test.js new file mode 100644 index 0000000..8ead256 --- /dev/null +++ b/src/fetchCollection.test.js @@ -0,0 +1,8 @@ +import { test, expect } from 'vitest'; + +import collection_example from './collection/example.js' +import { extract_user_forsale } from './fetchCollection.js'; + +test( 'basic collection parsing', () => { + extract_user_forsale(collection_example); +} );