Merge pull request #268 from nikhilbadyal/bug/267-download-assets-from-custom-github-tag

💚 Custom Resource Download
This commit is contained in:
Nikhil Badyal
2023-08-09 20:53:39 -07:00
committed by GitHub
8 changed files with 120 additions and 22 deletions
+29
View File
@@ -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
+4
View File
@@ -6,6 +6,7 @@ from loguru import logger
from src.config import RevancedConfig from src.config import RevancedConfig
from src.downloader.factory import DownloaderFactory from src.downloader.factory import DownloaderFactory
from src.exceptions import PatchingFailed
from src.parser import Parser from src.parser import Parser
from src.patches import Patches from src.patches import Patches
from src.utils import AppNotFound, PatchesJsonFailed, check_java, extra_downloads from src.utils import AppNotFound, PatchesJsonFailed, check_java, extra_downloads
@@ -16,6 +17,7 @@ def main() -> None:
from src.app import APP from src.app import APP
env = Env() env = Env()
env.read_env()
config = RevancedConfig(env) config = RevancedConfig(env)
extra_downloads(config) extra_downloads(config)
check_java(config.dry_run) check_java(config.dry_run)
@@ -38,6 +40,8 @@ def main() -> None:
logger.info(f"Invalid app requested to build {e}") logger.info(f"Invalid app requested to build {e}")
except PatchesJsonFailed: except PatchesJsonFailed:
logger.exception("Patches.json not found") logger.exception("Patches.json not found")
except PatchingFailed as e:
logger.exception(e)
except Exception as e: except Exception as e:
logger.exception(f"Failed to build {app} because of {e}") logger.exception(f"Failed to build {app} because of {e}")
+4 -3
View File
@@ -8,7 +8,8 @@ from typing import Dict
from loguru import logger from loguru import logger
from src.config import RevancedConfig from src.config import RevancedConfig
from src.utils import PatcherDownloadFailed, slugify from src.exceptions import PatchingFailed
from src.utils import slugify
class APP(object): class APP(object):
@@ -64,7 +65,7 @@ class APP(object):
if url.startswith("https://github"): if url.startswith("https://github"):
from src.downloader.github import 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: if not file_name:
extension = pathlib.Path(url).suffix extension = pathlib.Path(url).suffix
file_name = APP.generate_filename(url) + extension file_name = APP.generate_filename(url) + extension
@@ -97,7 +98,7 @@ class APP(object):
try: try:
self.resource[resource_name] = future.result() self.resource[resource_name] = future.result()
except Exception as e: except Exception as e:
raise PatcherDownloadFailed(f"An exception occurred: {e}") from e raise PatchingFailed(e) from e
@staticmethod @staticmethod
def generate_filename(url: str) -> str: def generate_filename(url: str) -> str:
+3
View File
@@ -10,6 +10,7 @@ from tqdm import tqdm
from src.config import RevancedConfig from src.config import RevancedConfig
from src.downloader.utils import implement_method from src.downloader.utils import implement_method
from src.exceptions import PatchingFailed
from src.patches import Patches from src.patches import Patches
from src.utils import handle_response from src.utils import handle_response
@@ -35,6 +36,8 @@ class Downloader(object):
return False return False
def _download(self, url: str, file_name: str) -> None: def _download(self, url: str, file_name: str) -> None:
if not url:
raise PatchingFailed("No download to download")
if self.file_status_check( if self.file_status_check(
self.config.temp_folder.joinpath(file_name), self.config.dry_run, url self.config.temp_folder.joinpath(file_name), self.config.dry_run, url
): ):
+61 -7
View File
@@ -1,10 +1,12 @@
"""Github Downloader.""" """Github Downloader."""
from typing import Dict, List import re
from typing import Dict, Tuple
from urllib.parse import urlparse
import requests import requests
from lastversion import latest
from loguru import logger from loguru import logger
from src.config import RevancedConfig
from src.downloader.download import Downloader from src.downloader.download import Downloader
from src.utils import handle_response, update_changelog from src.utils import handle_response, update_changelog
@@ -42,9 +44,61 @@ class Github(Downloader):
self._download(download_url, file_name=app) self._download(download_url, file_name=app)
@staticmethod @staticmethod
def patch_resource(repo_url: str, assets_filter: str) -> list[str]: def _extract_repo_owner_and_tag(url: str) -> Tuple[str, str, str]:
"""Fetch patch resource from repo url.""" """Extract repo owner and url from github url."""
latest_resource_version: List[str] = latest( parsed_url = urlparse(url)
repo_url, assets_filter=assets_filter, output_format="assets" path_segments = parsed_url.path.strip("/").split("/")
github_repo_owner = path_segments[0]
github_repo_name = path_segments[1]
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
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"]
if match := filter_pattern.search(assets_url):
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."""
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
+12
View File
@@ -2,3 +2,15 @@ class APKMirrorScrapperFailure(Exception):
"""Failed to scrap icon from apkmirror.""" """Failed to scrap icon from apkmirror."""
pass pass
class ExtraAssetsFailure(Exception):
"""Failed to scrap icon from apkmirror."""
pass
class PatchingFailed(Exception):
"""Patching Failed."""
pass
+2 -3
View File
@@ -1,5 +1,4 @@
"""Revanced Parser.""" """Revanced Parser."""
import sys
from subprocess import PIPE, Popen from subprocess import PIPE, Popen
from time import perf_counter from time import perf_counter
from typing import List from typing import List
@@ -8,6 +7,7 @@ from loguru import logger
from src.app import APP from src.app import APP
from src.config import RevancedConfig from src.config import RevancedConfig
from src.exceptions import PatchingFailed
from src.patches import Patches from src.patches import Patches
from src.utils import possible_archs from src.utils import possible_archs
@@ -112,8 +112,7 @@ class Parser(object):
process = Popen(["java", *args], stdout=PIPE) process = Popen(["java", *args], stdout=PIPE)
output = process.stdout output = process.stdout
if not output: if not output:
logger.error("Failed to send request for patching.") raise PatchingFailed("Failed to send request for patching.")
sys.exit(-1)
for line in output: for line in output:
logger.debug(line.decode(), flush=True, end="") logger.debug(line.decode(), flush=True, end="")
process.wait() process.wait()
+5 -9
View File
@@ -8,6 +8,7 @@ from loguru import logger
from requests import Response from requests import Response
from src.config import RevancedConfig from src.config import RevancedConfig
from src.exceptions import PatchingFailed
default_build = [ default_build = [
"youtube", "youtube",
@@ -43,12 +44,6 @@ class AppNotFound(ValueError):
pass pass
class PatcherDownloadFailed(Exception):
"""Not a valid Revanced App."""
pass
class PatchesJsonFailed(ValueError): class PatchesJsonFailed(ValueError):
"""Patches failed.""" """Patches failed."""
@@ -59,8 +54,9 @@ def handle_response(response: Response) -> None:
"""Handle Get Request Response.""" """Handle Get Request Response."""
response_code = response.status_code response_code = response.status_code
if response_code != 200: if response_code != 200:
logger.error(response.text) raise PatchingFailed(
exit(1) f"Unable to downloaded assets from GitHub. Reason - {response.text}"
)
def slugify(string: str) -> str: def slugify(string: str) -> str:
@@ -98,7 +94,7 @@ def check_java(dry_run: bool) -> None:
raise subprocess.CalledProcessError(-1, "java -version") raise subprocess.CalledProcessError(-1, "java -version")
logger.debug("Cool!! Java is available") logger.debug("Cool!! Java is available")
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
logger.debug("Java>= 17 Must be installed") logger.error("Java>= 17 Must be installed")
exit(-1) exit(-1)