Created
July 19, 2023 08:56
-
-
Save Aadv1k/fcd655f3189151166f86d00f0bf9cd60 to your computer and use it in GitHub Desktop.
Download images, given that a `data.json` file is present
This file contains 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 https = require('https'); | |
function downloadImage(url, destinationPath) { | |
if (!url) { | |
console.error('Error: URL is empty.'); | |
return; | |
} | |
https.get(url, (response) => { | |
const statusCode = response.statusCode; | |
if (statusCode !== 200) { | |
console.error('Error downloading image:', statusCode); | |
return; | |
} | |
const fileStream = fs.createWriteStream(destinationPath); | |
response.pipe(fileStream); | |
fileStream.on('finish', () => { | |
fileStream.close(); | |
console.log('Image downloaded successfully:', destinationPath); | |
}); | |
}).on('error', (err) => { | |
console.error('Error while downloading image:', err.message); | |
}); | |
} | |
function getRandomFilename(extension) { | |
const randomString = Math.random().toString(36).substr(2, 10); | |
return `data/${randomString}.${extension}`; | |
} | |
function extractImageUrls(post) { | |
const urls = []; | |
if (post.data?.is_gallery) { | |
const galleryImages = post.data?.gallery_data?.items || []; | |
galleryImages.forEach((image, idx) => { | |
const imageUrl = | |
image?.media_id || | |
image?.media?.gif?.source?.url || | |
image?.media?.reddit_video?.fallback_url; | |
if (imageUrl) { | |
urls.push(imageUrl); | |
} | |
}); | |
} else { | |
const url = post.data?.url || post.data?.url_overridden_by_dest; | |
urls.push(url); | |
} | |
return urls; | |
} | |
function downloadImages(data) { | |
data.data.children.forEach((post, index) => { | |
const imageUrls = extractImageUrls(post); | |
imageUrls.forEach((url) => { | |
const extension = url.substring(url.lastIndexOf('.') + 1).toLowerCase(); | |
if (!['jpg', 'jpeg', 'png'].includes(extension)) { | |
console.log('Skipping non-image URL:', url); | |
return; | |
} | |
const destinationPath = getRandomFilename(extension); | |
downloadImage(url, destinationPath); | |
}); | |
}); | |
} | |
fs.readFile('data.json', 'utf8', (err, data) => { | |
if (err) { | |
console.error('Error reading the JSON file:', err); | |
return; | |
} | |
try { | |
const jsonData = JSON.parse(data); | |
downloadImages(jsonData); | |
} catch (parseError) { | |
console.error('Error parsing JSON data:', parseError); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment