This commit is contained in:
retardgerman 2024-09-28 04:46:06 +10:00 committed by GitHub
commit 70fdb5965e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 75 additions and 68 deletions

View file

@ -1,14 +1,10 @@
// 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) {
@ -19,25 +15,33 @@ async function main() {
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');
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir);
console.info(`📁 Created directory: ${downloadDir}`);
}
let fileIndex = 1;
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) {
const imageUrl = subproperty.dhd; const imageUrl = subproperty.dhd;
console.info(`🔍 Found image URL!`); console.info(`🔍 Found image URL!`);
await delay(100);
const ext = path.extname(new URL(imageUrl).pathname) || '.jpg'; // Extract the artist name before the underscore
const filename = `${fileIndex}${ext}`; const artistNameMatch = imageUrl.match(/a~([^_/]+)/);
const filePath = path.join(downloadDir, filename); 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); await downloadImage(imageUrl, filePath);
console.info(`🖼️ Saved image to ${filePath}`); console.info(`🖼️ Saved image to ${filePath}`);
fileIndex++; await delay(250); // Delay between downloads
await delay(250);
} }
} }
} catch (error) { } catch (error) {
@ -45,6 +49,7 @@ async function main() {
} }
} }
// 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) {
@ -55,6 +60,7 @@ async function downloadImage(url, filePath) {
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,10 +71,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...`);
} }
// Start program with ASCII art and delay
(() => { (() => {
asciiArt(); asciiArt();
setTimeout(main, 5000); setTimeout(main, 5000);

View file

@ -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: