Created
April 9, 2025 13:37
-
-
Save fox3000foxy/e0606c9cfdd979589b6ff24b6692e1bc to your computer and use it in GitHub Desktop.
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 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