Generates set.json files from Scryfall

I should have done this in the first place instead of figuring out how
to use MTGJSON files. Scryfall structures its data a bit differently and
notably includes a list of keywords for each card.

Having these set.json files enables easier analysis of the keyword
content of each set.
This commit is contained in:
Dan Niemitalo 2020-11-29 12:14:26 -06:00
parent 40f12289a2
commit 9dd4186e1c
2 changed files with 159 additions and 0 deletions

40
scryfall_requests.py Normal file
View file

@ -0,0 +1,40 @@
import requests
import json
import time
# Requests card objects from each set from Scryfall.com
# Takes a few seconds and creates a ~1 MB json file per set
# Reads set codes on separate lines in set_codes.txt
def request_set(code):
time.sleep(0.1) # Scryfall asks for 50-100 ms delay between requests
# Scryfall query is "set:code in:booster"
url = f'https://api.scryfall.com/cards/search?q=set%3A{code}+in%3Dbooster'
print(f'Requesting {code}')
response = requests.get(url)
d = response.json()
cards = []
cards += d['data']
while d['has_more']:
time.sleep(0.1)
print(f'Requesting {code} next page')
response = requests.get(d['next_page'])
d = response.json()
cards += d['data']
output_cards = json.dumps(cards)
if code == 'CON':
code = 'CON_'
f = open(f'scryfall/sets/{code}.json', 'w+')
f.write(output_cards)
f.close()
sets = []
set_file = open('set_codes.txt')
for line in set_file:
sets += [line.strip()]
set_file.close()
for code in sets:
request_set(code)