Skip to content

Instantly share code, notes, and snippets.

@grapswiz
Last active May 29, 2023 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grapswiz/cef42b7426bedf1e747598cb9aa715be to your computer and use it in GitHub Desktop.
Save grapswiz/cef42b7426bedf1e747598cb9aa715be to your computer and use it in GitHub Desktop.
fetch boardgame images from BGG
const Axios = require('axios');
const Path = require('path');
const Fs = require('fs');
const Url = require('url');
const OBJECT_ID = process.argv[2] || 230802;
const PAGE_NUMBER = process.argv[3] || 1;
const API_URL = `https://api.geekdo.com/api/images?ajax=1&gallery=all&nosession=1&objectid=${OBJECT_ID}&objecttype=thing&pageid=${PAGE_NUMBER}&showcount=50&size=thumb&sort=recent`;
const getJSON = async url => {
try {
const response = await Axios.get(url);
const data = response.data;
const LENGTH = data.images.length;
for (const [index, image] of data.images.entries()) {
console.log(`downloading ${index+1} / ${LENGTH} images.`);
await download((''+(index+1)).padStart(3, '0'), image.imageurl_lg);
}
console.log('done.');
} catch (e) {
}
};
const download = async (index, url) => {
const fileExtension = Path.extname(Url.parse(url).pathname);
const filePath = Path.resolve(__dirname, 'images', index + fileExtension);
const response = await Axios({
method: 'GET',
url: url,
responseType: 'stream'
});
response.data.pipe(Fs.createWriteStream(filePath));
return new Promise((resolve, reject) => {
response.data.on('end', () => {
resolve();
});
response.data.on('error', () => {
reject();
});
});
};
getJSON(API_URL);
{
"name": "bgg-image-fetch",
"version": "0.0.1",
"dependencies": {
"axios": "^0.18.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment