From 9dc407d510fc3c95c2a59299863782ace4086885 Mon Sep 17 00:00:00 2001 From: Anthony Date: Wed, 25 Sep 2024 21:44:38 -0400 Subject: [PATCH] downloads are sorted based on artist name, filenames are stored instead of file indices --- mkbsd.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/mkbsd.py b/mkbsd.py index 2b33310..8ecabfb 100644 --- a/mkbsd.py +++ b/mkbsd.py @@ -3,6 +3,7 @@ import os import time import aiohttp +import re import asyncio from urllib.parse import urlparse url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s' @@ -23,7 +24,7 @@ async def download_image(session, image_url, file_path): async def main(): try: - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session: # Ignore SSL errors async with session.get(url) as response: if response.status != 200: raise Exception(f"⛔ Failed to fetch JSON file: {response.status}") @@ -38,21 +39,30 @@ async def main(): os.makedirs(download_dir) print(f"📁 Created directory: {download_dir}") - file_index = 1 + # file_index = 1 #Not used 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) - ext = os.path.splitext(parsed_url.path)[-1] or '.jpg' - filename = f"{file_index}{ext}" - file_path = os.path.join(download_dir, filename) + match = re.search(r'/content/([^/]+)/', image_url) # Extract artist name from URL + if match: + artist_name = match.group(1) + sanitized_artist_name = artist_name.split('_')[0].split('~')[1] + print(f"🎨 Sanitized artist name: {sanitized_artist_name}") + artist_dir = os.path.join(download_dir, sanitized_artist_name) + if not os.path.exists(artist_dir): + os.makedirs(artist_dir) + print(f"📁 Created artist directory: {artist_dir}") - await download_image(session, image_url, file_path) - print(f"🖼️ Saved image to {file_path}") + file_name_match = re.search(r'/([^/]+)\.(jpg|png)', image_url) # Extract file name from URL + if file_name_match: + raw_file_name = file_name_match.group(1) + sanitized_file_name = re.sub(r'[^a-zA-Z0-9 ]', '', raw_file_name).replace('~', ' ') + file_path = os.path.join(artist_dir, f"{sanitized_file_name}.jpg") - file_index += 1 - await delay(250) + await download_image(session, image_url, file_path) + print(f"🖼️ Saved image to {file_path}") + + await delay(250) except Exception as e: print(f"Error: {str(e)}")