mirror of
https://github.com/nadimkobeissi/mkbsd.git
synced 2024-12-23 00:55:20 +00:00
load imagges from local JSON instead of API call.
This commit is contained in:
parent
e38f3de6c8
commit
9d64df8423
3638
images.json
Normal file
3638
images.json
Normal file
File diff suppressed because it is too large
Load Diff
78
mkbsd.js
78
mkbsd.js
@ -1,51 +1,65 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// Function to delay the downloads
|
||||
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
async function main() {
|
||||
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 jsonFilePath = path.join(__dirname, 'images.json');
|
||||
|
||||
// Load local JSON file
|
||||
let jsonData;
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
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.');
|
||||
if (!fs.existsSync(jsonFilePath)) {
|
||||
throw new Error('⛔ JSON file not found.');
|
||||
}
|
||||
|
||||
for (const key in data) {
|
||||
const subproperty = data[key];
|
||||
if (subproperty && subproperty.dhd) {
|
||||
const imageUrl = subproperty.dhd;
|
||||
console.info(`🔍 Found image URL!`);
|
||||
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;
|
||||
}
|
||||
|
||||
// Extract the artist name before the underscore
|
||||
const artistNameMatch = imageUrl.match(/a~([^_/]+)/);
|
||||
const artistName = artistNameMatch ? artistNameMatch[1] : 'unknown_artist';
|
||||
const artistDir = path.join(__dirname, 'downloads', artistName);
|
||||
const data = jsonData.data;
|
||||
if (!data) {
|
||||
console.error('⛔ JSON does not have a "data" property at its root.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create artist directory if it doesn't exist
|
||||
if (!fs.existsSync(artistDir)) {
|
||||
fs.mkdirSync(artistDir, { recursive: true });
|
||||
console.info(`📁 Created directory: ${artistDir}`);
|
||||
}
|
||||
// Loop through each item in the JSON
|
||||
for (const key in data) {
|
||||
const subproperty = data[key];
|
||||
if (subproperty && subproperty.dhd) {
|
||||
const imageUrl = subproperty.dhd;
|
||||
console.info(`🔍 Found image URL!`);
|
||||
|
||||
// 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);
|
||||
// Extract the artist name before the underscore
|
||||
const artistNameMatch = imageUrl.match(/a~([^_/]+)/);
|
||||
const artistName = artistNameMatch ? artistNameMatch[1] : 'unknown_artist';
|
||||
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);
|
||||
console.info(`🖼️ Saved image to ${filePath}`);
|
||||
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...`);
|
||||
}
|
||||
|
||||
// Start program with ASCII art and delay
|
||||
// Start the program with ASCII art and delay
|
||||
(() => {
|
||||
asciiArt();
|
||||
setTimeout(main, 5000);
|
||||
|
Loading…
Reference in New Issue
Block a user