-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
69 lines (54 loc) · 1.76 KB
/
import.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
61
62
63
64
65
66
67
68
69
from slugify import slugify
import json
import os.path
import string
import time
import timeit
import urllib2
def set_filename(card):
# Nur ASCII siehe rename_originals.py
filename = card['set'] + '-' + card['collector_number'] + '-' + card['lang'] + '-' + slugify(card['name']) + '.jpg'
return ''.join(c for c in filename if c in string.printable)
def create_directory(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def download(card):
global i
directory = os.path.join('original', card['set'])
filename = set_filename(card)
if 'image_uris' not in card:
print(str(i) + ': Datei ' + filename + ' nicht vorhanden')
return 0
if 'large' not in card['image_uris']:
print(str(i) + ': Datei ' + filename + ' nicht vorhanden')
return 0
response = urllib2.urlopen(card['image_uris']['large'])
data = response.read()
# Write data to file
create_directory(directory)
path = os.path.join(directory, filename)
if os.path.isfile(path):
print(str(i) + ': Datei ' + filename + ' schon vorhanden')
return 0
file_ = open(path, 'w')
file_.write(data)
file_.close()
print(str(i) + ': Datei ' + filename + ' heruntergeladen')
time.sleep(.100)
return 1
i = 46507
start = timeit.default_timer()
with open('import/scryfall-default-cards.json') as f:
cards = json.load(f)
# print(json.dumps(cards[0], indent=4, sort_keys=True))
# print set_filename(cards[0])
# download(cards[0])
downloads = 0
for card in cards[i:]:
downloads += download(card)
i += 1
# if (downloads > 10):
# break;
print('Anzahl Downloads: ' + str(downloads))
stop = timeit.default_timer()
print 'Dauer: ' + str(round((stop - start), 2)) + ' Sekunden'