mirror of
https://github.com/sotam0316/docker-py-revanced.git
synced 2026-04-25 03:48:37 +09:00
💚 Fixed Branch prefix for bugs
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user