Last active
May 29, 2023 17:37
-
-
Save grapswiz/cef42b7426bedf1e747598cb9aa715be to your computer and use it in GitHub Desktop.
fetch boardgame images from BGG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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