Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a prompt for users to choose how many files to download. #78

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions README.md

This file was deleted.

64 changes: 54 additions & 10 deletions mkbsd.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright 2024 Nadim Kobeissi
// Licensed under the WTFPL License


const fs = require(`fs`);
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 delay = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

try {
const response = await fetch(url);
if (!response.ok) {
Expand All @@ -19,13 +18,24 @@ async function main() {
if (!data) {
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');
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir);
console.info(`📁 Created directory: ${downloadDir}`);
}

let fileIndex = 1;
for (const key in data) {
if (fileIndex > imageCount) break; // Stop if we've reached the desired count
const subproperty = data[key];
if (subproperty && subproperty.dhd) {
const imageUrl = subproperty.dhd;
Expand Down Expand Up @@ -69,7 +79,41 @@ function asciiArt() {
console.info(`🤑 Starting downloads from your favorite sellout grifter's wallpaper app...`);
}

(() => {
asciiArt();
setTimeout(main, 5000);
})();
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();
setTimeout(() => {
main(imageCount).finally(() => rl.close());
}, 5000);
});
}

// Start the download process
startDownload();