Created
May 12, 2019 02:50
-
-
Save dyspop/43e64e04b7a8e0610514bb6ea006d6fa to your computer and use it in GitHub Desktop.
Download mtg set data
This file contains 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 json | |
import requests | |
# Ask the user for the set abbreviation. | |
setabbrv = input("What's the abbreviation of the set?\n") | |
# Define the URL to look it up from | |
lookupobject = "https://api.scryfall.com/cards/search?order=name&q=set:" + setabbrv | |
# Make a get request and convert the json response to a dictionary | |
resp = json.loads(requests.get(lookupobject).text) | |
# Define a separate set data object so we can add to it separately | |
set_data = resp["data"] | |
# Since we're not using python csv library we need this to set a default value | |
def default_val(s): | |
return s if s is not None else "n/a" | |
while resp["has_more"] and True is resp["has_more"]: | |
# Get the data for the next page | |
resp_next = json.loads(requests.get(resp["next_page"]).text) | |
# Add the partial set data from the next page to the set data | |
set_data += resp_next["data"] | |
# Assign the next response to the object we check in the while | |
resp = resp_next | |
with open(f"mtg-{setabbrv}-info.txt", "w+") as f: | |
frmt = ",\t" | |
for card in set_data: | |
name = default_val(card["name"]) | |
rarity = default_val(card["rarity"]) | |
price = default_val(card["prices"]["usd"]) | |
foil_price = default_val(card["prices"]["usd_foil"]) | |
line = frmt.join([name, rarity, price, foil_price, "\n"]) | |
f.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment