From 6f1afa9d76e9c2611a8025fb067d7ee14518dc18 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 20 Apr 2020 00:57:21 +0200 Subject: Remove in_progress file for manga if download fails --- plugins/mangadex.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'plugins/mangadex.py') diff --git a/plugins/mangadex.py b/plugins/mangadex.py index 292db35..3c70b59 100755 --- a/plugins/mangadex.py +++ b/plugins/mangadex.py @@ -33,11 +33,13 @@ if len(sys.argv) < 2: def download_file(url, save_path): with requests.get(url, stream=True) as response: - response.raise_for_status() + if not response.ok(): + return False with open(save_path, "wb") as file: for chunk in response.iter_content(chunk_size=8192): if chunk: file.write(chunk) + return True def title_url_extract_manga_id(url): result = re.search("mangadex.org/title/([0-9]+)/", url) @@ -95,6 +97,7 @@ def chapter_url_extract_manga_id(url): return result.groups()[0] def download_chapter(url, download_dir): + request_url = url manga_id = chapter_url_extract_manga_id(url) if not manga_id: print("Failed to extract manga id from url: %s. Note: url is expected to be in this format: mangadex.org/chapter/" % url) @@ -109,7 +112,7 @@ def download_chapter(url, download_dir): in_progress_filepath = os.path.join(download_dir, ".in_progress") with open(in_progress_filepath, "w") as file: - file.write(url) + file.write(request_url) img_number = 1 json_response = response.json() @@ -125,11 +128,15 @@ def download_chapter(url, download_dir): image_name = str(img_number) + ext image_path = os.path.join(download_dir, image_name) print("Downloading {} to {}".format(image_url, image_path)) - download_file(image_url, image_path) + if not download_file(image_url, image_path): + print("Failed to download image: %s" % image_url) + os.remove(in_progress_filepath) + exit(2) img_number += 1 if img_number == 1: print("Failed to find images for chapter") + os.remove(in_progress_filepath) exit(2) with open(os.path.join(download_dir, ".finished"), "w") as file: -- cgit v1.2.3