manga-dlp/mangadlp/downloader.py

54 lines
1.6 KiB
Python
Raw Permalink Normal View History

2021-12-19 17:20:34 +01:00
from pathlib import Path
from time import sleep
2022-05-16 16:09:17 +02:00
import shutil
import requests
2022-05-16 16:09:17 +02:00
import mangadlp.utils as utils
2021-12-19 17:20:34 +01:00
2022-05-16 16:09:17 +02:00
# download images
2022-05-13 22:34:25 +02:00
def download_chapter(image_urls, chapter_path, download_wait, verbose):
total_img = len(image_urls)
2022-05-16 16:09:17 +02:00
for img_num, img in enumerate(image_urls, 1):
2022-05-04 19:17:12 +02:00
# set image path
image_path = Path(f"{chapter_path}/{img_num:03d}")
# show progress bar
2022-05-16 16:09:17 +02:00
utils.progress_bar(img_num, total_img, verbose)
counter = 1
while counter <= 3:
try:
r = requests.get(img, stream=True)
except KeyboardInterrupt:
print("ERR: Stopping")
exit(1)
except:
if counter >= 3:
print("ERR: Maybe the MangaDex Servers are down?")
raise ConnectionError
print(f"ERR: Request for image {img} failed, retrying")
sleep(download_wait)
counter += 1
else:
if r.status_code != 200:
print(f"ERR: Image {img} could not be downloaded. Retrying")
continue
break
2021-12-19 17:20:34 +01:00
2022-05-16 16:09:17 +02:00
# verbose logging
if verbose:
print(f"INFO: Downloaded image {img_num}")
# write image
try:
2022-05-04 19:17:12 +02:00
with image_path.open("wb") as file:
2022-05-16 16:09:17 +02:00
r.raw.decode_content = True
2022-05-13 22:34:25 +02:00
shutil.copyfileobj(r.raw, file)
2022-05-16 16:09:17 +02:00
except:
print("ERR: Can't write file")
raise IOError
2021-12-19 17:20:34 +01:00
2022-05-16 16:09:17 +02:00
img_num += 1
sleep(download_wait)
2022-05-16 16:09:17 +02:00
# if every image was downloaded and written successfully
return True