This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxkarchive.py
57 lines (50 loc) · 1.77 KB
/
xkarchive.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
import os
import re
import requests
def downloadJSON(comicNumber):
'''
Downloads the JSON from xkcd.
'''
r = requests.get('https://xkcd.com/' + str(comicNumber) + '/info.0.json')
return r.json()['img']
def downloadComic(comicNumber, downloadPath):
'''
Downloads the comics and saves it to the disk.
Uses regex to filter the filename from the URL.
'''
print('Download Comic: #' + str(comicNumber))
regex = r'(https:\/\/imgs\.xkcd\.com\/comics\/)(.*)'
img = downloadJSON(comicNumber)
title = re.findall(regex, img)
with open(downloadPath + '/' + str(comicNumber) + ' - ' + title[0][1], 'wb') as f:
image = requests.get(img)
f.write(image.content)
def getPath(path):
'''
Checks if the path exists, if not it will create a new folder at this postion.
'''
if os.path.exists(os.path.normpath(path)):
print('Download path exists!')
else:
os.mkdir(os.path.normpath(path))
print('Download path was created!')
return (os.path.normpath(path))
def download(latestXKCD, latestDownloadedXKCD, path):
'''
Triggers the loop to download the XKCD comics.
'''
for x in range(latestDownloadedXKCD, latestXKCD):
# This try statement is caused by: https://xkcd.com/404/
try:
downloadComic(x, path)
except:
print('An error occured:')
pass
def main():
latestDownloadedXKCD = int(input('Enter the latest XKCD you downloaded. If you do not have any XKCD, please enter 1. \n'))
latestXKCD = int(input('Please enter the latest XKCD: \n')) + 1
path = input('Please enter t^he download path: \n')
downloadPath = getPath(path)
download(latestXKCD, latestDownloadedXKCD, downloadPath)
if __name__ == '__main__':
main()