From 41750868e3cc45fc84383b460f298f128b73f4d7 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Wed, 15 Nov 2023 12:54:17 -0500 Subject: [PATCH] assemble decks from a text file and output all the required images into a folder --- Deck Lists/Starter Deck - Ruby & Emerald.txt | 28 ++++++++ assembleDeck.py | 68 ++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 Deck Lists/Starter Deck - Ruby & Emerald.txt create mode 100644 assembleDeck.py diff --git a/Deck Lists/Starter Deck - Ruby & Emerald.txt b/Deck Lists/Starter Deck - Ruby & Emerald.txt new file mode 100644 index 0000000..30ab604 --- /dev/null +++ b/Deck Lists/Starter Deck - Ruby & Emerald.txt @@ -0,0 +1,28 @@ +1 Cruella De Vil - Miserable As Usual +2 Aladdin - Prince Ali +2 Scar - Fiery Usurper +2 Donald Duck - Boisterous Fowl +3 Mickey Mouse - Steamboat Pilot +3 Sergeant Tibbs - Courageous Cat +1 Iago - Loud-Mouthed Parrot +3 Rapunzel - Letting Down Her Hair +2 Peter Pan - Never Landing +3 Pongo - Ol' Rascal +3 Aladdin - Street Rat +2 Duke Of Weselton - Opportunistic Official +3 Megara - Pulling the Strings +2 Jasper - Common Crook +3 Horace - No-Good Scoundrel +1 Lefou - Instigator +3 Mad Hatter - Gracious Host +2 Captain - Colonel's Lieutenant +1 Stitch - Abomination +1 Aladdin - Heroic Outlaw +3 Dragon Fire +2 Stampede +2 Vicious Betrayal +2 He's Got a Sword! +1 Steal from the Rich +3 Mother Knows Best +2 Stolen Scimitar +2 Shield of Virtue \ No newline at end of file diff --git a/assembleDeck.py b/assembleDeck.py new file mode 100644 index 0000000..6aa7736 --- /dev/null +++ b/assembleDeck.py @@ -0,0 +1,68 @@ +import csv +import os +import urllib.request + +# Function to download images +def download_image(image_url, folder_path, file_name): + image_path = os.path.join(folder_path, file_name) + urllib.request.urlretrieve(image_url, image_path) + +# List all .txt files in the Deck Lists folder +txt_files = [file for file in os.listdir('Deck Lists') if file.endswith('.txt')] + +# Display the list of .txt files and prompt for selection +print("Available text files:") +for index, file in enumerate(txt_files, start=1): + print(f"{index}. {file}") + +selection = input("Enter the number corresponding to the text file: ") + +# Validate the user input and extract the selected text file name +try: + selection_index = int(selection) + if 1 <= selection_index <= len(txt_files): + text_file_name = txt_files[selection_index - 1] + + # Extract the deck name from the text file name + deck_name = os.path.splitext(text_file_name)[0] + + # Read CSV file and store image URLs and quantities + image_urls = {} + with open('data.csv', newline='', encoding='utf-8') as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + name_title = f"{row['name']} - {row['title']}" if row['title'] else row['name'] + if name_title not in image_urls: + image_urls[name_title] = {'image': row['image'], 'qty': int(row['number'])} + + # Create output folder using the deck name + output_folder = f'images/{deck_name}' + os.makedirs(output_folder, exist_ok=True) + + # Read text file and organize images + with open(f'Deck Lists/{text_file_name}', 'r') as textfile: + for line in textfile: + line = line.strip() + parts = line.split(' - ') + if len(parts) >= 2: + quantity, name, title = parts[0].split()[0], ' '.join(parts[0].split()[1:]), parts[1] + else: + quantity, name = line.split()[0], ' '.join(line.split()[1:]) + title = None # Set title as None for entries without titles + + name_title = f"{name} - {title}" if title else name + + card_info = image_urls.get(name_title) + + if card_info: + qty_needed = int(quantity) + qty_available = card_info['qty'] + + # Download images based on quantity needed + for i in range(min(qty_needed, qty_available)): + file_name = f"{name_title} ({i + 1}).webp" + download_image(card_info['image'], output_folder, file_name) + else: + print("Invalid selection.") +except ValueError: + print("Invalid input. Please enter a number.")