From 2de40f2d25b971df38ef60271b701732ff1f1796 Mon Sep 17 00:00:00 2001 From: David Gillespie Date: Thu, 3 Oct 2024 00:41:56 -0600 Subject: [PATCH] fix temp dir overwrite bug when running concurrent tasks --- mur.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/mur.py b/mur.py index 961c404..d4ee995 100644 --- a/mur.py +++ b/mur.py @@ -1,19 +1,20 @@ #!/usr/bin/env python3 # Sorrow446 -import os -import re -import sys -import json -import shutil -import zipfile import argparse +import img2pdf +import json +import os import platform +import random +import re +from requests.exceptions import HTTPError +import shutil +import sys +from tqdm import tqdm +import zipfile import api -import img2pdf -from tqdm import tqdm -from requests.exceptions import HTTPError from api.exceptions import IneligibleError client = api.Client() @@ -70,6 +71,12 @@ def parse_args(): required=False, action='store_true' ) + parser.add_argument( + '-n', '--nextNum', + help="Download n number of issues after given issue", + required=False, + action='store_true' + ) return parser.parse_args() def parse_cookies(cd, out_cookies={}): @@ -146,13 +153,14 @@ def main(): cd = os.path.dirname(sys.executable) else: cd = os.path.dirname(__file__) - tmp_dir = os.path.join(cd, 'mur_tmp') + tmp_dir = os.path.join(cd, f'mur_tmp_{random.randint(1000,9999)}') dl_dir = os.path.join(cd, 'MUR downloads') dir_setup(tmp_dir, dl_dir) parse_cookies(cd) args = parse_args() tot = len(args.url) cur = 0 + next_count = 0 urls = [] + args.url for url in urls: @@ -171,10 +179,12 @@ def main(): id = url fmt = args.format meta = client.get_comic_meta(id) - if args.all: + if args.all or (args.nextNum and next_count < args.nextNum): next_id = client.get_next_comic(id) if next_id: urls.append(next_id) + next_count += 1 + title = meta['title'] title_s = sanitize(title) print(str(title) + "\n") @@ -190,8 +200,7 @@ def main(): except IneligibleError as e: print(e) sys.exit(1) - #input("Press Enter to continue...") - ##NEW + sourcedir = "tmp_dir"; number_ofdigits = 5; extensions = (".jpg", ".jpeg") files = os.listdir(tmp_dir) @@ -200,8 +209,7 @@ def main(): name = item.split("."); zeros = number_ofdigits-len(name[0]) newname = str(zeros*"0")+name[0]+"."+name[1] shutil.move(tmp_dir+"/"+item, tmp_dir+"/"+newname) - ##END NEW - #input("Press Enter to continue...") + images = [os.path.join(tmp_dir, i) for i in os.listdir(tmp_dir)] print('Converting to {}...'.format(fmt.upper())) if fmt == 'pdf': @@ -214,6 +222,7 @@ def main(): write_meta(meta_abs, meta) for i in images: os.remove(i) + os.rmdir(tmp_dir) except HTTPError as e: err(e, cur, tot) except Exception as e: