-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgrab_images.py
More file actions
37 lines (27 loc) · 901 Bytes
/
grab_images.py
File metadata and controls
37 lines (27 loc) · 901 Bytes
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
# grab_images.py
from __future__ import with_statement
import os
import sys
#
import grab_html
import tools
IMAGE_URL = "http://gatherer.wizards.com/Handlers/Image.ashx?"\
"multiverseid=%(id)s&type=card"
def grab_images(short_set):
""" Download all card images for the given set. """
path = os.path.join('images', short_set)
if not os.path.exists(path):
os.makedirs(path)
ids = grab_html.read_ids(short_set)
for idx, id in enumerate(ids):
url = IMAGE_URL % locals()
data = tools.grab_url(url)
print "Grabbed image: %s" % id
write_image(short_set, id, data)
def write_image(short_set, id, data):
path = os.path.join('images', short_set, '%s.jpg' % id) # assume JPG
with open(path, 'wb') as f:
f.write(data)
if __name__ == "__main__":
for short_set in sys.argv[1:]:
grab_images(short_set)