🥅 Exception handling (#290)

This commit is contained in:
Nikhil Badyal
2023-08-17 21:09:42 +05:30
committed by GitHub
parent 5bffa4c79c
commit 02e550585c
11 changed files with 105 additions and 35 deletions
+16 -6
View File
@@ -7,7 +7,7 @@ from loguru import logger
from scripts.status_check import headers
from src.downloader.download import Downloader
from src.exceptions import AppNotFound
from src.exceptions import APKMirrorAPKDownloadFailure, APKMirrorAPKNotFound
from src.utils import apkmirror_status_check, bs4_parser
@@ -25,7 +25,9 @@ class ApkMirror(Downloader):
return self._download(
self.config.apk_mirror + possible_link["href"], f"{app}.apk"
)
raise AppNotFound(f"Unable to download apk from {link}")
raise APKMirrorAPKDownloadFailure(
f"Unable to extract force download for {app}", url=link
)
def extract_download_link(self, main_page: str, app: str) -> None:
"""Function to extract the download link from apkmirror html page.
@@ -49,7 +51,9 @@ class ApkMirror(Downloader):
self.config.apk_mirror + final_download_link, app
)
else:
raise AppNotFound(f"Unable to download apk from {main_page}")
raise APKMirrorAPKDownloadFailure(
f"Unable to extract link from {app} version list", url=main_page
)
def get_download_page(self, main_page: str) -> str:
"""Function to get the download page in apk_mirror.
@@ -67,7 +71,9 @@ class ApkMirror(Downloader):
sub_url = row.find(class_="accent_color")["href"]
break
if not sub_url:
raise AppNotFound("Unable to download apk from APKMirror.")
raise APKMirrorAPKDownloadFailure(
"Unable to extract download page", url=main_page
)
return f"{self.config.apk_mirror}{sub_url}"
@staticmethod
@@ -75,7 +81,11 @@ class ApkMirror(Downloader):
"""Extract search div."""
r = requests.get(url, headers=headers)
if r.status_code != 200:
raise AppNotFound(f"Unable to connect with {url} on ApkMirror.")
raise APKMirrorAPKDownloadFailure(
f"Unable to connect with {url} on ApkMirror. Are you blocked by APKMirror or abused apkmirror "
f"?.Reason - {r.text}",
url=url,
)
soup = BeautifulSoup(r.text, bs4_parser)
return soup.find(class_=search_class)
@@ -108,4 +118,4 @@ class ApkMirror(Downloader):
f"Trying to download {app}'s latest version({version}) from apkmirror"
)
return self.specific_version(app, version)
raise AppNotFound("App not found on apkmirror.")
raise APKMirrorAPKNotFound("App not found on apkmirror.")
+2 -8
View File
@@ -4,7 +4,7 @@ from typing import Any
from loguru import logger
from src.downloader.download import Downloader
from src.exceptions import AppNotFound
from src.patches import Patches
class ApkPure(Downloader):
@@ -17,13 +17,7 @@ class ApkPure(Downloader):
:param app: Name of the application
:return: Version of downloaded apk
"""
package_name = None
for package, app_tuple in self.patcher.revanced_app_ids.items():
if app_tuple[0] == app:
package_name = package
if not package_name:
logger.info("Unable to download from apkpure")
raise AppNotFound()
package_name = Patches.get_package_name(app)
download_url = f"https://d.apkpure.com/b/APK/{package_name}?version=latest"
self._download(download_url, f"{app}.apk")
logger.debug(f"Downloaded {app} apk from apk_pure_downloader in rt")
+2 -2
View File
@@ -6,7 +6,7 @@ from bs4 import BeautifulSoup
from scripts.status_check import headers
from src.downloader.download import Downloader
from src.exceptions import AppNotFound
from src.exceptions import APKSosAPKDownloadFailure
from src.utils import bs4_parser
@@ -26,7 +26,7 @@ class ApkSos(Downloader):
for possible_link in possible_links:
if possible_link.get("href"):
return self._download(possible_link["href"], f"{app}.apk")
raise AppNotFound("Unable to download apk from apk_combo")
raise APKSosAPKDownloadFailure(f"Unable to download {app}", url=page)
def latest_version(self, app: str, **kwargs: Any) -> None:
"""Function to download whatever the latest version of app from
+2 -2
View File
@@ -10,7 +10,7 @@ from tqdm import tqdm
from src.config import RevancedConfig
from src.downloader.utils import implement_method
from src.exceptions import PatchingFailed
from src.exceptions import DownloadFailure
from src.patches import Patches
from src.utils import handle_github_response
@@ -37,7 +37,7 @@ class Downloader(object):
def _download(self, url: str, file_name: str) -> None:
if not url:
raise PatchingFailed("No download to download")
raise DownloadFailure("No url provided to download")
if self.file_status_check(
self.config.temp_folder.joinpath(file_name), self.config.dry_run, url
):
+4 -2
View File
@@ -8,7 +8,7 @@ from loguru import logger
from src.config import RevancedConfig
from src.downloader.download import Downloader
from src.exceptions import PatchingFailed
from src.exceptions import DownloadFailure
from src.utils import handle_github_response, update_changelog
@@ -84,7 +84,9 @@ class Github(Downloader):
try:
filter_pattern = re.compile(asset_filter)
except re.error as e:
raise PatchingFailed("Invalid regex pattern provided.") from e
raise DownloadFailure(
f"Invalid regex {asset_filter} pattern provided."
) from e
for asset in assets:
assets_url = asset["browser_download_url"]
assets_name = asset["name"]
+7 -3
View File
@@ -7,7 +7,7 @@ from loguru import logger
from scripts.status_check import headers
from src.downloader.download import Downloader
from src.exceptions import AppNotFound
from src.exceptions import UptoDownAPKDownloadFailure
from src.utils import bs4_parser
@@ -20,7 +20,9 @@ class UptoDown(Downloader):
soup = soup.find(id="detail-download-button")
download_url = soup.get("data-url")
if not download_url:
raise AppNotFound("Unable to download from uptodown.")
raise UptoDownAPKDownloadFailure(
f"Unable to download {app} from uptodown.", url=page
)
self._download(download_url, f"{app}.apk")
logger.debug(f"Downloaded {app} apk from upto_down_downloader in rt")
@@ -45,7 +47,9 @@ class UptoDown(Downloader):
download_url = version_item["data-url"]
break
if download_url is None:
raise AppNotFound(f"Unable to get download url for {app}")
raise UptoDownAPKDownloadFailure(
f"Unable to download {app} from uptodown.", url=url
)
self.extract_download_link(download_url, app)
logger.debug(f"Downloaded {app} apk from upto_down_downloader in rt")
+63 -3
View File
@@ -16,6 +16,57 @@ class APKMirrorIconScrapFailure(Exception):
self.url = kwargs.get("url", None)
class DownloadFailure(Exception):
"""Generic Download failure."""
def __init__(self, *args: Any, **kwargs: Any) -> None:
"""Initialize the APKMirrorAPKDownloadFailure exception.
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
url (str, optional): The URL of the failed icon scraping. Defaults to None.
"""
super().__init__(*args)
self.url = kwargs.get("url", None)
class APKDownloadFailure(DownloadFailure):
"""Exception raised when the apk cannot be scraped."""
pass
class APKMirrorAPKDownloadFailure(APKDownloadFailure):
"""Exception raised when downloading an APK from apkmirror failed."""
pass
class APKMirrorAPKNotFound(APKDownloadFailure):
"""Exception raised when apk doesn't exist on APKMirror."""
pass
class UptoDownAPKDownloadFailure(APKDownloadFailure):
"""Exception raised when downloading an APK from uptodown failed."""
pass
class APKPureAPKDownloadFailure(APKDownloadFailure):
"""Exception raised when downloading an APK from apkpure failed."""
pass
class APKSosAPKDownloadFailure(APKDownloadFailure):
"""Exception raised when downloading an APK from apksos failed."""
pass
class PatchingFailed(Exception):
"""Patching Failed."""
@@ -28,7 +79,16 @@ class AppNotFound(ValueError):
pass
class PatchesJsonFailed(ValueError):
"""Patches failed."""
class PatchesJsonLoadFailed(ValueError):
"""Failed to load patches json."""
pass
def __init__(self, *args: Any, **kwargs: Any) -> None:
"""Initialize the PatchesJsonLoadFailed exception.
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
file_name (str, optional): The name of json file. Defaults to None.
"""
super().__init__(*args)
self.file_name = kwargs.get("file_name", None)
+4 -4
View File
@@ -8,7 +8,7 @@ from loguru import logger
from src.app import APP
from src.config import RevancedConfig
from src.exceptions import AppNotFound, PatchesJsonFailed
from src.exceptions import AppNotFound, PatchesJsonLoadFailed
class Patches(object):
@@ -66,7 +66,7 @@ class Patches(object):
for package, app_tuple in Patches.revanced_app_ids.items():
if app_tuple[0] == app:
return package
raise AppNotFound("App Not Found.")
raise AppNotFound(f"App {app} not supported yet.")
@staticmethod
def support_app() -> Dict[str, str]:
@@ -113,7 +113,7 @@ class Patches(object):
app_names = {value[0]: value[1] for value in self.revanced_app_ids.values()}
if not (app_name := app_names.get(app)):
raise AppNotFound(f"App '{app}' not found in the supported apps.")
raise AppNotFound(f"App {app} not supported yet.")
patches = getattr(self, app_name)
version = "latest"
@@ -176,4 +176,4 @@ class PatchLoader:
patches = json.load(f)
return patches
except FileNotFoundError as e:
raise PatchesJsonFailed() from e
raise PatchesJsonLoadFailed("File not found", file_name=file_name) from e
+2 -2
View File
@@ -9,7 +9,7 @@ from loguru import logger
from requests import Response
from src.config import RevancedConfig
from src.exceptions import PatchingFailed
from src.exceptions import DownloadFailure
default_build = [
"youtube",
@@ -71,7 +71,7 @@ def handle_github_response(response: Response) -> None:
"""Handle Get Request Response."""
response_code = response.status_code
if response_code != 200:
raise PatchingFailed(
raise DownloadFailure(
f"Unable to downloaded assets from GitHub. Reason - {response.text}"
)