From 2c4f79c3076977ffa1028a63cbec2a4ceb2d9c4a Mon Sep 17 00:00:00 2001 From: retardgerman <78982850+retardgerman@users.noreply.github.com> Date: Fri, 27 Sep 2024 10:54:28 +0000 Subject: [PATCH 1/4] files being renamed while downloaded --- mkbsd.js | 106 ++++++++++++++++++++++++++----------------------------- 1 file changed, 51 insertions(+), 55 deletions(-) diff --git a/mkbsd.js b/mkbsd.js index d02593a..466aa5a 100644 --- a/mkbsd.js +++ b/mkbsd.js @@ -1,62 +1,59 @@ -// 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.'); + } + const downloadDir = path.join(__dirname, 'downloads'); + if (!fs.existsSync(downloadDir)) { + fs.mkdirSync(downloadDir); + console.info(`๐Ÿ“ Created directory: ${downloadDir}`); + } + + for (const key in data) { + const subproperty = data[key]; + if (subproperty && subproperty.dhd) { + const imageUrl = subproperty.dhd; + console.info(`๐Ÿ” Found image URL!`); + + // Extrahiere den Dateinamen aus der URL + const urlPath = new URL(imageUrl).pathname; + const fileName = path.basename(urlPath, '.jpg'); // Name ohne '.jpg' + const filePath = path.join(downloadDir, `${fileName}.jpg`); + + await downloadImage(imageUrl, filePath); + console.info(`๐Ÿ–ผ๏ธ Saved image to ${filePath}`); + await delay(250); // Wartezeit zwischen Downloads + } + } + } catch (error) { + console.error(`Error: ${error.message}`); + } } 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); } function asciiArt() { - console.info(` + console.info(` /$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$ | $$$ /$$$| $$ /$$/| $$__ $$ /$$__ $$| $$__ $$ | $$$$ /$$$$| $$ /$$/ | $$ \\ $$| $$ \\__/| $$ \\ $$ @@ -65,11 +62,10 @@ 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...`); } (() => { - asciiArt(); - setTimeout(main, 5000); + asciiArt(); + setTimeout(main, 5000); })(); From 7fa0915766e2316e41471c65b1ae8358683f144f Mon Sep 17 00:00:00 2001 From: retardgerman <78982850+retardgerman@users.noreply.github.com> Date: Fri, 27 Sep 2024 11:00:50 +0000 Subject: [PATCH 2/4] files are renamed during download. --- mkbsd.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mkbsd.py b/mkbsd.py index 2b33310..6b2db59 100644 --- a/mkbsd.py +++ b/mkbsd.py @@ -5,6 +5,7 @@ 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): @@ -38,20 +39,19 @@ async def main(): os.makedirs(download_dir) print(f"๐Ÿ“ Created directory: {download_dir}") - file_index = 1 - for key, subproperty in data.items(): + for file_index, (key, subproperty) in enumerate(data.items(), start=1): if subproperty and subproperty.get('dhd'): image_url = subproperty['dhd'] print(f"๐Ÿ” Found image URL!") 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) + + # Extrahiere den Dateinamen ohne .jpg + filename = os.path.basename(parsed_url.path).replace('.jpg', '') or f'image_{file_index}' + file_path = os.path.join(download_dir, f"{filename}.jpg") 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: From 7ea8a0fb9466e43f56af91ab23079ba818ca845b Mon Sep 17 00:00:00 2001 From: SimplyJanDE <78982850+SimplyJanDE@users.noreply.github.com> Date: Fri, 27 Sep 2024 13:27:32 +0200 Subject: [PATCH 3/4] Refactor image download logic to group images by artist name - Extract artist name from the URL by removing the unique ID suffix. - Ensure all images from the same artist (e.g., 'justinmaller') are saved in a single folder. - Support dynamic file extensions (e.g., .jpg, .png) when saving images. - Created artist-specific directories based on the artist's name, ensuring proper organization of images. --- mkbsd.js | 23 ++++++++++++++--------- mkbsd.py | 27 ++++++++++++++------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/mkbsd.js b/mkbsd.js index 466aa5a..d45c542 100644 --- a/mkbsd.js +++ b/mkbsd.js @@ -4,7 +4,7 @@ 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) => new Promise(resolve => setTimeout(resolve, ms)); - + try { const response = await fetch(url); if (!response.ok) { @@ -15,11 +15,6 @@ async function main() { 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}`); - } for (const key in data) { const subproperty = data[key]; @@ -27,10 +22,20 @@ async function main() { const imageUrl = subproperty.dhd; console.info(`๐Ÿ” Found image URL!`); - // Extrahiere den Dateinamen aus der URL + // Extrahiere den Kรผnstlernamen vor dem Unterstrich + const artistNameMatch = imageUrl.match(/a~([^_/]+)/); + const artistName = artistNameMatch ? artistNameMatch[1] : 'unknown_artist'; + const artistDir = path.join(__dirname, 'downloads', artistName); + + if (!fs.existsSync(artistDir)) { + fs.mkdirSync(artistDir, { recursive: true }); + console.info(`๐Ÿ“ Created directory: ${artistDir}`); + } + + // Extrahiere den Dateinamen und die Endung const urlPath = new URL(imageUrl).pathname; - const fileName = path.basename(urlPath, '.jpg'); // Name ohne '.jpg' - const filePath = path.join(downloadDir, `${fileName}.jpg`); + const fileName = path.basename(urlPath); // Name inklusive Endung (z.B. .jpg oder .png) + const filePath = path.join(artistDir, fileName); await downloadImage(imageUrl, filePath); console.info(`๐Ÿ–ผ๏ธ Saved image to ${filePath}`); diff --git a/mkbsd.py b/mkbsd.py index 6b2db59..4b8977e 100644 --- a/mkbsd.py +++ b/mkbsd.py @@ -1,5 +1,3 @@ -# Licensed under the WTFPL License - import os import time import aiohttp @@ -34,24 +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}") - - for file_index, (key, subproperty) in enumerate(data.items(), start=1): + for key, subproperty in data.items(): if subproperty and subproperty.get('dhd'): image_url = subproperty['dhd'] print(f"๐Ÿ” Found image URL!") - parsed_url = urlparse(image_url) - # Extrahiere den Dateinamen ohne .jpg - filename = os.path.basename(parsed_url.path).replace('.jpg', '') or f'image_{file_index}' - file_path = os.path.join(download_dir, f"{filename}.jpg") + # Extrahiere den Kรผnstlernamen vor dem Unterstrich + parsed_url = urlparse(image_url) + 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}") - + await delay(250) except Exception as e: From e38f3de6c86d1bfa14eaeb2c98e5b27162e8e794 Mon Sep 17 00:00:00 2001 From: retardgerman <78982850+retardgerman@users.noreply.github.com> Date: Fri, 27 Sep 2024 20:36:04 +0200 Subject: [PATCH 4/4] Refactor code comments to English for better clarity and understanding --- mkbsd.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mkbsd.js b/mkbsd.js index d45c542..9e444c8 100644 --- a/mkbsd.js +++ b/mkbsd.js @@ -22,24 +22,26 @@ async function main() { const imageUrl = subproperty.dhd; console.info(`๐Ÿ” Found image URL!`); - // Extrahiere den Kรผnstlernamen vor dem Unterstrich + // 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}`); } - // Extrahiere den Dateinamen und die Endung + // Extract the filename and extension const urlPath = new URL(imageUrl).pathname; - const fileName = path.basename(urlPath); // Name inklusive Endung (z.B. .jpg oder .png) + 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); // Wartezeit zwischen Downloads + await delay(250); // Delay between downloads } } } catch (error) { @@ -47,6 +49,7 @@ async function main() { } } +// Function to download the image from the provided URL async function downloadImage(url, filePath) { const response = await fetch(url); if (!response.ok) { @@ -57,6 +60,7 @@ async function downloadImage(url, filePath) { await fs.promises.writeFile(filePath, buffer); } +// ASCII art function (optional) function asciiArt() { console.info(` /$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$ @@ -70,6 +74,7 @@ function asciiArt() { console.info(`๐Ÿค‘ Starting downloads from your favorite sellout grifter's wallpaper app...`); } +// Start program with ASCII art and delay (() => { asciiArt(); setTimeout(main, 5000);