load imagges from local JSON instead of API call.

This commit is contained in:
SimplyJanDE 2024-10-09 11:28:51 +02:00
parent e38f3de6c8
commit 9d64df8423
2 changed files with 3685 additions and 33 deletions

3638
images.json Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,21 +1,34 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
async function main() { // Function to delay the downloads
const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s'; const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function main() {
const jsonFilePath = path.join(__dirname, 'images.json');
// Load local JSON file
let jsonData;
try { try {
const response = await fetch(url); if (!fs.existsSync(jsonFilePath)) {
if (!response.ok) { throw new Error('⛔ JSON file not found.');
throw new Error(`⛔ Failed to fetch JSON file: ${response.statusText}`);
} }
const jsonData = await response.json();
const jsonFileContent = fs.readFileSync(jsonFilePath, 'utf-8');
jsonData = JSON.parse(jsonFileContent);
console.info('📂 JSON file loaded successfully!');
} catch (error) {
console.error(`⛔ Failed to load JSON file: ${error.message}`);
return;
}
const data = jsonData.data; const data = jsonData.data;
if (!data) { if (!data) {
throw new Error('⛔ JSON does not have a "data" property at its root.'); console.error('⛔ JSON does not have a "data" property at its root.');
return;
} }
// Loop through each item in the JSON
for (const key in data) { for (const key in data) {
const subproperty = data[key]; const subproperty = data[key];
if (subproperty && subproperty.dhd) { if (subproperty && subproperty.dhd) {
@ -35,17 +48,18 @@ async function main() {
// Extract the filename and extension // Extract the filename and extension
const urlPath = new URL(imageUrl).pathname; const urlPath = new URL(imageUrl).pathname;
const fileName = path.basename(urlPath); // Filename including extension (e.g. .jpg or .png) const fileName = path.basename(urlPath); // Filename including extension (e.g., .jpg or .png)
const filePath = path.join(artistDir, fileName); const filePath = path.join(artistDir, fileName);
// Download the image and save it to the specified path // Download the image and save it to the specified path
try {
await downloadImage(imageUrl, filePath); await downloadImage(imageUrl, filePath);
console.info(`🖼️ Saved image to ${filePath}`); console.info(`🖼️ Saved image to ${filePath}`);
await delay(250); // Delay between downloads await delay(250); // Delay between downloads
} catch (err) {
console.error(`❌ Error downloading image: ${err.message}`);
} }
} }
} catch (error) {
console.error(`Error: ${error.message}`);
} }
} }
@ -74,7 +88,7 @@ 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...`);
} }
// Start program with ASCII art and delay // Start the program with ASCII art and delay
(() => { (() => {
asciiArt(); asciiArt();
setTimeout(main, 5000); setTimeout(main, 5000);