initial push

This commit is contained in:
Caleb Fultz 2024-08-13 15:37:13 -04:00
commit df1659b3ef
6 changed files with 136 additions and 0 deletions

0
README.md Normal file
View File

4
cards.csv Normal file
View File

@ -0,0 +1,4 @@
Card Name,Set Code,Collector Number,Quantity,Foil
"Linda, Kandarian Queen",sld,1355,1,True
"Vraska, Golgari Queen",sld,1702,1,False
"Vraska, Golgari Queen",sld,1702,1,True
1 Card Name Set Code Collector Number Quantity Foil
2 Linda, Kandarian Queen sld 1355 1 True
3 Vraska, Golgari Queen sld 1702 1 False
4 Vraska, Golgari Queen sld 1702 1 True

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

60
mtgcardcsv.py Normal file
View File

@ -0,0 +1,60 @@
import csv
import os
import argparse
# Function to append a card entry to the CSV file
def add_card_to_csv(csv_path, card_name, set_code, collector_number, quantity, foil):
file_exists = os.path.isfile(csv_path)
with open(csv_path, mode='a', newline='') as file:
writer = csv.writer(file)
# Write header if the file doesn't exist
if not file_exists:
writer.writerow(['Card Name', 'Set Code', 'Collector Number', 'Quantity', 'Foil'])
# Append the card data
writer.writerow([card_name, set_code, collector_number, quantity, foil])
print(f"Added {quantity}x {card_name} from set {set_code} with collector number {collector_number} (Foil: {foil}) to {csv_path}.")
# Function to parse and add multiple cards
def add_multiple_cards(csv_path, card_entries):
for entry in card_entries:
card_name, set_code, collector_number, quantity, foil = entry
add_card_to_csv(csv_path, card_name, set_code, collector_number, quantity, foil)
# Main function to handle command-line arguments
def main():
parser = argparse.ArgumentParser(description="Add one or more Magic: The Gathering cards to a CSV file.")
parser.add_argument('csv_path',
help="The path to the CSV file where the card data will be stored. If the file does not exist, it will be created.")
# Allow multiple --card entries
parser.add_argument('--card',
action='append',
nargs=5,
metavar=('Card_Name', 'Set_Code', 'Collector_Number', 'Quantity', 'Foil'),
help=("Add a card to the CSV file. "
"Each card entry should be specified in the following format: "
"Card_Name Set_Code Collector_Number Quantity Foil. "
"Foil should be 'True' or 'False' indicating if the card is foil or not. "
"This option can be used multiple times to add multiple cards in one command."))
args = parser.parse_args()
if args.card:
card_entries = []
for card in args.card:
card_name, set_code, collector_number, quantity, foil = card
quantity = int(quantity)
foil = foil.lower() == 'true' # Convert the foil input to a boolean
card_entries.append((card_name, set_code, collector_number, quantity, foil))
add_multiple_cards(args.csv_path, card_entries)
else:
print("No cards provided. Use the --card option to add cards.")
if __name__ == "__main__":
main()

72
mtgimagedl.py Normal file
View File

@ -0,0 +1,72 @@
import requests
import pandas as pd
import os
import time
# Function to fetch card details from Scryfall using the set code and collector number
def fetch_card_images_by_set_and_number(set_code, collector_number):
url = f"https://api.scryfall.com/cards/{set_code}/{collector_number}"
response = requests.get(url)
if response.status_code == 200:
card_data = response.json()
images = []
# Handle single-sided or multi-sided cards
if 'image_uris' in card_data:
images.append((card_data['image_uris']['normal'], card_data['name']))
if 'card_faces' in card_data:
for face in card_data['card_faces']:
if 'image_uris' in face:
images.append((face['image_uris']['normal'], face['name']))
return images
else:
print(f"Error fetching card with set code {set_code} and collector number {collector_number}: {response.status_code}")
return None
# Function to download and save an image
def download_image(image_url, save_path):
response = requests.get(image_url)
if response.status_code == 200:
with open(save_path, 'wb') as file:
file.write(response.content)
else:
print(f"Failed to download image from {image_url}")
# Function to process the CSV and download images
def download_images_from_csv(csv_path, output_folder):
# Create output directory if it doesn't exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Read the CSV file
df = pd.read_csv(csv_path)
# Assuming the CSV has columns 'Card Name', 'Set Code', and 'Collector Number'
for index, row in df.iterrows():
card_name = row['Card Name']
set_code = row['Set Code']
collector_number = row['Collector Number']
print(f"Processing {card_name} from set {set_code} with collector number {collector_number}...")
images = fetch_card_images_by_set_and_number(set_code, collector_number)
if images:
for image_url, face_name in images:
# Sanitize file name by removing invalid characters
image_file_name = f"{card_name.replace('/', '-').replace(':', '')}_{face_name.replace('/', '-').replace(':', '')}.jpg"
save_path = os.path.join(output_folder, image_file_name)
download_image(image_url, save_path)
time.sleep(0.1) # Sleep to avoid hitting API rate limits
else:
print(f"No images found for {card_name} from set {set_code} with collector number {collector_number}")
# Main execution
if __name__ == "__main__":
# Define the path to the input CSV and output folder
csv_path = 'cards.csv' # Replace with your CSV file path
output_folder = 'downloaded_images'
# Start the download process
download_images_from_csv(csv_path, output_folder)
print("Download completed.")