Files
legolog/db/imagestealer.js
Ben 925af9cc9d databases
Former-commit-id: 5bc8a1b761fe2241c32e51b30b6c0e0dfdeb3461
2022-02-03 18:55:09 +00:00

65 lines
2.1 KiB
JavaScript

// https://www.bricklink.com/v2/catalog/catalogitem.page?P=bb0031#T=C
// download image from bricklink for every item in the tab-delimited database Parts.txt
const fs = require('fs');
const axios = require('axios');
// For sets make sets.txt
const parts = fs.readFileSync('res/Sets.txt', 'utf8').toString().split('\n').map((i) => i.split('\t'));
async function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function downloadImage(url, filename) {
try {
const res = await axios.get(url, {
method: 'GET',
headers: { 'User-Agent':'Chrome/96.0.4664.175' } ,
responseType: 'stream'
});
if (!res.data) {
console.log(`${filename} failed to start downloading`);
fs.appendFileSync('error.txt', `${url}\n`);
return;
}
return new Promise((resolve, reject) => {
res.data.pipe(fs.createWriteStream(filename));
res.data.on('end', () => {
console.log('downloaded file ' + filename);
fs.appendFileSync('saved.txt', `${url}\n`);
resolve();
})
res.data.on('error', () => {
console.log('error downloading file ' + filename);
fs.appendFileSync('error.txt', `${url}\n`);
resolve();
})
});
} catch (e) {
console.log(`${filename} failed to start downloading`);
fs.appendFileSync('error.txt', `${url}\n`);
return;
}
}
async function main() {
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
// for sets use https://img.bricklink.com/ItemImage/SL/${part[2]}.png
// for for pieces use https://img.bricklink.com/ItemImage/PL/${part[2]}.png
const url = `https://img.bricklink.com/ItemImage/SL/${part[2]}.png`;
const filename = `res/image/${part[2]}.png`;
await downloadImage(url, filename);
// await timeout(10); // let's not get rate limited
console.log(`${i}/${parts.length} ${url}`);
}
}
main();