From b1af206dadb18e741de2ccd8cfdb91030a8b530f Mon Sep 17 00:00:00 2001 From: Nikhil Badyal Date: Wed, 9 Aug 2023 22:15:09 +0530 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=92=9A=20Fixed=20Branch=20prefix=20fo?= =?UTF-8?q?r=20bugs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 29 +++++++++++++++++ main.py | 4 +++ src/app.py | 7 ++-- src/downloader/download.py | 3 ++ src/downloader/github.py | 65 ++++++++++++++++++++++++++++++++++---- src/exceptions.py | 12 +++++++ src/parser.py | 5 ++- src/utils.py | 14 +++----- 8 files changed, 118 insertions(+), 21 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..0fb79e8 --- /dev/null +++ b/.env.example @@ -0,0 +1,29 @@ +#GLobal envs +PATCH_APPS=youtube,youtube_music,twiiter +GLOBAL_CLI_DL=https://github.com/revanced/revanced-cli +GLOBAL_PATCHES_DL=https://github.com/revanced/revanced-patches +GLOBAL_PATCHES_JSON_DL=https://github.com/revanced/revanced-patches +GLOBAL_INTEGRATIONS_DL=https://github.com/revanced/revanced-integrations +EXISTING_DOWNLOADED_APKS=youtube,youtube_music +PERSONAL_ACCESS_TOKEN=ghp_asample_token + +#YouTune +YOUTUBE_CLI_DL=https://github.com/revanced/revanced-cli +YOUTUBE_PATCHES_DL=https://github.com/revanced/revanced-patches +YOUTUBE_PATCHES_JSON_DL=https://github.com/revanced/revanced-patches +YOUTUBE_INTEGRATIONS_DL=https://github.com/revanced/revanced-integrations +YOUTUBE_KEYSTORE_FILE_NAME=youtube.keystore +YOUTUBE_ARCHS_TO_BUILD=arm64-v8a,armeabi-v7a +YOUTUBE_EXCLUDE_PATCH=custom-branding,hide-get-premium +YOUTUBE_INCLUDE_PATCH=remove-screenshot-restriction +YOUTUBE_VERSION=17.31.36 + +#YOUTUBE_MUSIC Music +YOUTUBE_MUSIC_CLI_DL=https://github.com/revanced/revanced-cli/releases/tag/v2.22.1 +YOUTUBE_MUSIC_PATCHES_DL=https://github.com/revanced/revanced-patches +YOUTUBE_MUSIC_PATCHES_JSON_DL=https://github.com/revanced/revanced-patches +YOUTUBE_MUSIC_INTEGRATIONS_DL=https://github.com/revanced/revanced-integrations +YOUTUBE_MUSIC_EXCLUDE_PATCH=yt-music-is-shit + +#Twitter +TWITTER_VERSION=latest diff --git a/main.py b/main.py index 2c296f4..2e1a820 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ from loguru import logger from src.config import RevancedConfig from src.downloader.factory import DownloaderFactory +from src.exceptions import PatchingFailed from src.parser import Parser from src.patches import Patches from src.utils import AppNotFound, PatchesJsonFailed, check_java, extra_downloads @@ -16,6 +17,7 @@ def main() -> None: from src.app import APP env = Env() + env.read_env() config = RevancedConfig(env) extra_downloads(config) check_java(config.dry_run) @@ -38,6 +40,8 @@ def main() -> None: logger.info(f"Invalid app requested to build {e}") except PatchesJsonFailed: logger.exception("Patches.json not found") + except PatchingFailed as e: + logger.exception(e) except Exception as e: logger.exception(f"Failed to build {app} because of {e}") diff --git a/src/app.py b/src/app.py index 83a4f81..5d18b29 100644 --- a/src/app.py +++ b/src/app.py @@ -8,7 +8,8 @@ from typing import Dict from loguru import logger from src.config import RevancedConfig -from src.utils import PatcherDownloadFailed, slugify +from src.exceptions import PatchingFailed +from src.utils import slugify class APP(object): @@ -64,7 +65,7 @@ class APP(object): if url.startswith("https://github"): from src.downloader.github import Github - url = Github.patch_resource(url, assets_filter)[0] + url = Github.patch_resource(url, assets_filter, config) if not file_name: extension = pathlib.Path(url).suffix file_name = APP.generate_filename(url) + extension @@ -97,7 +98,7 @@ class APP(object): try: self.resource[resource_name] = future.result() except Exception as e: - raise PatcherDownloadFailed(f"An exception occurred: {e}") from e + raise PatchingFailed(e) from e @staticmethod def generate_filename(url: str) -> str: diff --git a/src/downloader/download.py b/src/downloader/download.py index 9229242..4f40a71 100644 --- a/src/downloader/download.py +++ b/src/downloader/download.py @@ -10,6 +10,7 @@ from tqdm import tqdm from src.config import RevancedConfig from src.downloader.utils import implement_method +from src.exceptions import PatchingFailed from src.patches import Patches from src.utils import handle_response @@ -35,6 +36,8 @@ class Downloader(object): return False def _download(self, url: str, file_name: str) -> None: + if not url: + raise PatchingFailed("No download to download") if self.file_status_check( self.config.temp_folder.joinpath(file_name), self.config.dry_run, url ): diff --git a/src/downloader/github.py b/src/downloader/github.py index b47a8e9..87dd2cb 100644 --- a/src/downloader/github.py +++ b/src/downloader/github.py @@ -1,10 +1,12 @@ """Github Downloader.""" -from typing import Dict, List +import re +from typing import Dict, Tuple +from urllib.parse import urlparse import requests -from lastversion import latest from loguru import logger +from src.config import RevancedConfig from src.downloader.download import Downloader from src.utils import handle_response, update_changelog @@ -42,9 +44,60 @@ class Github(Downloader): self._download(download_url, file_name=app) @staticmethod - def patch_resource(repo_url: str, assets_filter: str) -> list[str]: + def _extract_repo_owner_and_tag(url: str) -> Tuple[str, str, str]: + """Extract repo owner and url from github url.""" + parsed_url = urlparse(url) + path_segments = parsed_url.path.strip("/").split("/") + + github_repo_owner = path_segments[0] + github_repo_name = path_segments[1] + + release_tag = "latest" + for i, segment in enumerate(path_segments): + if segment == "tag": + release_tag = "tags/" + path_segments[i + 1] + break + + return github_repo_owner, github_repo_name, release_tag + + @staticmethod + def _get_release_assets( + github_repo_owner: str, + github_repo_name: str, + release_tag: str, + asset_filter: str, + config: RevancedConfig, + ) -> str: + """Get assets from given tag.""" + api_url = f"https://api.github.com/repos/{github_repo_owner}/{github_repo_name}/releases/{release_tag}" + headers = { + "Content-Type": "application/vnd.github.v3+json", + } + if config.personal_access_token: + headers["Authorization"] = f"token {config.personal_access_token}" + response = requests.get(api_url, headers=headers) + handle_response(response) + assets = response.json()["assets"] + try: + filter_pattern = re.compile(asset_filter) + except re.error: + logger.error("Invalid regex pattern provided.") + raise Exception() + for asset in assets: + assets_url = asset["browser_download_url"] + assets_name = asset["name"] + match = filter_pattern.search(assets_url) + if match: + logger.debug(f"Found {assets_name} to be downloaded from {assets_url}") + return match.group() + return "" + + @staticmethod + def patch_resource( + repo_url: str, assets_filter: str, config: RevancedConfig + ) -> str: """Fetch patch resource from repo url.""" - latest_resource_version: List[str] = latest( - repo_url, assets_filter=assets_filter, output_format="assets" + repo_owner, repo_name, tag = Github._extract_repo_owner_and_tag(repo_url) + return Github._get_release_assets( + repo_owner, repo_name, tag, assets_filter, config ) - return latest_resource_version diff --git a/src/exceptions.py b/src/exceptions.py index ce17b67..4f28168 100644 --- a/src/exceptions.py +++ b/src/exceptions.py @@ -2,3 +2,15 @@ class APKMirrorScrapperFailure(Exception): """Failed to scrap icon from apkmirror.""" pass + + +class ExtraAssetsFailure(Exception): + """Failed to scrap icon from apkmirror.""" + + pass + + +class PatchingFailed(Exception): + """Patching Failed.""" + + pass diff --git a/src/parser.py b/src/parser.py index 6204308..4db166c 100644 --- a/src/parser.py +++ b/src/parser.py @@ -1,5 +1,4 @@ """Revanced Parser.""" -import sys from subprocess import PIPE, Popen from time import perf_counter from typing import List @@ -8,6 +7,7 @@ from loguru import logger from src.app import APP from src.config import RevancedConfig +from src.exceptions import PatchingFailed from src.patches import Patches from src.utils import possible_archs @@ -112,8 +112,7 @@ class Parser(object): process = Popen(["java", *args], stdout=PIPE) output = process.stdout if not output: - logger.error("Failed to send request for patching.") - sys.exit(-1) + raise PatchingFailed("Failed to send request for patching.") for line in output: logger.debug(line.decode(), flush=True, end="") process.wait() diff --git a/src/utils.py b/src/utils.py index b1195ed..a0d392a 100644 --- a/src/utils.py +++ b/src/utils.py @@ -8,6 +8,7 @@ from loguru import logger from requests import Response from src.config import RevancedConfig +from src.exceptions import PatchingFailed default_build = [ "youtube", @@ -43,12 +44,6 @@ class AppNotFound(ValueError): pass -class PatcherDownloadFailed(Exception): - """Not a valid Revanced App.""" - - pass - - class PatchesJsonFailed(ValueError): """Patches failed.""" @@ -59,8 +54,9 @@ def handle_response(response: Response) -> None: """Handle Get Request Response.""" response_code = response.status_code if response_code != 200: - logger.error(response.text) - exit(1) + raise PatchingFailed( + f"Unable to downloaded assets from GitHub. Reason - {response.text}" + ) def slugify(string: str) -> str: @@ -98,7 +94,7 @@ def check_java(dry_run: bool) -> None: raise subprocess.CalledProcessError(-1, "java -version") logger.debug("Cool!! Java is available") except subprocess.CalledProcessError: - logger.debug("Java>= 17 Must be installed") + logger.error("Java>= 17 Must be installed") exit(-1) From e9717442e0df1fe4becb23343922b47d0a73ef5a Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Wed, 9 Aug 2023 16:48:12 +0000 Subject: [PATCH 2/2] 'Refactored by Sourcery' --- src/downloader/github.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/downloader/github.py b/src/downloader/github.py index 87dd2cb..30e494a 100644 --- a/src/downloader/github.py +++ b/src/downloader/github.py @@ -52,12 +52,14 @@ class Github(Downloader): github_repo_owner = path_segments[0] github_repo_name = path_segments[1] - release_tag = "latest" - for i, segment in enumerate(path_segments): - if segment == "tag": - release_tag = "tags/" + path_segments[i + 1] - break - + release_tag = next( + ( + f"tags/{path_segments[i + 1]}" + for i, segment in enumerate(path_segments) + if segment == "tag" + ), + "latest", + ) return github_repo_owner, github_repo_name, release_tag @staticmethod @@ -86,8 +88,7 @@ class Github(Downloader): for asset in assets: assets_url = asset["browser_download_url"] assets_name = asset["name"] - match = filter_pattern.search(assets_url) - if match: + if match := filter_pattern.search(assets_url): logger.debug(f"Found {assets_name} to be downloaded from {assets_url}") return match.group() return ""