From f42381b37c008ff82471a6b0f8f68eee58d26aaf Mon Sep 17 00:00:00 2001 From: Nikhil Badyal Date: Fri, 11 Nov 2022 23:20:29 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Handle=20a=20case=20when=20user?= =?UTF-8?q?=20enters=20invalid=20app=20for=20rebuilding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 57 ++++++++++++++++++++++++++++++++------------------ src/config.py | 3 ++- src/parser.py | 16 ++++++++------ src/patches.py | 5 ++--- src/utils.py | 6 ++++++ 5 files changed, 57 insertions(+), 30 deletions(-) diff --git a/main.py b/main.py index 4fa61ef..6bcb554 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,7 @@ from src.config import RevancedConfig from src.downloader import Downloader from src.parser import Parser from src.patches import Patches +from src.utils import AppNotFound def main() -> None: @@ -25,35 +26,51 @@ def main() -> None: logger.info("Trying to build %s" % app) app_all_patches, version, is_experimental = patcher.get_app_configs(app) version = downloader.download_apk_to_patch(version, app) + config.app_versions[app] = version patcher.include_exclude_patch(app, parser, app_all_patches) logger.info(f"Downloaded {app}, version {version}") parser.patch_app(app=app, version=version, is_experimental=is_experimental) + except AppNotFound as e: + logger.info(f"Invalid app requested to build {e}") except Exception as e: logger.exception(f"Failed to build {app} because of {e}") - if len(config.alternative_youtube_patches): + if len(config.alternative_youtube_patches) and "youtube" in config.apps: for alternative_patch in config.alternative_youtube_patches: - logger.info(f"Rebuilding youtube with inverted ${alternative_patch} patch.") _, version, is_experimental = patcher.get_app_configs("youtube") - parser.invert_patch(alternative_patch) - parser.patch_app( - app="youtube", - version=version, - is_experimental=is_experimental, - output_prefix="-" + alternative_patch + "-", - ) - if len(config.alternative_youtube_music_patches): + was_inverted = parser.invert_patch(alternative_patch) + if was_inverted: + logger.info( + f"Rebuilding youtube with inverted {alternative_patch} patch." + ) + parser.patch_app( + app="youtube", + version=config.app_versions.get("youtube", "latest"), + is_experimental=is_experimental, + output_prefix="-" + alternative_patch + "-", + ) + else: + logger.info( + f"Skipping Rebuilding youtube as {alternative_patch} patch was not found." + ) + if len(config.alternative_youtube_music_patches) and "youtube_music" in config.apps: for alternative_patch in config.alternative_youtube_music_patches: - logger.info( - f"Rebuilding youtube music with inverted ${alternative_patch} patch." - ) + _, version, is_experimental = patcher.get_app_configs("youtube_music") - parser.invert_patch(alternative_patch) - parser.patch_app( - app="youtube_music", - version=version, - is_experimental=is_experimental, - output_prefix="-" + alternative_patch + "-", - ) + was_inverted = parser.invert_patch(alternative_patch) + if was_inverted: + logger.info( + f"Rebuilding youtube music with inverted {alternative_patch} patch." + ) + parser.patch_app( + app="youtube_music", + version=config.app_versions.get("youtube_music", "latest"), + is_experimental=is_experimental, + output_prefix="-" + alternative_patch + "-", + ) + else: + logger.info( + f"Skipping Rebuilding youtube music as {alternative_patch} patch was not found." + ) if __name__ == "__main__": diff --git a/src/config.py b/src/config.py index c952e8f..b25a326 100644 --- a/src/config.py +++ b/src/config.py @@ -1,6 +1,6 @@ """Revanced Configurations.""" from pathlib import Path -from typing import List +from typing import Dict, List from environs import Env from requests import Session @@ -12,6 +12,7 @@ class RevancedConfig: """Revanced Configurations.""" def __init__(self, env: Env) -> None: + self.app_versions: Dict[str, str] = {} self.env = env self.temp_folder = Path("apks") self.session = Session() diff --git a/src/parser.py b/src/parser.py index aaeb7d9..3f8d15d 100644 --- a/src/parser.py +++ b/src/parser.py @@ -49,16 +49,20 @@ class Parser(object): """ return self._PATCHES - def invert_patch(self, name: str) -> None: + def invert_patch(self, name: str) -> bool: """ Getter to get all excluded patches :return: List of excluded patches """ - patch_index = self._PATCHES.index(name) - if self._PATCHES[patch_index - 1] == "-e": - self._PATCHES[patch_index - 1] = "-i" - else: - self._PATCHES[patch_index - 1] = "-e" + try: + patch_index = self._PATCHES.index(name) + if self._PATCHES[patch_index - 1] == "-e": + self._PATCHES[patch_index - 1] = "-i" + else: + self._PATCHES[patch_index - 1] = "-e" + return True + except ValueError: + return False # noinspection IncorrectFormatting def patch_app( diff --git a/src/patches.py b/src/patches.py index ccc6a46..738b253 100644 --- a/src/patches.py +++ b/src/patches.py @@ -1,12 +1,12 @@ """Revanced Patches.""" import subprocess -import sys from typing import Any, Dict, List, Tuple from loguru import logger from requests import Session from src.config import RevancedConfig +from src.utils import AppNotFound class Patches(object): @@ -118,8 +118,7 @@ class Patches(object): "nyx-music-player": "_nyx", } if not (app_name := app_names.get(app)): - logger.debug("Invalid app name") - sys.exit(-1) + raise AppNotFound(app_name) patches = getattr(self, app_name) version = "" try: diff --git a/src/utils.py b/src/utils.py index 73801d7..98741dc 100644 --- a/src/utils.py +++ b/src/utils.py @@ -31,3 +31,9 @@ def update_changelog(name: str, response: Dict[str, str]) -> None: ) file1.write(change_log) file1.close() + + +class AppNotFound(ValueError): + """Not a valid Revanced App.""" + + pass