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,51 +1,65 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
// Function to delay the downloads
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function main() { async function main() {
const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s'; const jsonFilePath = path.join(__dirname, 'images.json');
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// 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 data = jsonData.data;
if (!data) {
throw new Error('⛔ JSON does not have a "data" property at its root.');
} }
for (const key in data) { const jsonFileContent = fs.readFileSync(jsonFilePath, 'utf-8');
const subproperty = data[key]; jsonData = JSON.parse(jsonFileContent);
if (subproperty && subproperty.dhd) { console.info('📂 JSON file loaded successfully!');
const imageUrl = subproperty.dhd; } catch (error) {
console.info(`🔍 Found image URL!`); console.error(`⛔ Failed to load JSON file: ${error.message}`);
return;
}
// Extract the artist name before the underscore const data = jsonData.data;
const artistNameMatch = imageUrl.match(/a~([^_/]+)/); if (!data) {
const artistName = artistNameMatch ? artistNameMatch[1] : 'unknown_artist'; console.error('⛔ JSON does not have a "data" property at its root.');
const artistDir = path.join(__dirname, 'downloads', artistName); return;
}
// Create artist directory if it doesn't exist // Loop through each item in the JSON
if (!fs.existsSync(artistDir)) { for (const key in data) {
fs.mkdirSync(artistDir, { recursive: true }); const subproperty = data[key];
console.info(`📁 Created directory: ${artistDir}`); if (subproperty && subproperty.dhd) {
} const imageUrl = subproperty.dhd;
console.info(`🔍 Found image URL!`);
// Extract the filename and extension // Extract the artist name before the underscore
const urlPath = new URL(imageUrl).pathname; const artistNameMatch = imageUrl.match(/a~([^_/]+)/);
const fileName = path.basename(urlPath); // Filename including extension (e.g. .jpg or .png) const artistName = artistNameMatch ? artistNameMatch[1] : 'unknown_artist';
const filePath = path.join(artistDir, fileName); const artistDir = path.join(__dirname, 'downloads', artistName);
// Download the image and save it to the specified path // Create artist directory if it doesn't exist
if (!fs.existsSync(artistDir)) {
fs.mkdirSync(artistDir, { recursive: true });
console.info(`📁 Created directory: ${artistDir}`);
}
// Extract the filename and extension
const urlPath = new URL(imageUrl).pathname;
const fileName = path.basename(urlPath); // Filename including extension (e.g., .jpg or .png)
const filePath = path.join(artistDir, fileName);
// 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);