-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
61 lines (45 loc) · 1.43 KB
/
main.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
import datetime
import os
import json
import time
import urllib.request
def get_page(page):
request = urllib.request.Request(page)
request.add_header('User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0')
return urllib.request.urlopen(request).read()
folder = './images/'
# Remove all pre-existing images
if os.path.isdir(folder):
print("Cleaning images folder")
os.system("rm -rf " + folder)
os.system("git rm -r " + folder)
# Ensure the images folder exists
if not os.path.isdir(folder):
os.mkdir(folder)
hasMore = True
page = 0
images = []
while hasMore:
print("Loading page", page)
response = get_page("https://undraw.co/api/illustrations?page=" + str(page))
data = json.loads(response.decode('utf-8'))
hasMore = data['hasMore']
page = data['nextPage']
for illustration in data['illos']:
images.append((
illustration['title'],
illustration['image']
))
print("Loaded all pages")
for i in range(len(images)):
name, location = images[i]
image = get_page(location)
fname = folder + name + '.svg'
file = open(fname, 'w')
file.write(image.decode('utf-8'))
file.close()
print("[%04d/%04d] Downloaded %s" % (i + 1, len(images), name))
os.system("git add \"%s\"" % fname)
time.sleep(0.5)
date = datetime.datetime.now().strftime("%Y-%m-%d")
os.system("git commit -m \"Images %s\"" % date)