From 77f4e4ee09d0e16122779298ad175fd5cf816806 Mon Sep 17 00:00:00 2001 From: Rahmat Slamet Date: Tue, 21 Jan 2025 11:13:07 +0700 Subject: [PATCH] fix: playwright timeout sessions with provide opt-out params when creating sessions (#1196) --- TikTokApi/tiktok.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/TikTokApi/tiktok.py b/TikTokApi/tiktok.py index bb42517b..bad6fa3b 100644 --- a/TikTokApi/tiktok.py +++ b/TikTokApi/tiktok.py @@ -143,6 +143,7 @@ async def __create_session( sleep_after: int = 1, cookies: dict = None, suppress_resource_load_types: list[str] = None, + timeout: int = 300000, ): """Create a TikTokPlaywrightSession""" if ms_token is not None: @@ -177,6 +178,9 @@ def handle_request(request): if request.resource_type in suppress_resource_load_types else route.continue_(), ) + + # Set the navigation timeout + page.set_default_navigation_timeout(timeout) await page.goto(url) await page.goto(url) # hack: tiktok blocks first request not sure why @@ -214,7 +218,8 @@ async def create_sessions( cookies: list[dict] = None, suppress_resource_load_types: list[str] = None, browser: str = "chromium", - executable_path: str = None + executable_path: str = None, + timeout: int = 300000, ): """ Create sessions for use within the TikTokApi class. @@ -235,6 +240,7 @@ async def create_sessions( suppress_resource_load_types (list[str]): Types of resources to suppress playwright from loading, excluding more types will make playwright faster.. Types: document, stylesheet, image, media, font, script, textrack, xhr, fetch, eventsource, websocket, manifest, other. browser (str): specify either firefox or chromium, default is chromium executable_path (str): Path to the browser executable + timeout (int): The timeout in milliseconds for page navigation Example Usage: .. code-block:: python @@ -272,6 +278,7 @@ async def create_sessions( sleep_after=sleep_after, cookies=random_choice(cookies), suppress_resource_load_types=suppress_resource_load_types, + timeout=timeout, ) for _ in range(num_sessions) )