Skip to content

Instantly share code, notes, and snippets.

@grapswiz
Created July 27, 2023 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grapswiz/6314480c3ac4dcf9d2dd47079cc8d554 to your computer and use it in GitHub Desktop.
Save grapswiz/6314480c3ac4dcf9d2dd47079cc8d554 to your computer and use it in GitHub Desktop.
const COLLECTION_URL = 'https://boardgamegeek.com/xmlapi2/collection?username=grapswiz';
const THING_URL = 'https://boardgamegeek.com/xmlapi2/thing';
function myFunction() {
const collectionResponse = UrlFetchApp.fetch(COLLECTION_URL);
const xmlDocs = XmlService.parse(collectionResponse.getContentText());
const items = xmlDocs.getRootElement().getChildren('item');
const ids = items.map((item) => item.getAttribute('objectid').getValue());
Logger.log(ids.length);
const sliced = sliceByNumber(ids, 100);
const thingResponse = UrlFetchApp.fetch(`${THING_URL}?id=${sliced[0].join(',')}`);
const xmlDocsForThing = XmlService.parse(thingResponse.getContentText());
const thingItems = xmlDocsForThing.getRootElement().getChildren('item');
const output = thingItems.map(ti => {
const link = ti.getChildren('link');
const categories = [];
const mechanics = [];
const designers = [];
const artists = [];
const publishers = [];
for (const l of link) {
const type = l.getAttribute('type').getValue();
const value = l.getAttribute('value').getValue();
if (type === 'boardgamecategory') {
categories.push(value);
} else if (type === 'boardgamemechanic') {
mechanics.push(value);
} else if (type === 'boardgamedesigner') {
designers.push(value);
} else if (type === 'boardgameartist') {
artists.push(value);
} else if (type === 'boardgamepublisher') {
publishers.push(value);
}
}
return {
id: ti.getAttribute('id').getValue(),
name: ti.getChild('name').getAttribute('value').getValue(),
thumbnail: ti.getChild('thumbnail').getValue(),
minplayers: ti.getChild('minplayers').getAttribute('value').getValue(),
maxplayers: ti.getChild('maxplayers').getAttribute('value').getValue(),
playingtime: ti.getChild('playingtime').getAttribute('value').getValue(),
yearpublished: ti.getChild('yearpublished').getAttribute('value').getValue(),
categories: categories,
mechanics: mechanics,
designers: designers,
artists: artists,
publishers: publishers
};
});
Logger.log(output);
}
const sliceByNumber = (array, number) => {
const length = Math.ceil(array.length / number)
return new Array(length).fill().map((_, i) =>
array.slice(i * number, (i + 1) * number)
)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment