mirror of
https://github.com/sotam0316/docker-py-revanced.git
synced 2026-04-25 03:48:37 +09:00
123 lines
3.4 KiB
Python
123 lines
3.4 KiB
Python
"""Utilities."""
|
|
import os
|
|
import re
|
|
import subprocess
|
|
from typing import Dict
|
|
|
|
from loguru import logger
|
|
from requests import Response
|
|
|
|
from src.config import RevancedConfig
|
|
|
|
default_build = [
|
|
"youtube",
|
|
"youtube_music",
|
|
]
|
|
possible_archs = ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
|
|
|
|
|
def update_changelog(name: str, response: Dict[str, str]) -> None:
|
|
"""Updated Changelog."""
|
|
parent_repo = "https://github.com/nikhilbadyal/docker-py-revanced"
|
|
with open("changelog.md", "a", encoding="utf_8") as file1:
|
|
collapse_start = f"\n<details> <summary>👀 {name} </summary>\n\n"
|
|
release_version = f"**Release Version** - [{response['tag_name']}]({response['html_url']})<br>"
|
|
change_log = f"**Changelog** -<br> {response['body']}"
|
|
publish_time = f"**Published at** -<br> {response['published_at']}"
|
|
footer = f"<br><sub>Change logs generated by [Docker Py Revanced]({parent_repo})</sub>\n"
|
|
collapse_end = "</details>"
|
|
change_log = (
|
|
collapse_start
|
|
+ release_version
|
|
+ change_log
|
|
+ publish_time
|
|
+ footer
|
|
+ collapse_end
|
|
)
|
|
file1.write(change_log)
|
|
|
|
|
|
class AppNotFound(ValueError):
|
|
"""Not a valid Revanced App."""
|
|
|
|
pass
|
|
|
|
|
|
class PatcherDownloadFailed(Exception):
|
|
"""Not a valid Revanced App."""
|
|
|
|
pass
|
|
|
|
|
|
class PatchesJsonFailed(ValueError):
|
|
"""Patches failed."""
|
|
|
|
pass
|
|
|
|
|
|
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)
|
|
|
|
|
|
def slugify(string: str) -> str:
|
|
"""Converts a string to a slug format."""
|
|
# Convert to lowercase
|
|
string = string.lower()
|
|
|
|
# Remove special characters
|
|
string = re.sub(r"[^\w\s-]", "", string)
|
|
|
|
# Replace spaces with dashes
|
|
string = re.sub(r"\s+", "-", string)
|
|
|
|
# Remove consecutive dashes
|
|
string = re.sub(r"-+", "-", string)
|
|
|
|
# Remove leading and trailing dashes
|
|
string = string.strip("-")
|
|
|
|
return string
|
|
|
|
|
|
def check_java(dry_run: bool) -> None:
|
|
"""Check if Java>=17 is installed."""
|
|
try:
|
|
if dry_run:
|
|
return
|
|
jd = subprocess.check_output(
|
|
["java", "-version"], stderr=subprocess.STDOUT
|
|
).decode("utf-8")
|
|
jd = jd[1:-1]
|
|
if "Runtime Environment" not in jd:
|
|
raise subprocess.CalledProcessError(-1, "java -version")
|
|
if "17" not in jd and "20" not in jd:
|
|
raise subprocess.CalledProcessError(-1, "java -version")
|
|
logger.debug("Cool!! Java is available")
|
|
except subprocess.CalledProcessError:
|
|
logger.debug("Java>= 17 Must be installed")
|
|
exit(-1)
|
|
|
|
|
|
def extra_downloads(config: RevancedConfig) -> None:
|
|
"""Download extra files."""
|
|
from src.app import APP
|
|
|
|
try:
|
|
for extra in config.extra_download_files:
|
|
url, file_name = extra.split("@")
|
|
file_name_without_extension, file_extension = os.path.splitext(file_name)
|
|
|
|
if file_extension.lower() == ".apk":
|
|
new_file_name = file_name_without_extension + "-output" + file_extension
|
|
else:
|
|
raise ValueError("Only .apk extensions are allowed.")
|
|
APP.download(url, config, assets_filter="", file_name=new_file_name)
|
|
except ValueError:
|
|
logger.info(
|
|
"Unable to download extra file. Provide input in url@name.apk format."
|
|
)
|