Skip to content

Commit

Permalink
Merge pull request nikhilbadyal#274 from nikhilbadyal/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
nikhilbadyal authored Aug 10, 2023
2 parents 51c4983 + 677f302 commit 618ebd4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 49 deletions.
11 changes: 6 additions & 5 deletions TODOs.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## Things to work on

| | |
|:--------------------------------------------|---------------:|
| Parallelize app object creation. | <li> [ ] </li> |
| Parallelize app patching. | <li> [ ] </li> |
| Ability to provide local patching resources | <li> [X] </li> |
| | |
|:------------------------------------------------------|---------------:|
| Parallelize app object creation. | <li> [ ] </li> |
| Parallelize app patching. | <li> [ ] </li> |
| Ability to provide local patching resources | <li> [X] </li> |
| Ability to provide changelog repo in update_changelog | <li> [ ] </li> |
4 changes: 2 additions & 2 deletions scripts/status_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from src.exceptions import APKMirrorScrapperFailure
from src.patches import Patches
from src.utils import handle_response
from src.utils import handle_github_response

not_found_icon = "https://img.icons8.com/bubbles/500/android-os.png"
headers = {
Expand Down Expand Up @@ -105,7 +105,7 @@ def generate_markdown_table(data: List[List[str]]) -> str:
def main() -> None:
repo_url = "https://api.revanced.app/v2/patches/latest"
response = requests.get(repo_url)
handle_response(response)
handle_github_response(response)

parsed_data = response.json()
compatible_packages = parsed_data["patches"]
Expand Down
4 changes: 2 additions & 2 deletions src/downloader/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from src.downloader.utils import implement_method
from src.exceptions import PatchingFailed
from src.patches import Patches
from src.utils import handle_response
from src.utils import handle_github_response


class Downloader(object):
Expand Down Expand Up @@ -54,7 +54,7 @@ def _download(self, url: str, file_name: str) -> None:
stream=True,
headers=headers,
)
handle_response(response)
handle_github_response(response)
total = int(response.headers.get("content-length", 0))
bar = tqdm(
desc=file_name,
Expand Down
6 changes: 3 additions & 3 deletions src/downloader/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from src.config import RevancedConfig
from src.downloader.download import Downloader
from src.utils import handle_response, update_changelog
from src.utils import handle_github_response, update_changelog


class Github(Downloader):
Expand All @@ -35,7 +35,7 @@ def latest_version(self, app: str, **kwargs: Dict[str, str]) -> None:
logger.debug("Using personal access token")
headers["Authorization"] = f"token {self.config.personal_access_token}"
response = requests.get(repo_url, headers=headers)
handle_response(response)
handle_github_response(response)
if repo_name == "revanced-patches":
download_url = response.json()["assets"][1]["browser_download_url"]
else:
Expand Down Expand Up @@ -78,7 +78,7 @@ def _get_release_assets(
if config.personal_access_token:
headers["Authorization"] = f"token {config.personal_access_token}"
response = requests.get(api_url, headers=headers)
handle_response(response)
handle_github_response(response)
assets = response.json()["assets"]
try:
filter_pattern = re.compile(asset_filter)
Expand Down
22 changes: 15 additions & 7 deletions src/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
class Parser(object):
"""Revanced Parser."""

CLI_JAR = "-jar"
APK_ARG = "-a"
PATCHES_ARG = "-b"
INTEGRATIONS_ARG = "-m"
OUTPUT_ARG = "-o"
KEYSTORE_ARG = "--keystore"
OPTIONS_ARG = "--options"

def __init__(self, patcher: Patches, config: RevancedConfig) -> None:
self._PATCHES: List[str] = []
self._EXCLUDED: List[str] = []
Expand Down Expand Up @@ -78,19 +86,19 @@ def patch_app(
:param app: Name of the app
"""
args = [
"-jar",
self.CLI_JAR,
app.resource["cli"],
"-a",
self.APK_ARG,
f"{app.app_name}.apk",
"-b",
self.PATCHES_ARG,
app.resource["patches"],
"-m",
self.INTEGRATIONS_ARG,
app.resource["integrations"],
"-o",
self.OUTPUT_ARG,
app.get_output_file_name(),
"--keystore",
self.KEYSTORE_ARG,
app.keystore_name,
"--options",
self.OPTIONS_ARG,
"options.json",
]
if app.experiment:
Expand Down
28 changes: 16 additions & 12 deletions src/patches.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Revanced Patches."""
import json
import os
from typing import Any, Dict, List, Tuple

from loguru import logger
Expand Down Expand Up @@ -64,18 +63,10 @@ def support_app() -> Dict[str, str]:
"""Return supported apps."""
return Patches._revanced_app_ids

def scrap_patches(self, file_name: str) -> Any:
"""Scrap Patches."""
if os.path.exists(file_name):
with open(file_name) as f:
patches = json.load(f)
return patches
raise PatchesJsonFailed()

# noinspection DuplicatedCode
def fetch_patches(self, config: RevancedConfig, app: APP) -> None:
"""Function to fetch all patches."""
patches = self.scrap_patches(
patch_loader = PatchLoader()
patches = patch_loader.load_patches(
f'{config.temp_folder}/{app.resource["patches_json"]}'
)
for app_name in (self.revanced_app_ids[x][1] for x in self.revanced_app_ids):
Expand Down Expand Up @@ -121,7 +112,6 @@ def get(self, app: str) -> Tuple[List[Dict[str, str]], str]:
pass
return patches, version

# noinspection IncorrectFormatting
def include_exclude_patch(
self, app: APP, parser: Any, patches: List[Dict[str, str]]
) -> None:
Expand Down Expand Up @@ -164,3 +154,17 @@ def get_app_configs(self, app: "APP") -> List[Dict[str, str]]:
recommended_version = app.app_version
app.set_recommended_version(recommended_version, experiment)
return total_patches


class PatchLoader:
"""Patch Loader."""

@staticmethod
def load_patches(file_name: str) -> Any:
"""Load patches from a file."""
try:
with open(file_name) as f:
patches = json.load(f)
return patches
except FileNotFoundError:
raise PatchesJsonFailed()
39 changes: 21 additions & 18 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ def update_changelog(name: str, response: Dict[str, str]) -> None:
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
change_log = "".join(
[
collapse_start,
release_version,
change_log,
publish_time,
footer,
collapse_end,
]
)
file1.write(change_log)


def handle_response(response: Response) -> None:
def handle_github_response(response: Response) -> None:
"""Handle Get Request Response."""
response_code = response.status_code
if response_code != 200:
Expand All @@ -50,21 +52,21 @@ def handle_response(response: Response) -> None:
def slugify(string: str) -> str:
"""Converts a string to a slug format."""
# Convert to lowercase
string = string.lower()
modified_string = string.lower()

# Remove special characters
string = re.sub(r"[^\w\s-]", "", string)
modified_string = re.sub(r"[^\w\s-]", "", modified_string)

# Replace spaces with dashes
string = re.sub(r"\s+", "-", string)
modified_string = re.sub(r"\s+", "-", modified_string)

# Remove consecutive dashes
string = re.sub(r"-+", "-", string)
modified_string = re.sub(r"-+", "-", modified_string)

# Remove leading and trailing dashes
string = string.strip("-")
modified_string = modified_string.strip("-")

return string
return modified_string


def check_java(dry_run: bool) -> None:
Expand Down Expand Up @@ -95,10 +97,11 @@ def extra_downloads(config: RevancedConfig) -> None:
url, file_name = extra.split("@")
file_name_without_extension, file_extension = os.path.splitext(file_name)

if file_extension.lower() == ".apk":
new_file_name = f"{file_name_without_extension}-output{file_extension}"
else:
raise ValueError("Only .apk extensions are allowed.")
if file_extension.lower() != ".apk":
logger.info(f"Only .apk extensions are allowed {file_name}.")
continue

new_file_name = f"{file_name_without_extension}-output{file_extension}"
APP.download(url, config, assets_filter=".*apk", file_name=new_file_name)
except (ValueError, IndexError):
logger.info(
Expand Down

0 comments on commit 618ebd4

Please sign in to comment.