-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxkcd.py
61 lines (46 loc) · 1.36 KB
/
xkcd.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
from urllib.request import urlopen, urlretrieve
import os
os.makedirs('XKCD', exist_ok=True)
os.chdir('XKCD')
# Getting main info to get total images count
info = eval(urlopen(f'https://xkcd.com/info.0.json').read())
BIG_N = int(info['num'])
IMAGES = os.listdir()
CODE = True
N = 0
print(f'xkcd has {BIG_N} images')
while N < BIG_N:
N += 1
# xkcd real-life meme, lol XD
if N == 404:
continue
# Check if this one exists
if any(
map(
lambda i: i in IMAGES,
'{}.jpg {}.png {}.gif {}.jpeg'.format(*[N] * 4).split()
)
):
continue
try:
# Get image url from API instead of parsing [Faster]
json = eval(urlopen(f'https://xkcd.com/{N}/info.0.json').read())
img_url = json['img']
# Check if url has no comic (Magic comic)
if img_url.endswith('/comics/'):
print(f'No comic at: {N}')
continue
# Decide extension and name
ext = img_url.split('.')[-1]
img_name = f'{N}.{ext}'
# Download image
urlretrieve(
img_url,
img_name
)
print(f'Saved: {img_name}')
# Catch error and inform user
except BaseException as e:
print(f'Error at {N}: {e}')
continue
print('Finished :)')