feat(PlaywrightFetcher): Add async support for PlaywrightFetcher

This commit is contained in:
Karim shoair
2024-12-16 00:15:47 +02:00
parent 889c111855
commit af4f2c0f74
7 changed files with 170 additions and 35 deletions
+3 -4
View File
@@ -1,7 +1,6 @@
from .custom import (BaseFetcher, Response, StatusText, check_if_engine_usable,
check_type_validity, do_nothing, do_nothing_async,
get_variable_name)
check_type_validity, get_variable_name)
from .fingerprints import (generate_convincing_referer, generate_headers,
get_os_name)
from .navigation import (construct_cdp_url, construct_proxy_dict,
intercept_route, js_bypass_path)
from .navigation import (async_intercept_route, construct_cdp_url,
construct_proxy_dict, intercept_route, js_bypass_path)
-11
View File
@@ -296,14 +296,3 @@ def check_type_validity(variable: Any, valid_types: Union[List[Type], None], def
return default_value
return variable
# Pew Pew
def do_nothing(page):
# Just works as a filler for `page_action` argument in browser engines
return page
async def do_nothing_async(page):
# Just works as a filler for `page_action` argument in browser engines
return page
+16 -3
View File
@@ -4,6 +4,7 @@ Functions related to files and URLs
import os
from urllib.parse import urlencode, urlparse
from playwright.async_api import Route as async_Route
from playwright.sync_api import Route
from scrapling.core._types import Dict, Optional, Union
@@ -11,7 +12,7 @@ from scrapling.core.utils import log, lru_cache
from scrapling.engines.constants import DEFAULT_DISABLED_RESOURCES
def intercept_route(route: Route) -> Union[Route, None]:
def intercept_route(route: Route):
"""This is just a route handler but it drops requests that its type falls in `DEFAULT_DISABLED_RESOURCES`
:param route: PlayWright `Route` object of the current page
@@ -19,8 +20,20 @@ def intercept_route(route: Route) -> Union[Route, None]:
"""
if route.request.resource_type in DEFAULT_DISABLED_RESOURCES:
log.debug(f'Blocking background resource "{route.request.url}" of type "{route.request.resource_type}"')
return route.abort()
return route.continue_()
route.abort()
route.continue_()
async def async_intercept_route(route: async_Route):
"""This is just a route handler but it drops requests that its type falls in `DEFAULT_DISABLED_RESOURCES`
:param route: PlayWright `Route` object of the current page
:return: PlayWright `Route` object
"""
if route.request.resource_type in DEFAULT_DISABLED_RESOURCES:
log.debug(f'Blocking background resource "{route.request.url}" of type "{route.request.resource_type}"')
await route.abort()
await route.continue_()
def construct_proxy_dict(proxy_string: Union[str, Dict[str, str]]) -> Union[Dict, None]: