downloads are sorted based on artist name, filenames are stored instead of file indices

This commit is contained in:
Anthony 2024-09-25 21:44:38 -04:00
parent 82e50c64f0
commit 9dc407d510

View file

@ -3,6 +3,7 @@
import os import os
import time import time
import aiohttp import aiohttp
import re
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'
@ -23,7 +24,7 @@ async def download_image(session, image_url, file_path):
async def main(): async def main():
try: 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: async with session.get(url) as response:
if response.status != 200: if response.status != 200:
raise Exception(f"⛔ Failed to fetch JSON file: {response.status}") raise Exception(f"⛔ Failed to fetch JSON file: {response.status}")
@ -38,21 +39,30 @@ async def main():
os.makedirs(download_dir) os.makedirs(download_dir)
print(f"📁 Created directory: {download_dir}") print(f"📁 Created directory: {download_dir}")
file_index = 1 # file_index = 1 #Not used
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!") match = re.search(r'/content/([^/]+)/', image_url) # Extract artist name from URL
parsed_url = urlparse(image_url) if match:
ext = os.path.splitext(parsed_url.path)[-1] or '.jpg' artist_name = match.group(1)
filename = f"{file_index}{ext}" sanitized_artist_name = artist_name.split('_')[0].split('~')[1]
file_path = os.path.join(download_dir, filename) 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) file_name_match = re.search(r'/([^/]+)\.(jpg|png)', image_url) # Extract file name from URL
print(f"🖼️ Saved image to {file_path}") 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 download_image(session, image_url, file_path)
await delay(250) print(f"🖼️ Saved image to {file_path}")
await delay(250)
except Exception as e: except Exception as e:
print(f"Error: {str(e)}") print(f"Error: {str(e)}")