mirror of
https://github.com/nadimkobeissi/mkbsd.git
synced 2024-12-22 10:45:31 +00:00
Compare commits
5 commits
70fdb5965e
...
383ef1238b
Author | SHA1 | Date | |
---|---|---|---|
383ef1238b | |||
7dbdc490ae | |||
e96161c7c2 | |||
b0ce68033d | |||
9d64df8423 |
17
README.md
17
README.md
|
@ -18,15 +18,22 @@ MKBSD comes in two variants! Node.js and Python.
|
||||||
### Running in Node.js
|
### Running in Node.js
|
||||||
|
|
||||||
1. Ensure you have Node.js installed.
|
1. Ensure you have Node.js installed.
|
||||||
2. Run `node mkbsd.js`
|
2. Save the `images.json` file in the same directory as `mkbsd.js`.
|
||||||
3. Wait a little.
|
3. Run `node mkbsd.js`.
|
||||||
4. All wallpapers are now in a newly created `downloads` subfolder.
|
4. Wait a little.
|
||||||
|
5. All wallpapers are now in a newly created `downloads` subfolder.
|
||||||
|
|
||||||
|
### Changes Made in `mkbsd.js and mkbsd.py`
|
||||||
|
|
||||||
|
- **Local JSON Source**: The script was updated to read from a local `images.json` file instead of fetching data from a remote URL. This allows you to have full control over the data source.
|
||||||
|
- **File System Operations**: The script now uses Node.js's `fs` module to read and parse the JSON file directly.
|
||||||
|
- **Image Download Logic**: The image download logic was adapted to extract the artist's name and image URLs from the local JSON structure, ensuring the images are saved in artist-specific folders.
|
||||||
|
|
||||||
### Running in Python
|
### Running in Python
|
||||||
|
|
||||||
1. Ensure you have Python installed.
|
1. Ensure you have Python installed.
|
||||||
2. Ensure you have the `aiohttp` Python package installed (`pip install aiohttp`).
|
2. Save the `images.json` file in the same directory as `mkbsd.py`.
|
||||||
3. Run `python mkbsd.py`
|
3. Run `python mkbsd.py`.
|
||||||
4. Wait a little.
|
4. Wait a little.
|
||||||
5. All wallpapers are now in a newly created `downloads` subfolder.
|
5. All wallpapers are now in a newly created `downloads` subfolder.
|
||||||
|
|
||||||
|
|
3638
images.json
Normal file
3638
images.json
Normal file
File diff suppressed because it is too large
Load diff
42
mkbsd.js
42
mkbsd.js
|
@ -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 data = jsonData.data;
|
|
||||||
if (!data) {
|
|
||||||
throw new Error('⛔ JSON does not have a "data" property at its root.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
if (!data) {
|
||||||
|
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);
|
||||||
|
|
21
mkbsd.py
21
mkbsd.py
|
@ -2,9 +2,11 @@ import os
|
||||||
import time
|
import time
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s'
|
# Load the local JSON file
|
||||||
|
json_file_path = 'images.json'
|
||||||
|
|
||||||
async def delay(ms):
|
async def delay(ms):
|
||||||
await asyncio.sleep(ms / 1000)
|
await asyncio.sleep(ms / 1000)
|
||||||
|
@ -22,23 +24,22 @@ async def download_image(session, image_url, file_path):
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
try:
|
try:
|
||||||
async with aiohttp.ClientSession() as session:
|
# Load JSON data from the local file
|
||||||
async with session.get(url) as response:
|
with open(json_file_path, 'r') as json_file:
|
||||||
if response.status != 200:
|
json_data = json.load(json_file)
|
||||||
raise Exception(f"⛔ Failed to fetch JSON file: {response.status}")
|
|
||||||
json_data = await response.json()
|
|
||||||
data = json_data.get('data')
|
data = json_data.get('data')
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
raise Exception('⛔ JSON does not have a "data" property at its root.')
|
raise Exception('⛔ JSON does not have a "data" property at its root.')
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
for key, subproperty in data.items():
|
for key, subproperty in data.items():
|
||||||
if subproperty and subproperty.get('dhd'):
|
if subproperty and subproperty.get('dhd'):
|
||||||
image_url = subproperty['dhd']
|
image_url = subproperty['dhd']
|
||||||
print(f"🔍 Found image URL!")
|
print(f"🔍 Found image URL!")
|
||||||
|
|
||||||
# Extrahiere den Künstlernamen vor dem Unterstrich
|
# Extract artist name from the URL
|
||||||
parsed_url = urlparse(image_url)
|
|
||||||
artist_name = image_url.split('a~')[1].split('_')[0]
|
artist_name = image_url.split('a~')[1].split('_')[0]
|
||||||
artist_dir = os.path.join(os.getcwd(), 'downloads', artist_name)
|
artist_dir = os.path.join(os.getcwd(), 'downloads', artist_name)
|
||||||
|
|
||||||
|
@ -46,8 +47,8 @@ async def main():
|
||||||
os.makedirs(artist_dir)
|
os.makedirs(artist_dir)
|
||||||
print(f"📁 Created directory: {artist_dir}")
|
print(f"📁 Created directory: {artist_dir}")
|
||||||
|
|
||||||
# Extrahiere den Dateinamen und die Endung
|
# Extract filename from the URL
|
||||||
filename = os.path.basename(parsed_url.path) # Name inklusive Endung
|
filename = os.path.basename(urlparse(image_url).path) # Name including extension
|
||||||
file_path = os.path.join(artist_dir, filename)
|
file_path = os.path.join(artist_dir, filename)
|
||||||
|
|
||||||
await download_image(session, image_url, file_path)
|
await download_image(session, image_url, file_path)
|
||||||
|
|
Loading…
Reference in a new issue