-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscryfall.py
60 lines (43 loc) · 1.84 KB
/
scryfall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import text_parser
import requests
import ujson
import os
from shutil import copyfileobj
from datetime import datetime as dt
bulkDataFileName = 'bulkdata.json'
imagesDirectory = 'Card Images'
if not os.path.exists(imagesDirectory):
os.makedirs(imagesDirectory)
def batch_image_download(cardList):
for cardTuple in cardList:
#check for dupes here, and continue(skip) if it is already downloaded
card = cardTuple[1]
sameCards = []
print('Searching for card ' + card)
with open(bulkDataFileName, encoding="utf8") as bulkJson:
bulkData = ujson.load(bulkJson)
# Get the image URL
for i in bulkData:
if i['name'] == card:
if i['lang'] == 'en':
if i['set'] != 'prm' and i['promo'] == False and i['border_color'] == 'black' and i['set'] != 'akr':
sameCards.append(i)
print(str(len(sameCards)) + ' version(s) of ' + card + ' were found.')
if len(sameCards) > 0:
newestCard = max(sameCards, key=lambda ev: dt.strptime(ev['released_at'], "%Y-%m-%d"))
imgUrl = newestCard['image_uris']['png']
#save the current sheet
with open(imagesDirectory +'/'+ card +'.png', 'wb') as out_file:
copyfileobj(requests.get(imgUrl, stream = True).raw, out_file)
else:
print(card + ' not found, is it misspelled?')
def clean_images():
for file in os.scandir(imagesDirectory):
os.remove(file)
def bulk_data_fetch():
api = requests.get('https://api.scryfall.com/bulk-data')
apiJson = api.json()
bulkDataUrl = apiJson['data'][2]['download_uri']
response = requests.get(bulkDataUrl)
with open(bulkDataFileName, 'wb') as out_file:
out_file.write(response.content)