mirror of
https://github.com/nadimkobeissi/mkbsd.git
synced 2025-04-20 16:06:32 -04:00
Merge ebbb5520ef
into 82e50c64f0
This commit is contained in:
commit
cdfc1c7399
2 changed files with 49 additions and 20 deletions
34
mkbsd.js
34
mkbsd.js
|
@ -24,20 +24,36 @@ async function main() {
|
|||
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!`);
|
||||
console.info(`🔍 Found image URL!`, imageUrl);
|
||||
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);
|
||||
|
||||
const match = imageUrl.match(/\/content\/([^/]+)\//);
|
||||
let artistName = match[1];
|
||||
artistName = artistName.replace(/^[a~]+|_[^_]+$/g, '');
|
||||
// Create folder with artist's name
|
||||
const artistDir = path.join(downloadDir, artistName);
|
||||
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) {
|
||||
|
|
35
mkbsd.py
35
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,33 @@ 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'/([^/]+)\?', image_url) # Extract file name from URL
|
||||
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 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)}")
|
||||
|
|
Loading…
Add table
Reference in a new issue