2021-12-19 17:20:34 +01:00
|
|
|
import shutil
|
|
|
|
import requests
|
|
|
|
from time import sleep
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
def download_chapter(image_urls, chapter_path):
|
|
|
|
# download images
|
2021-12-19 19:44:19 +01:00
|
|
|
img_num = 1
|
2021-12-19 17:20:34 +01:00
|
|
|
for img in image_urls:
|
|
|
|
# set image path
|
2021-12-19 19:44:19 +01:00
|
|
|
image_path = Path(f'{chapter_path}/{img_num:03d}')
|
2021-12-19 17:20:34 +01:00
|
|
|
try:
|
2021-12-19 19:44:19 +01:00
|
|
|
#print('Try getting ' + img)
|
2021-12-19 17:20:34 +01:00
|
|
|
req = requests.get(img, stream = True)
|
|
|
|
except:
|
|
|
|
print('Request failed, retrying')
|
|
|
|
sleep(2)
|
|
|
|
req = requests.get(img, stream = True)
|
|
|
|
|
|
|
|
if req.status_code == 200:
|
|
|
|
req.raw.decode_content = True
|
|
|
|
with image_path.open('wb') as file:
|
|
|
|
shutil.copyfileobj(req.raw, file)
|
|
|
|
|
2021-12-19 19:44:19 +01:00
|
|
|
print(f' Downloaded image {img_num}')
|
2021-12-19 17:20:34 +01:00
|
|
|
img_num += 1
|
2021-12-19 19:44:19 +01:00
|
|
|
sleep(0.5)
|
2021-12-19 17:20:34 +01:00
|
|
|
else:
|
|
|
|
print('Image could not be downloaded. Exiting')
|
|
|
|
exit(1)
|