2022-07-06 22:19:40 +02:00
|
|
|
import logging
|
2022-05-25 20:10:43 +02:00
|
|
|
import shutil
|
|
|
|
import sys
|
2021-12-19 17:20:34 +01:00
|
|
|
from pathlib import Path
|
2022-05-09 15:42:13 +02:00
|
|
|
from time import sleep
|
2022-06-20 17:46:04 +02:00
|
|
|
from typing import Union
|
2022-05-25 20:10:43 +02:00
|
|
|
|
2022-05-09 15:42:13 +02:00
|
|
|
import requests
|
2022-12-29 18:13:19 +01:00
|
|
|
from loguru import logger as log
|
2022-05-16 16:09:17 +02:00
|
|
|
|
2022-07-22 21:11:01 +02:00
|
|
|
from mangadlp import utils
|
2022-05-16 16:09:17 +02:00
|
|
|
|
2022-07-21 20:39:56 +02:00
|
|
|
|
2022-05-09 15:42:13 +02:00
|
|
|
# download images
|
2022-05-19 00:06:35 +02:00
|
|
|
def download_chapter(
|
2022-06-20 17:46:04 +02:00
|
|
|
image_urls: list,
|
|
|
|
chapter_path: Union[str, Path],
|
|
|
|
download_wait: float,
|
2022-05-19 00:06:35 +02:00
|
|
|
) -> None:
|
2022-05-13 16:43:50 +02:00
|
|
|
total_img = len(image_urls)
|
2022-06-17 23:39:01 +02:00
|
|
|
for image_num, image in enumerate(image_urls, 1):
|
|
|
|
# get image suffix
|
|
|
|
image_suffix = str(Path(image).suffix) or ".png"
|
2022-05-04 19:17:12 +02:00
|
|
|
# set image path
|
2022-06-17 23:39:01 +02:00
|
|
|
image_path = Path(f"{chapter_path}/{image_num:03d}{image_suffix}")
|
2022-07-06 22:19:40 +02:00
|
|
|
# show progress bar for default log level
|
|
|
|
if logging.root.level == logging.INFO:
|
2022-06-17 23:39:01 +02:00
|
|
|
utils.progress_bar(image_num, total_img)
|
2022-12-29 18:13:19 +01:00
|
|
|
log.debug(f"Downloading image {image_num}/{total_img}")
|
2022-05-17 13:16:29 +02:00
|
|
|
|
2022-05-16 16:09:17 +02:00
|
|
|
counter = 1
|
|
|
|
while counter <= 3:
|
|
|
|
try:
|
2022-06-17 23:39:01 +02:00
|
|
|
r = requests.get(image, stream=True)
|
2022-05-19 00:06:35 +02:00
|
|
|
if r.status_code != 200:
|
2022-07-15 12:49:49 +02:00
|
|
|
log.error(f"Request for image {image} failed, retrying")
|
2022-05-19 00:06:35 +02:00
|
|
|
raise ConnectionError
|
2022-05-16 16:09:17 +02:00
|
|
|
except KeyboardInterrupt:
|
2022-07-15 12:49:49 +02:00
|
|
|
log.critical("Stopping")
|
2022-05-25 20:10:43 +02:00
|
|
|
sys.exit(1)
|
2022-07-22 21:11:01 +02:00
|
|
|
except Exception as exc:
|
2022-05-16 16:09:17 +02:00
|
|
|
if counter >= 3:
|
2022-07-15 12:49:49 +02:00
|
|
|
log.error("Maybe the MangaDex Servers are down?")
|
2022-07-22 21:11:01 +02:00
|
|
|
raise ConnectionError from exc
|
2022-05-16 16:09:17 +02:00
|
|
|
sleep(download_wait)
|
|
|
|
counter += 1
|
|
|
|
else:
|
|
|
|
break
|
2021-12-19 17:20:34 +01:00
|
|
|
|
2022-05-16 16:09:17 +02:00
|
|
|
# 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-07-22 21:11:01 +02:00
|
|
|
except Exception as exc:
|
2022-07-15 12:49:49 +02:00
|
|
|
log.error("Can't write file")
|
2022-07-22 21:11:01 +02:00
|
|
|
raise IOError from exc
|
2021-12-19 17:20:34 +01:00
|
|
|
|
2022-06-17 23:39:01 +02:00
|
|
|
image_num += 1
|
2022-05-16 16:09:17 +02:00
|
|
|
sleep(download_wait)
|