This commit is contained in:
Anthony Nikhil Reddy 2024-09-25 20:06:21 -07:00 committed by GitHub
commit cdfc1c7399
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 20 deletions

View file

@ -24,20 +24,36 @@ async function main() {
fs.mkdirSync(downloadDir); fs.mkdirSync(downloadDir);
console.info(`📁 Created directory: ${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!`, imageUrl);
await delay(100); await delay(100);
const ext = path.extname(new URL(imageUrl).pathname) || '.jpg';
const filename = `${fileIndex}${ext}`; const match = imageUrl.match(/\/content\/([^/]+)\//);
const filePath = path.join(downloadDir, filename); let artistName = match[1];
await downloadImage(imageUrl, filePath); artistName = artistName.replace(/^[a~]+|_[^_]+$/g, '');
console.info(`🖼️ Saved image to ${filePath}`); // Create folder with artist's name
fileIndex++; const artistDir = path.join(downloadDir, artistName);
await delay(250); if (!fs.existsSync(artistDir)) {
fs.mkdirSync(artistDir, { recursive: true });
}
// Extract and clean file name
const fileNameMatch = imageUrl.match(/\/([^/]+)\?/);
let cleanFileName = '';
if (fileNameMatch) {
let cleanFileName = fileNameMatch[1].replace(/~/g, ' ');
const filePath = path.join(artistDir, cleanFileName);
console.info('📂', filePath);
await downloadImage(imageUrl, filePath);
console.info(`🖼️ Saved image to ${filePath}`);
await delay(250);
}
} }
} }
} catch (error) { } catch (error) {

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,33 @@ 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'/([^/]+)\?', 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)
file_name = raw_file_name.split('.')[0]
file_extension = raw_file_name.split('.')[-1]
sanitized_file_name = file_name.replace('~', ' ')
file_path = os.path.join(artist_dir, f"{sanitized_file_name}." + file_extension)
# print(f"📄 File path: {file_path}")
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)}")