Created
September 25, 2024 18:36
-
-
Save gokalper/91fe011e5092740de984e86d661cb771 to your computer and use it in GitHub Desktop.
Get Craftable Commons/Uncommons for new MTG Sets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def get_common_uncommon_cards(set_code): | |
"""Fetches common and uncommon cards from a given Scryfall set. | |
Args: | |
set_code (str): The three-letter code of the Scryfall set. | |
Returns: | |
list: A list of dictionaries representing the card data. | |
""" | |
url = "https://api.scryfall.com/cards/search" | |
params = { | |
"q": f"set:{set_code} -r:r -r:m", | |
} | |
response = requests.get(url, params=params) | |
response.raise_for_status() # Raise an exception for bad status codes | |
data = response.json() | |
cards = data.get("data", []) | |
# Handle pagination if there are more than 175 cards | |
while data.get("has_more"): | |
next_page_url = data.get("next_page") | |
response = requests.get(next_page_url) | |
response.raise_for_status() | |
data = response.json() | |
cards.extend(data.get("data", [])) | |
return cards | |
# Example usage | |
set_code = "dsk" | |
cards = get_common_uncommon_cards(set_code) | |
import os | |
os.makedirs(set_code, exist_ok=True) | |
file_counter = 0 | |
current_file = None | |
# Process the card data as needed | |
for x in range(0, len(cards)): | |
# Create a new file every 60 iterations or at the beginning | |
if x % 60 == 0: | |
file_counter += 1 | |
if current_file: | |
current_file.close() # Close the previous file if it exists | |
current_file = open(os.path.join(set_code, f"output_{file_counter}.txt"), "w") | |
# Write the card data to the current file | |
current_file.write(f"4 {cards[x]['name']}\n") | |
# Close the last file after the loop finishes | |
if current_file: | |
current_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment