manga-dlp/mangadlp/downloader.py

41 lines
1.2 KiB
Python
Raw Normal View History

2021-12-19 17:20:34 +01:00
import shutil
from pathlib import Path
from time import sleep
import requests
import mangadlp.utils as utils
2021-12-19 17:20:34 +01:00
# download images
2022-05-13 22:34:25 +02:00
def download_chapter(image_urls, chapter_path, download_wait, verbose):
2022-05-04 19:17:12 +02:00
img_num = 1
total_img = len(image_urls)
2022-05-04 19:17:12 +02:00
for img in image_urls:
# set image path
image_path = Path(f"{chapter_path}/{img_num:03d}")
# show progress bar
utils.progress_bar(img_num, total_img)
2022-05-04 19:17:12 +02:00
try:
# print('Try getting ' + img)
2022-05-13 22:34:25 +02:00
r = requests.get(img, stream=True)
except KeyboardInterrupt:
print("ERR: Stopping")
exit(1)
2022-05-04 19:17:12 +02:00
except:
print(f"ERR: Request for image {img} failed, retrying")
2022-05-13 22:34:25 +02:00
sleep(download_wait)
2022-05-04 19:17:12 +02:00
req = requests.get(img, stream=True)
2021-12-19 17:20:34 +01:00
2022-05-13 22:34:25 +02:00
if r.status_code == 200:
r.raw.decode_content = True
2022-05-04 19:17:12 +02:00
with image_path.open("wb") as file:
2022-05-13 22:34:25 +02:00
shutil.copyfileobj(r.raw, file)
2021-12-19 17:20:34 +01:00
2022-05-04 19:17:12 +02:00
# verbose logging
2022-05-13 22:34:25 +02:00
if verbose:
print(f"INFO: Downloaded image {img_num}")
2022-05-04 19:17:12 +02:00
img_num += 1
2022-05-13 22:34:25 +02:00
sleep(download_wait)
2022-05-04 19:17:12 +02:00
else:
print(f"ERR: Image {img} could not be downloaded. Exiting")
2022-05-04 19:17:12 +02:00
exit(1)