Ability to provide GitHub Access Token to bypass GitHub Rate Limit.

This commit is contained in:
Nikhil Badyal
2022-12-25 12:48:43 +05:30
parent c6f6c8c9fd
commit 1ea1de8086
5 changed files with 50 additions and 16 deletions
+6 -1
View File
@@ -257,7 +257,12 @@ By default, script build the version as recommended by Revanced team.
`/apks` folder. `/apks` folder.
Name of the downloaded apk must match with the available app choices found [here.](#note) Name of the downloaded apk must match with the available app choices found [here.](#note)
14. Sample Envs<br> 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=<PAT>
```
15. Sample Envs<br>
<img src="https://i.imgur.com/ajSE5nA.png" width="600" style="left"> <img src="https://i.imgur.com/ajSE5nA.png" width="600" style="left">
Thanks to [@aliharslan0](https://github.com/aliharslan0/pyrevanced) for his work. Thanks to [@aliharslan0](https://github.com/aliharslan0/pyrevanced) for his work.
+1
View File
@@ -84,3 +84,4 @@ class RevancedConfig:
"ALTERNATIVE_YOUTUBE_MUSIC_PATCHES", [] "ALTERNATIVE_YOUTUBE_MUSIC_PATCHES", []
) )
self.existing_downloaded_apks = env.list("EXISTING_DOWNLOADED_APKS", []) self.existing_downloaded_apks = env.list("EXISTING_DOWNLOADED_APKS", [])
self.personal_access_token = env.str("PERSONAL_ACCESS_TOKEN", None)
+25 -9
View File
@@ -14,7 +14,7 @@ from tqdm import tqdm
from src.config import RevancedConfig from src.config import RevancedConfig
from src.patches import Patches from src.patches import Patches
from src.utils import AppNotFound, update_changelog from src.utils import AppNotFound, handle_response, update_changelog
class Downloader(object): class Downloader(object):
@@ -32,8 +32,18 @@ class Downloader(object):
logger.debug(f"Trying to download {file_name} from {url}") logger.debug(f"Trying to download {file_name} from {url}")
self._QUEUE_LENGTH += 1 self._QUEUE_LENGTH += 1
start = perf_counter() start = perf_counter()
resp = self.config.session.get(url, stream=True) headers = {}
total = int(resp.headers.get("content-length", 0)) if self.config.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( bar = tqdm(
desc=file_name, desc=file_name,
total=total, total=total,
@@ -43,7 +53,7 @@ class Downloader(object):
colour="green", colour="green",
) )
with self.config.temp_folder.joinpath(file_name).open("wb") as dl_file, bar: 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) size = dl_file.write(chunk)
bar.update(size) bar.update(size)
self._QUEUE.put((perf_counter() - start, file_name)) self._QUEUE.put((perf_counter() - start, file_name))
@@ -197,14 +207,20 @@ class Downloader(object):
""" """
logger.debug(f"Trying to download {name} from github") logger.debug(f"Trying to download {name} from github")
repo_url = f"https://api.github.com/repos/{owner}/{name}/releases/latest" repo_url = f"https://api.github.com/repos/{owner}/{name}/releases/latest"
r = requests.get( headers = {
repo_url, headers={"Content-Type": "application/vnd.github.v3+json"} "Content-Type": "application/vnd.github.v3+json",
}
if self.config.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": if name == "revanced-patches":
download_url = r.json()["assets"][1]["browser_download_url"] download_url = response.json()["assets"][1]["browser_download_url"]
else: else:
download_url = r.json()["assets"][0]["browser_download_url"] download_url = response.json()["assets"][0]["browser_download_url"]
update_changelog(f"{owner}/{name}", r.json()) update_changelog(f"{owner}/{name}", response.json())
self._download(download_url, file_name=file_name) self._download(download_url, file_name=file_name)
def download_revanced(self) -> None: def download_revanced(self) -> None:
+7 -5
View File
@@ -6,7 +6,7 @@ from loguru import logger
from requests import Session from requests import Session
from src.config import RevancedConfig from src.config import RevancedConfig
from src.utils import AppNotFound from src.utils import AppNotFound, handle_response
class Patches(object): class Patches(object):
@@ -60,10 +60,11 @@ class Patches(object):
session = Session() session = Session()
logger.debug("fetching all patches") logger.debug("fetching all patches")
resp = session.get( response = session.get(
"https://raw.githubusercontent.com/revanced/revanced-patches/main/patches.json" "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): for app_name in (self.revanced_app_ids[x][1] for x in self.revanced_app_ids):
setattr(self, app_name, []) setattr(self, app_name, [])
@@ -83,8 +84,9 @@ class Patches(object):
else: else:
url = "https://raw.githubusercontent.com/revanced/revanced-patches/main/patches.json" url = "https://raw.githubusercontent.com/revanced/revanced-patches/main/patches.json"
resp_extended = session.get(url) response = session.get(url)
extended_patches = resp_extended.json() handle_response(response)
extended_patches = response.json()
for app_name in ( for app_name in (
self.revanced_extended_app_ids[x][1] for x in self.revanced_extended_app_ids self.revanced_extended_app_ids[x][1] for x in self.revanced_extended_app_ids
): ):
+10
View File
@@ -1,6 +1,9 @@
"""Utilities.""" """Utilities."""
from typing import Dict from typing import Dict
from loguru import logger
from requests import Response
default_build = [ default_build = [
"youtube", "youtube",
] ]
@@ -55,3 +58,10 @@ class AppNotFound(ValueError):
"""Not a valid Revanced App.""" """Not a valid Revanced App."""
pass pass
def handle_response(response: Response) -> None:
"""Handle Get Request Response."""
response_code = response.status_code
if response_code != 200:
logger.info(response.text)