Added a prompt to choose the number of images

This commit is contained in:
huzfm 2024-10-02 10:21:50 +05:30
parent 82e50c64f0
commit b3032dc640
2 changed files with 55 additions and 12 deletions

View file

@ -1,14 +1,12 @@
// Copyright 2024 Nadim Kobeissi
// Licensed under the WTFPL License
const fs = require(`fs`); const fs = require(`fs`);
const path = require(`path`); const path = require(`path`);
const readline = require('readline');
async function main() { async function main(imageCount) {
const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s'; const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s';
const delay = (ms) => { const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
return new Promise(resolve => setTimeout(resolve, ms));
}
try { try {
const response = await fetch(url); const response = await fetch(url);
if (!response.ok) { if (!response.ok) {
@ -19,13 +17,24 @@ async function main() {
if (!data) { if (!data) {
throw new Error('⛔ JSON does not have a "data" property at its root.'); throw new Error('⛔ JSON does not have a "data" property at its root.');
} }
const totalFiles = Object.keys(data).length; // Count total available files
console.info(`📦 Total available files: ${totalFiles}`);
if (imageCount > totalFiles) {
console.warn(`⚠️ You requested ${imageCount} files, but only ${totalFiles} are available. Downloading all available files instead.`);
imageCount = totalFiles; // Limit to available files
}
const downloadDir = path.join(__dirname, 'downloads'); const downloadDir = path.join(__dirname, 'downloads');
if (!fs.existsSync(downloadDir)) { if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir); fs.mkdirSync(downloadDir);
console.info(`📁 Created directory: ${downloadDir}`); console.info(`📁 Created directory: ${downloadDir}`);
} }
let fileIndex = 1; let fileIndex = 1;
for (const key in data) { for (const key in data) {
if (fileIndex > imageCount) break; // Stop if we've reached the desired count
const subproperty = data[key]; const subproperty = data[key];
if (subproperty && subproperty.dhd) { if (subproperty && subproperty.dhd) {
const imageUrl = subproperty.dhd; const imageUrl = subproperty.dhd;
@ -69,7 +78,41 @@ function asciiArt() {
console.info(`🤑 Starting downloads from your favorite sellout grifter's wallpaper app...`); console.info(`🤑 Starting downloads from your favorite sellout grifter's wallpaper app...`);
} }
(() => { const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Function to start the download process
async function startDownload() {
const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s';
const response = await fetch(url);
const jsonData = await response.json();
const data = jsonData.data;
const totalFiles = Object.keys(data).length; // Count total available files
console.info(`📦 Total available files: ${totalFiles}`);
rl.question('How many images would you like to download? ', (answer) => {
let imageCount = parseInt(answer, 10);
if (isNaN(imageCount) || imageCount <= 0) {
console.error('Please enter a valid positive number.');
rl.close();
return;
}
if (imageCount > totalFiles) {
console.warn(`⚠️ You requested ${imageCount} files, but only ${totalFiles} are available. Downloading all available files instead.`);
imageCount = totalFiles; // Limit to available files
}
asciiArt(); asciiArt();
setTimeout(main, 5000); setTimeout(() => {
})(); main(imageCount).finally(() => rl.close());
}, 5000);
});
}
// Start the download process
startDownload();