Merge pull request #40 from nikhilbadyal/docs

Added DocStrings
This commit is contained in:
Nikhil Badyal
2022-10-11 23:09:29 +05:30
committed by GitHub
6 changed files with 101 additions and 1 deletions
+2
View File
@@ -1,3 +1,4 @@
"""Entry point."""
import sys import sys
from environs import Env from environs import Env
@@ -10,6 +11,7 @@ from src.patches import Patches
def main() -> None: def main() -> None:
"""Entry point."""
env = Env() env = Env()
config = RevancedConfig(env) config = RevancedConfig(env)
+3
View File
@@ -1,3 +1,4 @@
"""Revanced Configurations."""
from pathlib import Path from pathlib import Path
from typing import List from typing import List
@@ -8,6 +9,8 @@ from src.utils import supported_apps
class RevancedConfig: class RevancedConfig:
"""Revanced Configurations."""
def __init__(self, env: Env) -> None: def __init__(self, env: Env) -> None:
self.env = env self.env = env
self.temp_folder = Path("apks") self.temp_folder = Path("apks")
+50
View File
@@ -1,3 +1,4 @@
"""Downloader Class."""
import re import re
import sys import sys
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
@@ -14,6 +15,8 @@ from src.config import RevancedConfig
class Downloader(object): class Downloader(object):
"""Files downloader."""
def __init__(self, config: RevancedConfig): def __init__(self, config: RevancedConfig):
self._CHUNK_SIZE = 2**21 * 5 self._CHUNK_SIZE = 2**21 * 5
self._QUEUE: PriorityQueue[Tuple[float, str]] = PriorityQueue() self._QUEUE: PriorityQueue[Tuple[float, str]] = PriorityQueue()
@@ -43,6 +46,11 @@ class Downloader(object):
logger.debug(f"Downloaded {file_name}") logger.debug(f"Downloaded {file_name}")
def extract_download_link(self, page: str, app: str) -> None: def extract_download_link(self, page: str, app: str) -> None:
"""Function to extract the download link from apkmirror html page.
:param page: Url of the page
:param app: Name of the app
"""
logger.debug(f"Extracting download link from\n{page}") logger.debug(f"Extracting download link from\n{page}")
parser = LexborHTMLParser(self.config.session.get(page).text) parser = LexborHTMLParser(self.config.session.get(page).text)
@@ -58,6 +66,12 @@ class Downloader(object):
logger.debug("Finished Extracting link and downloading") logger.debug("Finished Extracting link and downloading")
def get_download_page(self, parser: LexborHTMLParser, main_page: str) -> str: def get_download_page(self, parser: LexborHTMLParser, main_page: str) -> str:
"""Function to get the download page in apk_mirror.
:param parser: Parser
:param main_page: Main Download Page in APK mirror(Index)
:return:
"""
apm = parser.css(".apkm-badge") apm = parser.css(".apkm-badge")
sub_url = "" sub_url = ""
for is_apm in apm: for is_apm in apm:
@@ -84,6 +98,12 @@ class Downloader(object):
return app_version return app_version
def apkmirror_specific_version(self, app: str, version: str) -> str: def apkmirror_specific_version(self, app: str, version: str) -> str:
"""Function to download the specified version of app from apkmirror.
:param app: Name of the application
:param version: Version of the application to download
:return: Version of downloaded apk
"""
logger.debug(f"Trying to download {app},specific version {version}") logger.debug(f"Trying to download {app},specific version {version}")
version = version.replace(".", "-") version = version.replace(".", "-")
main_page = f"{self.config.apk_mirror_version_urls.get(app)}-{version}-release/" main_page = f"{self.config.apk_mirror_version_urls.get(app)}-{version}-release/"
@@ -94,6 +114,12 @@ class Downloader(object):
return version return version
def apkmirror_latest_version(self, app: str) -> str: def apkmirror_latest_version(self, app: str) -> str:
"""Function to download whatever the latest version of app from
apkmirror.
:param app: Name of the application
:return: Version of downloaded apk
"""
logger.debug(f"Trying to download {app}'s latest version from apkmirror") logger.debug(f"Trying to download {app}'s latest version from apkmirror")
page = self.config.apk_mirror_urls.get(app) page = self.config.apk_mirror_urls.get(app)
if not page: if not page:
@@ -119,6 +145,12 @@ class Downloader(object):
return version return version
def repository(self, owner: str, name: str, file_name: str) -> None: def repository(self, owner: str, name: str, file_name: str) -> None:
"""Function to download files from GitHub repositories.
:param owner: github user/organization
:param name: name of the repository
:param file_name: name of the file after downloading
"""
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( r = requests.get(
@@ -131,6 +163,7 @@ class Downloader(object):
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:
"""Download Revanced and Extended Patches, Integration and CLI."""
assets = [ assets = [
["revanced", "revanced-cli", self.config.normal_cli_jar], ["revanced", "revanced-cli", self.config.normal_cli_jar],
["revanced", "revanced-integrations", self.config.normal_integrations_apk], ["revanced", "revanced-integrations", self.config.normal_integrations_apk],
@@ -148,15 +181,32 @@ class Downloader(object):
logger.info("Downloaded revanced microG ,cli, integrations and patches.") logger.info("Downloaded revanced microG ,cli, integrations and patches.")
def upto_down_downloader(self, app: str) -> str: def upto_down_downloader(self, app: str) -> str:
"""Function to download from UptoDown.
:param app: Name of the application
:return: Version of downloaded APK
"""
return self.__upto_down_downloader(app) return self.__upto_down_downloader(app)
def download_from_apkmirror(self, version: str, app: str) -> str: def download_from_apkmirror(self, version: str, app: str) -> str:
"""Function to download from apkmirror.
:param version: version to download
:param app: App to download
:return: Version of downloaded APK
"""
if version and version != "latest": if version and version != "latest":
return self.apkmirror_specific_version(app, version) return self.apkmirror_specific_version(app, version)
else: else:
return self.apkmirror_latest_version(app) return self.apkmirror_latest_version(app)
def download_apk_to_patch(self, version: str, app: str) -> str: def download_apk_to_patch(self, version: str, app: str) -> str:
"""Public function to download apk to patch.
:param version: version to download
:param app: App to download
:return: Version of apk
"""
if app in self.config.upto_down: if app in self.config.upto_down:
return self.upto_down_downloader(app) return self.upto_down_downloader(app)
else: else:
+21
View File
@@ -1,3 +1,4 @@
"""Revanced Parser."""
import sys import sys
from subprocess import PIPE, Popen from subprocess import PIPE, Popen
from time import perf_counter from time import perf_counter
@@ -10,6 +11,8 @@ from src.patches import Patches
class Parser(object): class Parser(object):
"""Revanced Parser."""
def __init__(self, patcher: Patches, config: RevancedConfig) -> None: def __init__(self, patcher: Patches, config: RevancedConfig) -> None:
self._PATCHES: List[str] = [] self._PATCHES: List[str] = []
self._EXCLUDED: List[str] = [] self._EXCLUDED: List[str] = []
@@ -17,16 +20,34 @@ class Parser(object):
self.config = config self.config = config
def include(self, name: str) -> None: def include(self, name: str) -> None:
"""Include a given patch.
:param name: Name of the patch
"""
self._PATCHES.extend(["-i", name]) self._PATCHES.extend(["-i", name])
def exclude(self, name: str) -> None: def exclude(self, name: str) -> None:
"""Exclude a given patch.
:param name: Name of the patch to exclude
"""
self._PATCHES.extend(["-e", name]) self._PATCHES.extend(["-e", name])
self._EXCLUDED.append(name) self._EXCLUDED.append(name)
def get_excluded_patches(self) -> List[str]: def get_excluded_patches(self) -> List[str]:
"""
Getter to get all excluded patches
:return: List of excluded patches
"""
return self._EXCLUDED return self._EXCLUDED
def patch_app(self, app: str, version: str, is_experimental: bool = False) -> None: def patch_app(self, app: str, version: str, is_experimental: bool = False) -> None:
"""Revanced APP Patcher.
:param app: Name of the app
:param version: Version of the application
:param is_experimental: Whether to enable experimental support
"""
logger.debug(f"Sending request to revanced cli for building {app} revanced") logger.debug(f"Sending request to revanced cli for building {app} revanced")
cli = self.config.normal_cli_jar cli = self.config.normal_cli_jar
patches = self.config.normal_patches_jar patches = self.config.normal_patches_jar
+24 -1
View File
@@ -1,3 +1,4 @@
"""Revanced Patches."""
import subprocess import subprocess
import sys import sys
from typing import Any, Dict, List, Tuple from typing import Any, Dict, List, Tuple
@@ -9,7 +10,11 @@ from src.config import RevancedConfig
class Patches(object): class Patches(object):
def check_java(self) -> None: """Revanced Patches."""
@staticmethod
def check_java() -> None:
"""Check if Java17 is installed."""
logger.debug("Checking if java is available") logger.debug("Checking if java is available")
jd = subprocess.check_output( jd = subprocess.check_output(
["java", "-version"], stderr=subprocess.STDOUT ["java", "-version"], stderr=subprocess.STDOUT
@@ -23,7 +28,9 @@ class Patches(object):
exit(-1) exit(-1)
logger.debug("Cool!! Java is available") logger.debug("Cool!! Java is available")
# noinspection DuplicatedCode
def fetch_patches(self) -> None: def fetch_patches(self) -> None:
"""Function to fetch all patches."""
session = Session() session = Session()
logger.debug("fetching all patches") logger.debug("fetching all patches")
@@ -93,6 +100,11 @@ class Patches(object):
self.fetch_patches() self.fetch_patches()
def get(self, app: str) -> Tuple[List[Dict[str, str]], str]: def get(self, app: str) -> Tuple[List[Dict[str, str]], str]:
"""Get all patches for the given app.
:param app: Name of the application
:return: Patches
"""
logger.debug("Getting patches for %s" % app) logger.debug("Getting patches for %s" % app)
app_names = { app_names = {
"reddit": "_reddit", "reddit": "_reddit",
@@ -118,6 +130,12 @@ class Patches(object):
def include_and_exclude_patches( def include_and_exclude_patches(
self, app: str, arg_parser: Any, app_patches: List[Dict[str, str]] self, app: str, arg_parser: Any, app_patches: List[Dict[str, str]]
) -> None: ) -> None:
"""Include and exclude patches for a given app.
:param app: Name of the app
:param arg_parser: Parser Obj
:param app_patches: All the patches of a given app
"""
logger.debug(f"Excluding patches for app {app}") logger.debug(f"Excluding patches for app {app}")
if self.config.build_extended and app in self.config.extended_apps: if self.config.build_extended and app in self.config.extended_apps:
excluded_patches = self.config.env.list( excluded_patches = self.config.env.list(
@@ -136,6 +154,11 @@ class Patches(object):
logger.debug(f"No excluded patches for {app}") logger.debug(f"No excluded patches for {app}")
def get_app_configs(self, app: str) -> Tuple[List[Dict[str, str]], str, bool]: def get_app_configs(self, app: str) -> Tuple[List[Dict[str, str]], str, bool]:
"""Get Configurations for a given app.
:param app: Name of the application
:return: All Patches , Its version and whether it is experimental
"""
experiment = False experiment = False
total_patches, recommended_version = self.get(app=app) total_patches, recommended_version = self.get(app=app)
env_version = self.config.env.str(f"{app}_VERSION".upper(), None) env_version = self.config.env.str(f"{app}_VERSION".upper(), None)
+1
View File
@@ -1,3 +1,4 @@
"""Utilities."""
supported_apps = [ supported_apps = [
"youtube", "youtube",
"youtube_music", "youtube_music",