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
This commit is contained in:
daijro 2024-09-19 19:05:53 -05:00
parent e487754a42
commit f18b9a00be
6 changed files with 86 additions and 43 deletions

View file

@ -84,28 +84,34 @@ Launches a new browser instance for Camoufox.
Accepts all Playwright Firefox launch options, along with the following: Accepts all Playwright Firefox launch options, along with the following:
Parameters: Parameters:
playwright (Playwright):
The playwright instance to use.
config (Optional[Dict[str, Any]]): config (Optional[Dict[str, Any]]):
The configuration to use. Configuration to use.
addons (Optional[List[str]]): addons (Optional[List[str]]):
The addons to use. Addons to use.
fingerprint (Optional[Fingerprint]): fingerprint (Optional[Fingerprint]):
The fingerprint to use. BrowserForge fingerprint to use.
exclude_addons (Optional[List[DefaultAddons]]): 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]): screen (Optional[browserforge.fingerprints.Screen]):
The screen constraints to use. BrowserForge screen constraints to use.
os (Optional[ListOrString]): 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]): 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]]): 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]]): 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]): executable_path (Optional[str]):
The path to the Camoufox browser executable. Custom Camoufox browser executable path.
**launch_options (Dict[str, Any]): **launch_options (Dict[str, Any]):
Additional Firefox launch options. 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: 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 ```python
from camoufox import Camoufox from camoufox.sync_api import Camoufox
with Camoufox( with Camoufox(
config={ config={

View file

@ -52,7 +52,7 @@ def confirm_paths(paths: List[str]) -> None:
Confirms that the addon paths are valid Confirms that the addon paths are valid
""" """
for path in paths: for path in paths:
if not os.path.exists(path): if not os.path.isdir(path):
raise InvalidAddonPath(path) raise InvalidAddonPath(path)

View file

@ -19,8 +19,8 @@ class AsyncCamoufox(PlaywrightContextManager):
self.browser: Optional[Browser] = None self.browser: Optional[Browser] = None
async def __aenter__(self) -> Browser: async def __aenter__(self) -> Browser:
await super().__aenter__() _playwright = await super().__aenter__()
self.browser = await AsyncNewBrowser(self._playwright, **self.launch_options) self.browser = await AsyncNewBrowser(_playwright, **self.launch_options)
return self.browser return self.browser
async def __aexit__(self, *args: Any): async def __aexit__(self, *args: Any):
@ -42,6 +42,9 @@ async def AsyncNewBrowser(
fonts: Optional[List[str]] = None, fonts: Optional[List[str]] = None,
args: Optional[List[str]] = None, args: Optional[List[str]] = None,
executable_path: Optional[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, env: Optional[Dict[str, Union[str, float, bool]]] = None,
**launch_options: Dict[str, Any] **launch_options: Dict[str, Any]
) -> Browser: ) -> Browser:
@ -50,29 +53,35 @@ async def AsyncNewBrowser(
Parameters: Parameters:
playwright (Playwright): playwright (Playwright):
The playwright instance to use. Playwright instance to use.
config (Optional[Dict[str, Any]]): config (Optional[Dict[str, Any]]):
The configuration to use. Configuration to use.
addons (Optional[List[str]]): addons (Optional[List[str]]):
The addons to use. Addons to use.
fingerprint (Optional[Fingerprint]): fingerprint (Optional[Fingerprint]):
The fingerprint to use. BrowserForge fingerprint to use.
exclude_addons (Optional[List[DefaultAddons]]): 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]): screen (Optional[browserforge.fingerprints.Screen]):
The screen constraints to use. BrowserForge screen constraints to use.
os (Optional[ListOrString]): 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]): 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]]): 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]]): args (Optional[List[str]]):
The arguments to pass to the browser. Arguments to pass to the browser.
executable_path (Optional[str]): block_images (Optional[bool]):
The path to the Camoufox browser executable. 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]]]): 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]): **launch_options (Dict[str, Any]):
Additional Firefox launch options. Additional Firefox launch options.
""" """
@ -88,5 +97,8 @@ async def AsyncNewBrowser(
args=args, args=args,
executable_path=executable_path, executable_path=executable_path,
env=env, env=env,
block_images=block_images,
block_webrtc=block_webrtc,
firefox_user_prefs=firefox_user_prefs,
) )
return await playwright.firefox.launch(**opt, **launch_options) return await playwright.firefox.launch(**opt, **launch_options)

View file

@ -42,6 +42,9 @@ def NewBrowser(
fonts: Optional[List[str]] = None, fonts: Optional[List[str]] = None,
args: Optional[List[str]] = None, args: Optional[List[str]] = None,
executable_path: Optional[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, env: Optional[Dict[str, Union[str, float, bool]]] = None,
**launch_options: Dict[str, Any] **launch_options: Dict[str, Any]
) -> Browser: ) -> Browser:
@ -50,29 +53,35 @@ def NewBrowser(
Parameters: Parameters:
playwright (Playwright): playwright (Playwright):
The playwright instance to use. Playwright instance to use.
config (Optional[Dict[str, Any]]): config (Optional[Dict[str, Any]]):
The configuration to use. Configuration to use.
addons (Optional[List[str]]): addons (Optional[List[str]]):
The addons to use. Addons to use.
fingerprint (Optional[Fingerprint]): fingerprint (Optional[Fingerprint]):
The fingerprint to use. BrowserForge fingerprint to use.
exclude_addons (Optional[List[DefaultAddons]]): 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]): screen (Optional[browserforge.fingerprints.Screen]):
The screen constraints to use. BrowserForge screen constraints to use.
os (Optional[ListOrString]): 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]): 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]]): 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]]): args (Optional[List[str]]):
The arguments to pass to the browser. Arguments to pass to the browser.
executable_path (Optional[str]): block_images (Optional[bool]):
The path to the Camoufox browser executable. 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]]]): 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]): **launch_options (Dict[str, Any]):
Additional Firefox launch options. Additional Firefox launch options.
""" """
@ -88,5 +97,8 @@ def NewBrowser(
args=args, args=args,
executable_path=executable_path, executable_path=executable_path,
env=env, env=env,
block_images=block_images,
block_webrtc=block_webrtc,
firefox_user_prefs=firefox_user_prefs,
) )
return playwright.firefox.launch(**opt, **launch_options) return playwright.firefox.launch(**opt, **launch_options)

View file

@ -164,6 +164,9 @@ def get_launch_options(
args: Optional[List[str]] = None, args: Optional[List[str]] = None,
executable_path: Optional[str] = None, executable_path: Optional[str] = None,
env: Optional[Dict[str, Union[str, float, bool]]] = 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]: ) -> Dict[str, Any]:
""" """
Builds the launch options for the Camoufox browser. Builds the launch options for the Camoufox browser.
@ -212,6 +215,15 @@ def get_launch_options(
target_os = get_target_os(config) target_os = get_target_os(config)
update_fonts(config, target_os) 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 # Launch
threaded_try_load_addons(get_debug_port(args), addons) threaded_try_load_addons(get_debug_port(args), addons)
env_vars = { env_vars = {
@ -222,4 +234,5 @@ def get_launch_options(
"executable_path": executable_path or get_path(LAUNCH_FILE[OS_NAME]), "executable_path": executable_path or get_path(LAUNCH_FILE[OS_NAME]),
"args": args, "args": args,
"env": env_vars, "env": env_vars,
"firefox_user_prefs": firefox_user_prefs,
} }

View file

@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry] [tool.poetry]
name = "camoufox" name = "camoufox"
version = "0.1.2" version = "0.1.3"
description = "Wrapper around Playwright to help launch Camoufox" description = "Wrapper around Playwright to help launch Camoufox"
authors = ["daijro <daijro.dev@gmail.com>"] authors = ["daijro <daijro.dev@gmail.com>"]
license = "MIT" license = "MIT"