From f18b9a00be32a65a9685499f589cbbe428475e79 Mon Sep 17 00:00:00 2001 From: daijro Date: Thu, 19 Sep 2024 19:05:53 -0500 Subject: [PATCH] pythonlib: Add block_images, block_webrtc, fixes, etc. 0.1.3 - Adds block_images & block_webrtc parameters to Camoufox and AsyncCamoufox - Fixes async_api - Update README --- pythonlib/README.md | 32 +++++++++++++++---------- pythonlib/camoufox/addons.py | 2 +- pythonlib/camoufox/async_api.py | 42 +++++++++++++++++++++------------ pythonlib/camoufox/sync_api.py | 38 +++++++++++++++++++---------- pythonlib/camoufox/utils.py | 13 ++++++++++ pythonlib/pyproject.toml | 2 +- 6 files changed, 86 insertions(+), 43 deletions(-) diff --git a/pythonlib/README.md b/pythonlib/README.md index e0994aa..b7e7eee 100644 --- a/pythonlib/README.md +++ b/pythonlib/README.md @@ -84,28 +84,34 @@ Launches a new browser instance for Camoufox. Accepts all Playwright Firefox launch options, along with the following: Parameters: - playwright (Playwright): - The playwright instance to use. config (Optional[Dict[str, Any]]): - The configuration to use. + Configuration to use. addons (Optional[List[str]]): - The addons to use. + Addons to use. fingerprint (Optional[Fingerprint]): - The fingerprint to use. + BrowserForge fingerprint to use. exclude_addons (Optional[List[DefaultAddons]]): - The default addons to exclude, passed as a list of camoufox.DefaultAddons enums. + Default addons to exclude. Passed as a list of camoufox.DefaultAddons enums. screen (Optional[browserforge.fingerprints.Screen]): - The screen constraints to use. + BrowserForge screen constraints to use. os (Optional[ListOrString]): - The operating system to use for the fingerprint. Either a string or a list of strings. + Operating system to use for the fingerprint. Either a string or a list of strings. user_agent (Optional[ListOrString]): - The user agent to use for the fingerprint. Either a string or a list of strings. + User agent to use for the fingerprint. Either a string or a list of strings. fonts (Optional[List[str]]): - The fonts to load into Camoufox, in addition to the default fonts. + Fonts to load into Camoufox, in addition to the default fonts. args (Optional[List[str]]): - The arguments to pass to the browser. + Arguments to pass to the browser. + block_images (Optional[bool]): + Whether to block all images. + block_webrtc (Optional[bool]): + Whether to block WebRTC entirely. + firefox_user_prefs (Optional[Dict[str, Any]]): + Firefox user preferences to set. + env (Optional[Dict[str, Union[str, float, bool]]]): + Environment variables to set. executable_path (Optional[str]): - The path to the Camoufox browser executable. + Custom Camoufox browser executable path. **launch_options (Dict[str, Any]): Additional Firefox launch options. ``` @@ -119,7 +125,7 @@ Parameters: Camoufox [config data](https://github.com/daijro/camoufox?tab=readme-ov-file#fingerprint-injection) can be passed as a dictionary to the `config` parameter: ```python -from camoufox import Camoufox +from camoufox.sync_api import Camoufox with Camoufox( config={ diff --git a/pythonlib/camoufox/addons.py b/pythonlib/camoufox/addons.py index 35502a2..a725b4c 100644 --- a/pythonlib/camoufox/addons.py +++ b/pythonlib/camoufox/addons.py @@ -52,7 +52,7 @@ def confirm_paths(paths: List[str]) -> None: Confirms that the addon paths are valid """ for path in paths: - if not os.path.exists(path): + if not os.path.isdir(path): raise InvalidAddonPath(path) diff --git a/pythonlib/camoufox/async_api.py b/pythonlib/camoufox/async_api.py index ab98eb1..cb41b9d 100644 --- a/pythonlib/camoufox/async_api.py +++ b/pythonlib/camoufox/async_api.py @@ -19,8 +19,8 @@ class AsyncCamoufox(PlaywrightContextManager): self.browser: Optional[Browser] = None async def __aenter__(self) -> Browser: - await super().__aenter__() - self.browser = await AsyncNewBrowser(self._playwright, **self.launch_options) + _playwright = await super().__aenter__() + self.browser = await AsyncNewBrowser(_playwright, **self.launch_options) return self.browser async def __aexit__(self, *args: Any): @@ -42,6 +42,9 @@ async def AsyncNewBrowser( fonts: Optional[List[str]] = None, args: Optional[List[str]] = None, executable_path: Optional[str] = None, + block_images: Optional[bool] = None, + block_webrtc: Optional[bool] = None, + firefox_user_prefs: Optional[Dict[str, Any]] = None, env: Optional[Dict[str, Union[str, float, bool]]] = None, **launch_options: Dict[str, Any] ) -> Browser: @@ -50,29 +53,35 @@ async def AsyncNewBrowser( Parameters: playwright (Playwright): - The playwright instance to use. + Playwright instance to use. config (Optional[Dict[str, Any]]): - The configuration to use. + Configuration to use. addons (Optional[List[str]]): - The addons to use. + Addons to use. fingerprint (Optional[Fingerprint]): - The fingerprint to use. + BrowserForge fingerprint to use. exclude_addons (Optional[List[DefaultAddons]]): - The default addons to exclude, passed as a list of camoufox.DefaultAddons enums. + Default addons to exclude. Passed as a list of camoufox.DefaultAddons enums. screen (Optional[browserforge.fingerprints.Screen]): - The screen constraints to use. + BrowserForge screen constraints to use. os (Optional[ListOrString]): - The operating system to use for the fingerprint. Either a string or a list of strings. + Operating system to use for the fingerprint. Either a string or a list of strings. user_agent (Optional[ListOrString]): - The user agent to use for the fingerprint. Either a string or a list of strings. + User agent to use for the fingerprint. Either a string or a list of strings. fonts (Optional[List[str]]): - The fonts to load into Camoufox, in addition to the default fonts. + Fonts to load into Camoufox, in addition to the default fonts. args (Optional[List[str]]): - The arguments to pass to the browser. - executable_path (Optional[str]): - The path to the Camoufox browser executable. + Arguments to pass to the browser. + block_images (Optional[bool]): + Whether to block all images. + block_webrtc (Optional[bool]): + Whether to block WebRTC entirely. + firefox_user_prefs (Optional[Dict[str, Any]]): + Firefox user preferences to set. env (Optional[Dict[str, Union[str, float, bool]]]): - The environment variables to set. + Environment variables to set. + executable_path (Optional[str]): + Custom Camoufox browser executable path. **launch_options (Dict[str, Any]): Additional Firefox launch options. """ @@ -88,5 +97,8 @@ async def AsyncNewBrowser( args=args, executable_path=executable_path, env=env, + block_images=block_images, + block_webrtc=block_webrtc, + firefox_user_prefs=firefox_user_prefs, ) return await playwright.firefox.launch(**opt, **launch_options) diff --git a/pythonlib/camoufox/sync_api.py b/pythonlib/camoufox/sync_api.py index 1aa4980..28fd343 100644 --- a/pythonlib/camoufox/sync_api.py +++ b/pythonlib/camoufox/sync_api.py @@ -42,6 +42,9 @@ def NewBrowser( fonts: Optional[List[str]] = None, args: Optional[List[str]] = None, executable_path: Optional[str] = None, + block_images: Optional[bool] = None, + block_webrtc: Optional[bool] = None, + firefox_user_prefs: Optional[Dict[str, Any]] = None, env: Optional[Dict[str, Union[str, float, bool]]] = None, **launch_options: Dict[str, Any] ) -> Browser: @@ -50,29 +53,35 @@ def NewBrowser( Parameters: playwright (Playwright): - The playwright instance to use. + Playwright instance to use. config (Optional[Dict[str, Any]]): - The configuration to use. + Configuration to use. addons (Optional[List[str]]): - The addons to use. + Addons to use. fingerprint (Optional[Fingerprint]): - The fingerprint to use. + BrowserForge fingerprint to use. exclude_addons (Optional[List[DefaultAddons]]): - The default addons to exclude, passed as a list of camoufox.DefaultAddons enums. + Default addons to exclude. Passed as a list of camoufox.DefaultAddons enums. screen (Optional[browserforge.fingerprints.Screen]): - The screen constraints to use. + BrowserForge screen constraints to use. os (Optional[ListOrString]): - The operating system to use for the fingerprint. Either a string or a list of strings. + Operating system to use for the fingerprint. Either a string or a list of strings. user_agent (Optional[ListOrString]): - The user agent to use for the fingerprint. Either a string or a list of strings. + User agent to use for the fingerprint. Either a string or a list of strings. fonts (Optional[List[str]]): - The fonts to load into Camoufox, in addition to the default fonts. + Fonts to load into Camoufox, in addition to the default fonts. args (Optional[List[str]]): - The arguments to pass to the browser. - executable_path (Optional[str]): - The path to the Camoufox browser executable. + Arguments to pass to the browser. + block_images (Optional[bool]): + Whether to block all images. + block_webrtc (Optional[bool]): + Whether to block WebRTC entirely. + firefox_user_prefs (Optional[Dict[str, Any]]): + Firefox user preferences to set. env (Optional[Dict[str, Union[str, float, bool]]]): - The environment variables to set. + Environment variables to set. + executable_path (Optional[str]): + Custom Camoufox browser executable path. **launch_options (Dict[str, Any]): Additional Firefox launch options. """ @@ -88,5 +97,8 @@ def NewBrowser( args=args, executable_path=executable_path, env=env, + block_images=block_images, + block_webrtc=block_webrtc, + firefox_user_prefs=firefox_user_prefs, ) return playwright.firefox.launch(**opt, **launch_options) diff --git a/pythonlib/camoufox/utils.py b/pythonlib/camoufox/utils.py index c6c7cf8..1143c22 100644 --- a/pythonlib/camoufox/utils.py +++ b/pythonlib/camoufox/utils.py @@ -164,6 +164,9 @@ def get_launch_options( args: Optional[List[str]] = None, executable_path: Optional[str] = None, env: Optional[Dict[str, Union[str, float, bool]]] = None, + block_images: Optional[bool] = None, + block_webrtc: Optional[bool] = None, + firefox_user_prefs: Optional[Dict[str, Any]] = None, ) -> Dict[str, Any]: """ Builds the launch options for the Camoufox browser. @@ -212,6 +215,15 @@ def get_launch_options( target_os = get_target_os(config) update_fonts(config, target_os) + # Set Firefox user preferences + if firefox_user_prefs is None: + firefox_user_prefs = {} + + if block_images: + firefox_user_prefs['permissions.default.image'] = 2 + if block_webrtc: + firefox_user_prefs['media.peerconnection.enabled'] = False + # Launch threaded_try_load_addons(get_debug_port(args), addons) env_vars = { @@ -222,4 +234,5 @@ def get_launch_options( "executable_path": executable_path or get_path(LAUNCH_FILE[OS_NAME]), "args": args, "env": env_vars, + "firefox_user_prefs": firefox_user_prefs, } diff --git a/pythonlib/pyproject.toml b/pythonlib/pyproject.toml index 6aa5f92..02849e6 100644 --- a/pythonlib/pyproject.toml +++ b/pythonlib/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "camoufox" -version = "0.1.2" +version = "0.1.3" description = "Wrapper around Playwright to help launch Camoufox" authors = ["daijro "] license = "MIT"