From 7e92b33ae872d6524e18066f3cbf07ee6571d2d5 Mon Sep 17 00:00:00 2001 From: Danylo Bodnarchuk Date: Sun, 18 Aug 2024 04:13:16 +0200 Subject: [PATCH] * Added help messages * Added scrolling functionality that works after couple of failed attempts * Added scrolling after a 5 consecutive successful matches, to fix transparent ads problem * Fixed error, which prevented showing info about templates folder --- README.md | 27 +++++++++++++++++++++++++ nexus_autodl.py | 52 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 95a6500..939a86a 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,33 @@ This spawns a terminal window which you can close when you are done downloading Users on other platforms can download the source code on GitHub. +## Usage + +```text +nexus_autodl.py [OPTIONS] + +Options: + --confidence FLOAT Specifies the accuracy with which the script + should find the screenshot button [default:0.7] + --grayscale / --color Try to locate button using only desaturated + images, can speed up the locating action + [default: grayscale] + --min-sleep-interval INTEGER Minumal amount of time to wait between every + click attempt [default: 1] + --max-sleep-interval INTEGER Minumal amount of time to wait between every + click attempt [default: 5] + --retries-before-scroll INTEGER + Amount of failed attemps before trying to + scroll down [default: 3] + --disable-transparent-ads-fix BOOLEAN + Disable the functionality that can + potentially locate transparent ads which + prevent clicking [default: False] + --templates-path TEXT Set a path to the folder with button screenshots + [default: \templates] + --help Show this message and exit. +``` + ## Caution Using a bot to download from Nexus is in direct violation of their TOS: diff --git a/nexus_autodl.py b/nexus_autodl.py index bd9acf5..f6257de 100755 --- a/nexus_autodl.py +++ b/nexus_autodl.py @@ -28,28 +28,35 @@ def key(key: Path) -> tuple[int | str, ...]: @click.command() -@click.option("--confidence", default=0.7, show_default=True) -@click.option("--grayscale/--color", default=True, show_default=True) -@click.option("--min-sleep-interval", default=1, show_default=True) -@click.option("--max-sleep-interval", default=5, show_default=True) -@click.option("--templates-path", default=Path.cwd() / "templates", show_default=True) +@click.option("--confidence", default=0.7, show_default=True, help="Specifies the accuracy with which the script should find the screenshot button") +@click.option("--grayscale/--color", default=True, show_default=True, help="Try to locate button using only desaturated images, can speed up the locating action") +@click.option("--min-sleep-interval", default=1, show_default=True, help="Minumal amount of time to wait between every click attempt") +@click.option("--max-sleep-interval", default=5, show_default=True, help="Minumal amount of time to wait between every click attempt") +@click.option("--retries-before-scroll", default=3, show_default=True, help="Amount of failed attemps before trying to scroll down") +@click.option("--disable-transparent-ads-fix", default=False, show_default=True, help="Disable the functionality that can potentially locate transparent ads which prevent clicking") +@click.option("--templates-path", default=Path.cwd() / "templates", show_default=True, help="Set a path to the folder with button screenshots") def main( confidence: float, grayscale: bool, min_sleep_interval: int, max_sleep_interval: int, templates_path: str, -) -> None: + retries_before_scroll: int, + disable_transparent_ads_fix: bool, + ) -> None: + transparent_ads_fix = 5 + matched = False templates_path_ = Path(templates_path) + retries_before_scroll_ = retries_before_scroll templates: dict[Path, Image] = {} - for template_path in human_sorted(templates_path_.iterdir()): - try: - templates[template_path] = open_image(template_path) - logging.info(f"Loaded {template_path}") - except UnidentifiedImageError: - logging.info(f"{template_path} is not a valid image; skipping") - - if len(templates) == 0: + try: + for template_path in human_sorted(templates_path_.iterdir()): + templates[template_path] = open_image(template_path) + logging.info(f"Loaded {template_path}") + except UnidentifiedImageError: + logging.info(f"{template_path} is not a valid image; skipping") + except FileNotFoundError: + logging.error( f"No images found in {templates_path_.absolute()}. " f"If this is your first time running, take a screenshot and crop " @@ -75,10 +82,27 @@ def main( except ImageNotFoundException: pass if not isinstance(box, Box): + if not retries_before_scroll_: + logging.info(f"Picture matching failed, attempting to scroll.") + pyautogui.scroll(-40) + else: + retries_before_scroll_ -= 1 + matched = False continue + if matched: + transparent_ads_fix -= 1 + else: + transparent_ads_fix = 5 + if not transparent_ads_fix and not disable_transparent_ads_fix: + logging.info(f"Potential transparent ad encountered, attempting to scroll.") + pyautogui.scroll(-40) + transparent_ads_fix = 5 + matched = True + retries_before_scroll_ = retries_before_scroll match_x, match_y = pyautogui.center(box) pyautogui.click(match_x, match_y) logging.info(f"Matched at ({match_x}, {match_y}).") + pyautogui.moveTo(400, 400) break sleep_interval = random.uniform(min_sleep_interval, max_sleep_interval)