diff --git a/mkbsd.js b/mkbsd.js index d02593a..9e444c8 100644 --- a/mkbsd.js +++ b/mkbsd.js @@ -1,62 +1,68 @@ -// Copyright 2024 Nadim Kobeissi -// Licensed under the WTFPL License - -const fs = require(`fs`); -const path = require(`path`); +const fs = require('fs'); +const path = require('path'); async function main() { - const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s'; - const delay = (ms) => { - return new Promise(resolve => setTimeout(resolve, ms)); - } - 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.'); - } - const downloadDir = path.join(__dirname, 'downloads'); - if (!fs.existsSync(downloadDir)) { - fs.mkdirSync(downloadDir); - console.info(`πŸ“ Created directory: ${downloadDir}`); - } - let fileIndex = 1; - for (const key in data) { - const subproperty = data[key]; - if (subproperty && subproperty.dhd) { - const imageUrl = subproperty.dhd; - console.info(`πŸ” Found image URL!`); - await delay(100); - const ext = path.extname(new URL(imageUrl).pathname) || '.jpg'; - const filename = `${fileIndex}${ext}`; - const filePath = path.join(downloadDir, filename); - await downloadImage(imageUrl, filePath); - console.info(`πŸ–ΌοΈ Saved image to ${filePath}`); - fileIndex++; - await delay(250); - } - } - } catch (error) { - console.error(`Error: ${error.message}`); - } + const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s'; + const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); + + 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.'); + } + + for (const key in data) { + const subproperty = data[key]; + if (subproperty && subproperty.dhd) { + const imageUrl = subproperty.dhd; + console.info(`πŸ” Found image URL!`); + + // 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); + + // 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 + 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) { - const response = await fetch(url); - if (!response.ok) { - throw new Error(`Failed to download image: ${response.statusText}`); - } - const arrayBuffer = await response.arrayBuffer(); - const buffer = Buffer.from(arrayBuffer); - await fs.promises.writeFile(filePath, buffer); + const response = await fetch(url); + if (!response.ok) { + throw new Error(`Failed to download image: ${response.statusText}`); + } + const arrayBuffer = await response.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + await fs.promises.writeFile(filePath, buffer); } +// ASCII art function (optional) 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(); - setTimeout(main, 5000); + asciiArt(); + setTimeout(main, 5000); })(); diff --git a/mkbsd.py b/mkbsd.py index 2b33310..4b8977e 100644 --- a/mkbsd.py +++ b/mkbsd.py @@ -1,10 +1,9 @@ -# Licensed under the WTFPL License - import os import time import aiohttp import asyncio from urllib.parse import urlparse + url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s' async def delay(ms): @@ -33,25 +32,27 @@ async def main(): if not data: 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(): if subproperty and subproperty.get('dhd'): image_url = subproperty['dhd'] print(f"πŸ” Found image URL!") + + # Extrahiere den KΓΌnstlernamen vor dem Unterstrich parsed_url = urlparse(image_url) - ext = os.path.splitext(parsed_url.path)[-1] or '.jpg' - filename = f"{file_index}{ext}" - file_path = os.path.join(download_dir, filename) + artist_name = image_url.split('a~')[1].split('_')[0] + artist_dir = os.path.join(os.getcwd(), 'downloads', artist_name) + + 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) print(f"πŸ–ΌοΈ Saved image to {file_path}") - - file_index += 1 + await delay(250) except Exception as e: