Skip to content

Instantly share code, notes, and snippets.

@fox3000foxy
Created April 9, 2025 13:37
Show Gist options
  • Save fox3000foxy/e0606c9cfdd979589b6ff24b6692e1bc to your computer and use it in GitHub Desktop.
Save fox3000foxy/e0606c9cfdd979589b6ff24b6692e1bc to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const axios = require('axios');
// Load JSON file with URLs
const urls = require('./urls.json'); // Adjust the path to your JSON file
// Directory to save downloaded files
const outputDir = path.join(__dirname, 'downloads');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
async function downloadFile(url, index) {
try {
const response = await axios.get(url, { responseType: 'stream' });
const fileName = path.basename(new URL(url).pathname) || `file_${index}`;
const filePath = path.join(outputDir, fileName);
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => {
console.log(`βœ… Downloaded: ${fileName}`);
resolve();
});
writer.on('error', reject);
});
} catch (err) {
console.error(`❌ Failed to download ${url}:`, err.message);
}
}
async function downloadAll() {
console.log(`πŸš€ Starting download of ${urls.length} files...`);
for (let i = 0; i < urls.length; i++) {
await downloadFile(urls[i], i);
}
console.log('πŸŽ‰ All downloads completed!');
}
downloadAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment