name folder set names specefied by

This commit is contained in:
0ceanSlim 2023-11-14 11:30:28 -05:00
parent 4411576f07
commit 85d6e42be9
3 changed files with 17 additions and 4 deletions

View File

@ -2,11 +2,16 @@ import os
import pandas as pd import pandas as pd
import requests import requests
import re import re
import json
# Function to clean filename by removing problematic characters # Function to clean filename by removing problematic characters
def clean_filename(filename): def clean_filename(filename):
return re.sub(r'[\\/*?:"<>|]', '', filename) return re.sub(r'[\\/*?:"<>|]', '', filename)
# Load set names from JSON file
with open('sets.json', 'r') as file:
set_names_mapping = json.load(file)
# Read the CSV file # Read the CSV file
data = pd.read_csv('data.csv') data = pd.read_csv('data.csv')
@ -17,19 +22,22 @@ os.makedirs(images_folder, exist_ok=True)
# Iterate through the rows # Iterate through the rows
for index, row in data.iterrows(): for index, row in data.iterrows():
card_set_id = row['card_set_id'] card_set_id = row['card_set_id']
card_id = row['id'] set_id = row['number']
name = row['name'] name = row['name']
image_url = row['image'] image_url = row['image']
# Create a folder for the card_set_id if it doesn't exist # Get set name from mapping
card_set_folder = os.path.join(images_folder, str(card_set_id)) set_name = set_names_mapping.get(str(card_set_id), f"set_{card_set_id}")
# Create a folder for the set_name if it doesn't exist
card_set_folder = os.path.join(images_folder, set_name)
os.makedirs(card_set_folder, exist_ok=True) os.makedirs(card_set_folder, exist_ok=True)
# Get the image # Get the image
response = requests.get(image_url) response = requests.get(image_url)
if response.status_code == 200: if response.status_code == 200:
# Clean the filename # Clean the filename
image_filename = clean_filename(f"{card_id} - {name}.webp") # Adjust the extension if needed image_filename = clean_filename(f"{set_id} - {name}.webp") # Adjust the extension if needed
image_path = os.path.join(card_set_folder, image_filename) image_path = os.path.join(card_set_folder, image_filename)
with open(image_path, 'wb') as f: with open(image_path, 'wb') as f:
f.write(response.content) f.write(response.content)

5
sets.json Normal file
View File

@ -0,0 +1,5 @@
{
"1": "0 - Promo",
"2": "1 - The First Chapter",
"3": "2 - Rise of the Floodborn"
}