Skip to content

Commit

Permalink
pythonlib: Look for assets in earlier releases #134 0.4.9
Browse files Browse the repository at this point in the history
  • Loading branch information
daijro committed Dec 13, 2024
1 parent 9a9e61f commit e3d3dcd
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 43 deletions.
8 changes: 8 additions & 0 deletions pythonlib/camoufox/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ class UnsupportedVersion(Exception):
...


class MissingRelease(Exception):
"""
Raised when a required GitHub release asset is missing.
"""

...


class UnsupportedArchitecture(Exception):
"""
Raised when the architecture is not supported.
Expand Down
24 changes: 21 additions & 3 deletions pythonlib/camoufox/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import numpy as np
from language_tags import tags

from camoufox.pkgman import LOCAL_DATA, rprint, webdl
from camoufox.pkgman import LOCAL_DATA, GitHubDownloader, rprint, webdl
from camoufox.warnings import LeakWarning

from .exceptions import (
InvalidLocale,
MissingRelease,
NotInstalledGeoIPExtra,
UnknownIPLocation,
UnknownLanguage,
Expand Down Expand Up @@ -189,7 +190,22 @@ def _join_unique(seq: Iterable[str]) -> str:
"""

MMDB_FILE = LOCAL_DATA / 'GeoLite2-City.mmdb'
MMDB_URL = 'https://github.com/P3TERX/GeoLite.mmdb/releases/latest/download/GeoLite2-City.mmdb'
MMDB_REPO = "P3TERX/GeoLite.mmdb"


class MaxMindDownloader(GitHubDownloader):
"""
MaxMind database downloader from a GitHub repository.
"""

def check_asset(self, asset: Dict) -> Optional[str]:
# Check for the first -City.mmdb file
if asset['name'].endswith('-City.mmdb'):
return asset['browser_download_url']
return None

def missing_asset_error(self) -> None:
raise MissingRelease('Failed to find GeoIP database release asset')


def geoip_allowed() -> None:
Expand All @@ -208,9 +224,11 @@ def download_mmdb() -> None:
"""
geoip_allowed()

asset_url = MaxMindDownloader(MMDB_REPO).get_asset()

with open(MMDB_FILE, 'wb') as f:
webdl(
MMDB_URL,
asset_url,
desc='Downloading GeoIP database',
buffer=f,
)
Expand Down
121 changes: 82 additions & 39 deletions pythonlib/camoufox/pkgman.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .__version__ import CONSTRAINTS
from .exceptions import (
CamoufoxNotInstalled,
MissingRelease,
UnsupportedArchitecture,
UnsupportedOS,
UnsupportedVersion,
Expand Down Expand Up @@ -136,12 +137,59 @@ def build_minmax() -> Tuple['Version', 'Version']:
VERSION_MIN, VERSION_MAX = Version.build_minmax()


class CamoufoxFetcher:
class GitHubDownloader:
"""
Manages fetching and installing GitHub releases.
"""

def __init__(self, github_repo: str) -> None:
self.github_repo = github_repo
self.api_url = f"https://api.github.com/repos/{github_repo}/releases"

def check_asset(self, asset: Dict) -> Any:
"""
Compare the asset to determine if it's the desired asset.
Args:
asset: Asset information from GitHub API
Returns:
Any: Data to be returned if this is the desired asset, or None/False if not
"""
return asset.get('browser_download_url')

def missing_asset_error(self) -> None:
"""
Raise a MissingRelease exception if no release is found.
"""
raise MissingRelease(f"Could not find a release asset in {self.github_repo}.")

def get_asset(self) -> Any:
"""
Fetch the latest release from the GitHub API.
Gets the first asset that returns a truthy value from check_asset.
"""
resp = requests.get(self.api_url, timeout=20)
resp.raise_for_status()

releases = resp.json()

for release in releases:
for asset in release['assets']:
if data := self.check_asset(asset):
return data

self.missing_asset_error()


class CamoufoxFetcher(GitHubDownloader):
"""
Handles fetching and installing the latest version of Camoufox.
"""

def __init__(self) -> None:
super().__init__("daijro/camoufox")

self.arch = self.get_platform_arch()
self._version_obj: Optional[Version] = None
self.pattern: re.Pattern = re.compile(
Expand All @@ -150,6 +198,37 @@ def __init__(self) -> None:

self.fetch_latest()

def check_asset(self, asset: Dict) -> Optional[Tuple[Version, str]]:
"""
Finds the latest release from a GitHub releases API response that
supports the Camoufox version constraints, the OS, and architecture.
Returns:
Optional[Tuple[Version, str]]: The version and URL of a release
"""
# Search through releases for the first supported version
match = self.pattern.match(asset['name'])
if not match:
return None

# Check if the version is supported
version = Version(release=match['release'], version=match['version'])
if not version.is_supported():
return None

# Asset was found. Return data
return version, asset['browser_download_url']

def missing_asset_error(self) -> None:
"""
Raise a MissingRelease exception if no release is found.
"""
raise MissingRelease(
f"No matching release found for {OS_NAME} {self.arch} in the "
f"supported range: ({CONSTRAINTS.as_range()}). "
"Please update the Python library."
)

@staticmethod
def get_platform_arch() -> str:
"""
Expand All @@ -175,30 +254,6 @@ def get_platform_arch() -> str:

return arch

def find_release(self, releases: List[Dict]) -> Optional[Tuple[Version, str]]:
"""
Finds the latest release from a GitHub releases API response that
supports the Camoufox version constraints, the OS, and architecture.
Returns:
Optional[Tuple[Version, str]]: The version and URL of a release
"""
# Search through releases for the first supported version
for release in releases:
for asset in release['assets']:
match = self.pattern.match(asset['name'])
if not match:
continue

# Check if the version is supported
version = Version(release=match['release'], version=match['version'])
if not version.is_supported():
continue

# Asset was found. Return data
return version, asset['browser_download_url']
return None

def fetch_latest(self) -> None:
"""
Fetch the URL of the latest camoufox release for the current platform.
Expand All @@ -208,20 +263,7 @@ def fetch_latest(self) -> None:
requests.RequestException: If there's an error fetching release data
ValueError: If no matching release is found for the current platform
"""
api_url = "https://api.github.com/repos/daijro/camoufox/releases"
resp = requests.get(api_url, timeout=20)
resp.raise_for_status()

# Find a release that fits the constraints
releases = resp.json()
release_data = self.find_release(releases)

if release_data is None:
raise UnsupportedVersion(
f"No matching release found for {OS_NAME} {self.arch} in the "
f"supported range: ({CONSTRAINTS.as_range()}). "
"Please update the Python library."
)
release_data = self.get_asset()

# Set the version and URL
self._version_obj, self._url = release_data
Expand All @@ -232,6 +274,7 @@ def download_file(file: DownloadBuffer, url: str) -> DownloadBuffer:
Download a file from the given URL and return it as BytesIO.
Args:
file (DownloadBuffer): The buffer to download to
url (str): The URL to download the file from
Returns:
Expand Down
2 changes: 1 addition & 1 deletion pythonlib/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "camoufox"
version = "0.4.8"
version = "0.4.9"
description = "Wrapper around Playwright to help launch Camoufox"
authors = ["daijro <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit e3d3dcd

Please sign in to comment.