Skip to content

Instantly share code, notes, and snippets.

@cccaaannn
Created August 13, 2024 00:11
Show Gist options
  • Save cccaaannn/05ab87bc04c98b31e689bb801f76af69 to your computer and use it in GitHub Desktop.
Save cccaaannn/05ab87bc04c98b31e689bb801f76af69 to your computer and use it in GitHub Desktop.
Download images with node
const https = require('https');
const fs = require('fs');
const imageUrls = [
]
const padFileName = (index) => {
let str = index.toString();
while (str.length < 3) {
str = '0' + str;
}
return str;
}
(() => {
let index = 1;
for (const url of imageUrls) {
const fileName = `img/${padFileName(index)}.png`;
const file = fs.createWriteStream(fileName);
https.get(url, response => {
response.pipe(file);
file.on('finish', () => {
file.close();
console.log(`Downloaded: ${fileName}`);
});
}).on('error', err => {
fs.unlink(fileName);
console.error(`Error: ${fileName} - ${err.message}`);
});
index++;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment