diff --git a/.github/workflows/build-apk.yml b/.github/workflows/build-apk.yml index a4ebfbe..5effabc 100644 --- a/.github/workflows/build-apk.yml +++ b/.github/workflows/build-apk.yml @@ -36,6 +36,11 @@ on: type: boolean required: false default: true + DEBUG_ENABLED: + type: boolean + description: 'Run the build with tmate debugging enabled.' + required: false + default: false concurrency: group: ${{ github.head_ref || github.run_id }} @@ -45,6 +50,7 @@ jobs: uses: ./.github/workflows/build-artifact.yml with: COMMIT_CHANGELOG: ${{ inputs.COMMIT_CHANGELOG }} + DEBUG_ENABLED: ${{ inputs.DEBUG_ENABLED }} secrets: ENVS: ${{ secrets.ENVS }} diff --git a/.github/workflows/build-artifact.yml b/.github/workflows/build-artifact.yml index 9b252f9..ff0c05e 100644 --- a/.github/workflows/build-artifact.yml +++ b/.github/workflows/build-artifact.yml @@ -22,6 +22,11 @@ on: type: boolean required: false default: false + DEBUG_ENABLED: + type: boolean + description: 'Run the build with tmate debugging enabled.' + required: false + default: false jobs: build-apk: @@ -37,9 +42,13 @@ jobs: run: | echo "${{ secrets.ENVS }}" >> .env + - name: Setup tmate session + uses: mxschmitt/action-tmate@v3 + if: ${{ github.event_name == 'workflow_dispatch' && inputs.DEBUG_ENABLED }} + - name: Build Revanced APKs run: | - docker-compose up + docker-compose up --build - name: Upload Build APKS uses: nikhilbadyal/upload-artifact@count-check diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index 611e549..3e765e8 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -20,3 +20,4 @@ jobs: ENVS: | CI_TEST=True ES_JAVA_OPTS: '-Xms4g -Xmx4g' + PERSONAL_ACCESS_TOKEN=${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 22f64d6..d7bb86a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ venv */__pycache__* *.pyc /revanced-cache/ +changelog.md diff --git a/README.md b/README.md index c5e89d9..dcc2308 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,12 @@ By default, script build the version as recommended by Revanced team. `/apks` folder. Name of the downloaded apk must match with the available app choices found [here.](#note) -14. Sample Envs
+14. If you run script again & again. You might hit GitHub API limit. In that case you can provide your Personal + GitHub Access Token in `.env` file or in `ENVS` in `GitHub secrets` (Recommended) in the format - + ```dotenv + PERSONAL_ACCESS_TOKEN= + ``` +15. Sample Envs
Thanks to [@aliharslan0](https://github.com/aliharslan0/pyrevanced) for his work. diff --git a/src/config.py b/src/config.py index f6a24fc..95a7fc5 100644 --- a/src/config.py +++ b/src/config.py @@ -84,3 +84,4 @@ class RevancedConfig: "ALTERNATIVE_YOUTUBE_MUSIC_PATCHES", [] ) self.existing_downloaded_apks = env.list("EXISTING_DOWNLOADED_APKS", []) + self.personal_access_token = env.str("PERSONAL_ACCESS_TOKEN", None) diff --git a/src/downloader.py b/src/downloader.py index 809f11d..649125a 100644 --- a/src/downloader.py +++ b/src/downloader.py @@ -14,7 +14,7 @@ from tqdm import tqdm from src.config import RevancedConfig from src.patches import Patches -from src.utils import AppNotFound, update_changelog +from src.utils import AppNotFound, handle_response, update_changelog class Downloader(object): @@ -32,8 +32,19 @@ class Downloader(object): logger.debug(f"Trying to download {file_name} from {url}") self._QUEUE_LENGTH += 1 start = perf_counter() - resp = self.config.session.get(url, stream=True) - total = int(resp.headers.get("content-length", 0)) + headers = {} + if self.config.personal_access_token and "github" in url: + logger.debug("Using personal access token") + headers.update( + {"Authorization": "token " + self.config.personal_access_token} + ) + response = self.config.session.get( + url, + stream=True, + headers=headers, + ) + handle_response(response) + total = int(response.headers.get("content-length", 0)) bar = tqdm( desc=file_name, total=total, @@ -43,7 +54,7 @@ class Downloader(object): colour="green", ) with self.config.temp_folder.joinpath(file_name).open("wb") as dl_file, bar: - for chunk in resp.iter_content(self._CHUNK_SIZE): + for chunk in response.iter_content(self._CHUNK_SIZE): size = dl_file.write(chunk) bar.update(size) self._QUEUE.put((perf_counter() - start, file_name)) @@ -176,7 +187,7 @@ class Downloader(object): match = re.search(r"\d", main_page) if not match: logger.error("Cannot find app main page") - sys.exit(-1) + raise AppNotFound() int_version = match.start() extra_release = main_page.rfind("release") - 1 version: str = main_page[int_version:extra_release] @@ -197,14 +208,21 @@ class Downloader(object): """ logger.debug(f"Trying to download {name} from github") repo_url = f"https://api.github.com/repos/{owner}/{name}/releases/latest" - r = requests.get( - repo_url, headers={"Content-Type": "application/vnd.github.v3+json"} - ) + headers = { + "Content-Type": "application/vnd.github.v3+json", + } + if self.config.personal_access_token: + logger.debug("Using personal access token") + headers.update( + {"Authorization": "token " + self.config.personal_access_token} + ) + response = requests.get(repo_url, headers=headers) + handle_response(response) if name == "revanced-patches": - download_url = r.json()["assets"][1]["browser_download_url"] + download_url = response.json()["assets"][1]["browser_download_url"] else: - download_url = r.json()["assets"][0]["browser_download_url"] - update_changelog(f"{owner}/{name}", r.json()) + download_url = response.json()["assets"][0]["browser_download_url"] + update_changelog(f"{owner}/{name}", response.json()) self._download(download_url, file_name=file_name) def download_revanced(self) -> None: diff --git a/src/patches.py b/src/patches.py index 48b5edb..eaa6b84 100644 --- a/src/patches.py +++ b/src/patches.py @@ -6,7 +6,7 @@ from loguru import logger from requests import Session from src.config import RevancedConfig -from src.utils import AppNotFound +from src.utils import AppNotFound, handle_response class Patches(object): @@ -60,10 +60,11 @@ class Patches(object): session = Session() logger.debug("fetching all patches") - resp = session.get( + response = session.get( "https://raw.githubusercontent.com/revanced/revanced-patches/main/patches.json" ) - patches = resp.json() + handle_response(response) + patches = response.json() for app_name in (self.revanced_app_ids[x][1] for x in self.revanced_app_ids): setattr(self, app_name, []) @@ -83,8 +84,9 @@ class Patches(object): else: url = "https://raw.githubusercontent.com/revanced/revanced-patches/main/patches.json" - resp_extended = session.get(url) - extended_patches = resp_extended.json() + response = session.get(url) + handle_response(response) + extended_patches = response.json() for app_name in ( self.revanced_extended_app_ids[x][1] for x in self.revanced_extended_app_ids ): diff --git a/src/utils.py b/src/utils.py index e66c109..56e6dde 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,6 +1,9 @@ """Utilities.""" from typing import Dict +from loguru import logger +from requests import Response + default_build = [ "youtube", ] @@ -55,3 +58,10 @@ class AppNotFound(ValueError): """Not a valid Revanced App.""" pass + + +def handle_response(response: Response) -> None: + """Handle Get Request Response.""" + response_code = response.status_code + if response_code != 200: + logger.info(response.text)