mirror of
https://github.com/nadimkobeissi/mkbsd.git
synced 2024-12-22 18:55:33 +00:00
Merge e38f3de6c8
into 82e50c64f0
This commit is contained in:
commit
70fdb5965e
116
mkbsd.js
116
mkbsd.js
|
@ -1,62 +1,68 @@
|
||||||
// Copyright 2024 Nadim Kobeissi
|
const fs = require('fs');
|
||||||
// Licensed under the WTFPL License
|
const path = require('path');
|
||||||
|
|
||||||
const fs = require(`fs`);
|
|
||||||
const path = require(`path`);
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s';
|
const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s';
|
||||||
const delay = (ms) => {
|
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
|
||||||
}
|
try {
|
||||||
try {
|
const response = await fetch(url);
|
||||||
const response = await fetch(url);
|
if (!response.ok) {
|
||||||
if (!response.ok) {
|
throw new Error(`⛔ Failed to fetch JSON file: ${response.statusText}`);
|
||||||
throw new Error(`⛔ Failed to fetch JSON file: ${response.statusText}`);
|
}
|
||||||
}
|
const jsonData = await response.json();
|
||||||
const jsonData = await response.json();
|
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.');
|
||||||
throw new Error('⛔ JSON does not have a "data" property at its root.');
|
}
|
||||||
}
|
|
||||||
const downloadDir = path.join(__dirname, 'downloads');
|
for (const key in data) {
|
||||||
if (!fs.existsSync(downloadDir)) {
|
const subproperty = data[key];
|
||||||
fs.mkdirSync(downloadDir);
|
if (subproperty && subproperty.dhd) {
|
||||||
console.info(`📁 Created directory: ${downloadDir}`);
|
const imageUrl = subproperty.dhd;
|
||||||
}
|
console.info(`🔍 Found image URL!`);
|
||||||
let fileIndex = 1;
|
|
||||||
for (const key in data) {
|
// Extract the artist name before the underscore
|
||||||
const subproperty = data[key];
|
const artistNameMatch = imageUrl.match(/a~([^_/]+)/);
|
||||||
if (subproperty && subproperty.dhd) {
|
const artistName = artistNameMatch ? artistNameMatch[1] : 'unknown_artist';
|
||||||
const imageUrl = subproperty.dhd;
|
const artistDir = path.join(__dirname, 'downloads', artistName);
|
||||||
console.info(`🔍 Found image URL!`);
|
|
||||||
await delay(100);
|
// Create artist directory if it doesn't exist
|
||||||
const ext = path.extname(new URL(imageUrl).pathname) || '.jpg';
|
if (!fs.existsSync(artistDir)) {
|
||||||
const filename = `${fileIndex}${ext}`;
|
fs.mkdirSync(artistDir, { recursive: true });
|
||||||
const filePath = path.join(downloadDir, filename);
|
console.info(`📁 Created directory: ${artistDir}`);
|
||||||
await downloadImage(imageUrl, filePath);
|
}
|
||||||
console.info(`🖼️ Saved image to ${filePath}`);
|
|
||||||
fileIndex++;
|
// Extract the filename and extension
|
||||||
await delay(250);
|
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);
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error: ${error.message}`);
|
// Download the image and save it to the specified path
|
||||||
}
|
await downloadImage(imageUrl, filePath);
|
||||||
|
console.info(`🖼️ Saved image to ${filePath}`);
|
||||||
|
await delay(250); // Delay between downloads
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error: ${error.message}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to download the image from the provided URL
|
||||||
async function downloadImage(url, filePath) {
|
async function downloadImage(url, filePath) {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Failed to download image: ${response.statusText}`);
|
throw new Error(`Failed to download image: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
const arrayBuffer = await response.arrayBuffer();
|
const arrayBuffer = await response.arrayBuffer();
|
||||||
const buffer = Buffer.from(arrayBuffer);
|
const buffer = Buffer.from(arrayBuffer);
|
||||||
await fs.promises.writeFile(filePath, buffer);
|
await fs.promises.writeFile(filePath, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ASCII art function (optional)
|
||||||
function asciiArt() {
|
function asciiArt() {
|
||||||
console.info(`
|
console.info(`
|
||||||
/$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$
|
/$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$
|
||||||
| $$$ /$$$| $$ /$$/| $$__ $$ /$$__ $$| $$__ $$
|
| $$$ /$$$| $$ /$$/| $$__ $$ /$$__ $$| $$__ $$
|
||||||
| $$$$ /$$$$| $$ /$$/ | $$ \\ $$| $$ \\__/| $$ \\ $$
|
| $$$$ /$$$$| $$ /$$/ | $$ \\ $$| $$ \\__/| $$ \\ $$
|
||||||
|
@ -65,11 +71,11 @@ function asciiArt() {
|
||||||
| $$\\ $ | $$| $$\\ $$ | $$ \\ $$ /$$ \\ $$| $$ | $$
|
| $$\\ $ | $$| $$\\ $$ | $$ \\ $$ /$$ \\ $$| $$ | $$
|
||||||
| $$ \\/ | $$| $$ \\ $$| $$$$$$$/| $$$$$$/| $$$$$$$/
|
| $$ \\/ | $$| $$ \\ $$| $$$$$$$/| $$$$$$/| $$$$$$$/
|
||||||
|__/ |__/|__/ \\__/|_______/ \\______/ |_______/`);
|
|__/ |__/|__/ \\__/|_______/ \\______/ |_______/`);
|
||||||
console.info(``);
|
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
|
||||||
(() => {
|
(() => {
|
||||||
asciiArt();
|
asciiArt();
|
||||||
setTimeout(main, 5000);
|
setTimeout(main, 5000);
|
||||||
})();
|
})();
|
||||||
|
|
27
mkbsd.py
27
mkbsd.py
|
@ -1,10 +1,9 @@
|
||||||
# Licensed under the WTFPL License
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import asyncio
|
import asyncio
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s'
|
url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s'
|
||||||
|
|
||||||
async def delay(ms):
|
async def delay(ms):
|
||||||
|
@ -33,25 +32,27 @@ async def main():
|
||||||
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.')
|
||||||
|
|
||||||
download_dir = os.path.join(os.getcwd(), 'downloads')
|
|
||||||
if not os.path.exists(download_dir):
|
|
||||||
os.makedirs(download_dir)
|
|
||||||
print(f"📁 Created directory: {download_dir}")
|
|
||||||
|
|
||||||
file_index = 1
|
|
||||||
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
|
||||||
parsed_url = urlparse(image_url)
|
parsed_url = urlparse(image_url)
|
||||||
ext = os.path.splitext(parsed_url.path)[-1] or '.jpg'
|
artist_name = image_url.split('a~')[1].split('_')[0]
|
||||||
filename = f"{file_index}{ext}"
|
artist_dir = os.path.join(os.getcwd(), 'downloads', artist_name)
|
||||||
file_path = os.path.join(download_dir, filename)
|
|
||||||
|
if not os.path.exists(artist_dir):
|
||||||
|
os.makedirs(artist_dir)
|
||||||
|
print(f"📁 Created directory: {artist_dir}")
|
||||||
|
|
||||||
|
# Extrahiere den Dateinamen und die Endung
|
||||||
|
filename = os.path.basename(parsed_url.path) # Name inklusive Endung
|
||||||
|
file_path = os.path.join(artist_dir, filename)
|
||||||
|
|
||||||
await download_image(session, image_url, file_path)
|
await download_image(session, image_url, file_path)
|
||||||
print(f"🖼️ Saved image to {file_path}")
|
print(f"🖼️ Saved image to {file_path}")
|
||||||
|
|
||||||
file_index += 1
|
|
||||||
await delay(250)
|
await delay(250)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Reference in a new issue