From 31587d77cb9bb5136e0eeece0b9b0f7ac5a327ad Mon Sep 17 00:00:00 2001 From: Wixome <74211795+Wixome@users.noreply.github.com> Date: Mon, 21 Aug 2023 02:45:04 +0300 Subject: [PATCH] :tada: V6.1.1 - Add an option to suppress resource types from loading in playwright (#1048) * Added an option to disable image loading * switch to list of types of media to suppress, black formatting, bump to v6.1.1 --------- Co-authored-by: Vladislav Co-authored-by: davidteather <34144122+davidteather@users.noreply.github.com> --- .sphinx/conf.py | 2 +- CITATION.cff | 4 ++-- TikTokApi/api/user.py | 5 +++-- TikTokApi/exceptions.py | 2 +- TikTokApi/stealth/js/chrome_app.py | 2 +- TikTokApi/stealth/js/chrome_csi.py | 2 +- TikTokApi/stealth/js/chrome_hairline.py | 2 +- TikTokApi/stealth/js/chrome_load_times.py | 2 +- TikTokApi/stealth/js/chrome_runtime.py | 2 +- TikTokApi/stealth/js/generate_magic_arrays.py | 2 +- TikTokApi/stealth/js/iframe_contentWindow.py | 2 +- TikTokApi/stealth/js/media_codecs.py | 2 +- .../stealth/js/navigator_hardwareConcurrency.py | 2 +- TikTokApi/stealth/js/navigator_languages.py | 2 +- TikTokApi/stealth/js/navigator_permissions.py | 2 +- TikTokApi/stealth/js/navigator_platform.py | 2 +- TikTokApi/stealth/js/navigator_plugins.py | 2 +- TikTokApi/stealth/js/navigator_userAgent.py | 2 +- TikTokApi/stealth/js/navigator_vendor.py | 2 +- TikTokApi/stealth/js/utils.py | 2 +- TikTokApi/stealth/js/webgl_vendor.py | 2 +- TikTokApi/stealth/js/window_outerdimensions.py | 2 +- TikTokApi/tiktok.py | 13 +++++++++++++ setup.py | 6 +++--- tests/test_user.py | 5 ++++- 25 files changed, 45 insertions(+), 28 deletions(-) diff --git a/.sphinx/conf.py b/.sphinx/conf.py index a4b7a93d..d0ee8328 100644 --- a/.sphinx/conf.py +++ b/.sphinx/conf.py @@ -16,7 +16,7 @@ project = "TikTokAPI" copyright = "2023, David Teather" author = "David Teather" -release = "v6.1.0" +release = "v6.1.1" # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/main/usage/configuration.html#general-configuration diff --git a/CITATION.cff b/CITATION.cff index 54eafde2..f48689a3 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -5,5 +5,5 @@ authors: orcid: "https://orcid.org/0000-0002-9467-4676" title: "TikTokAPI" url: "https://github.com/davidteather/tiktok-api" -version: 6.1.0 -date-released: 2023-8-18 +version: 6.1.1 +date-released: 2023-8-20 diff --git a/TikTokApi/api/user.py b/TikTokApi/api/user.py index 7486ef28..7c36388a 100644 --- a/TikTokApi/api/user.py +++ b/TikTokApi/api/user.py @@ -140,7 +140,9 @@ async def videos(self, count=30, cursor=0, **kwargs) -> Iterator[Video]: cursor = resp.get("cursor") - async def liked(self, count: int = 30, cursor: int = 0, **kwargs) -> Iterator[Video]: + async def liked( + self, count: int = 30, cursor: int = 0, **kwargs + ) -> Iterator[Video]: """ Returns a user's liked posts if public. @@ -192,7 +194,6 @@ async def liked(self, count: int = 30, cursor: int = 0, **kwargs) -> Iterator[Vi return cursor = resp.get("cursor") - def __extract_from_data(self): data = self.as_dict diff --git a/TikTokApi/exceptions.py b/TikTokApi/exceptions.py index 034fa03c..6ebe627c 100644 --- a/TikTokApi/exceptions.py +++ b/TikTokApi/exceptions.py @@ -28,7 +28,7 @@ class SoundRemovedException(TikTokException): class InvalidJSONException(TikTokException): - """TikTok returned invalid JSON.""" + """TikTok returned invalid JSON.""" class InvalidResponseException(TikTokException): diff --git a/TikTokApi/stealth/js/chrome_app.py b/TikTokApi/stealth/js/chrome_app.py index 6b09b408..79c10af8 100644 --- a/TikTokApi/stealth/js/chrome_app.py +++ b/TikTokApi/stealth/js/chrome_app.py @@ -70,4 +70,4 @@ } utils.patchToStringNested(window.chrome.app) } -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/chrome_csi.py b/TikTokApi/stealth/js/chrome_csi.py index 31d67ec5..53310023 100644 --- a/TikTokApi/stealth/js/chrome_csi.py +++ b/TikTokApi/stealth/js/chrome_csi.py @@ -26,4 +26,4 @@ } utils.patchToString(window.chrome.csi) } -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/chrome_hairline.py b/TikTokApi/stealth/js/chrome_hairline.py index ca3365e2..3ce3df35 100644 --- a/TikTokApi/stealth/js/chrome_hairline.py +++ b/TikTokApi/stealth/js/chrome_hairline.py @@ -13,4 +13,4 @@ return elementDescriptor.get.apply(this); }, }); -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/chrome_load_times.py b/TikTokApi/stealth/js/chrome_load_times.py index 5ca902fe..a673cda4 100644 --- a/TikTokApi/stealth/js/chrome_load_times.py +++ b/TikTokApi/stealth/js/chrome_load_times.py @@ -121,4 +121,4 @@ } utils.patchToString(window.chrome.loadTimes) } -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/chrome_runtime.py b/TikTokApi/stealth/js/chrome_runtime.py index 64de810f..d971a90a 100644 --- a/TikTokApi/stealth/js/chrome_runtime.py +++ b/TikTokApi/stealth/js/chrome_runtime.py @@ -262,4 +262,4 @@ } } -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/generate_magic_arrays.py b/TikTokApi/stealth/js/generate_magic_arrays.py index ac813a8c..1642ea98 100644 --- a/TikTokApi/stealth/js/generate_magic_arrays.py +++ b/TikTokApi/stealth/js/generate_magic_arrays.py @@ -141,4 +141,4 @@ } }) } -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/iframe_contentWindow.py b/TikTokApi/stealth/js/iframe_contentWindow.py index cce1b3fd..0749092b 100644 --- a/TikTokApi/stealth/js/iframe_contentWindow.py +++ b/TikTokApi/stealth/js/iframe_contentWindow.py @@ -96,4 +96,4 @@ } catch (err) { // console.warn(err) } -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/media_codecs.py b/TikTokApi/stealth/js/media_codecs.py index f4d76a9e..46f25e6a 100644 --- a/TikTokApi/stealth/js/media_codecs.py +++ b/TikTokApi/stealth/js/media_codecs.py @@ -62,4 +62,4 @@ 'canPlayType', canPlayType ) -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/navigator_hardwareConcurrency.py b/TikTokApi/stealth/js/navigator_hardwareConcurrency.py index b26ee93f..1aa2d9dd 100644 --- a/TikTokApi/stealth/js/navigator_hardwareConcurrency.py +++ b/TikTokApi/stealth/js/navigator_hardwareConcurrency.py @@ -7,4 +7,4 @@ }) patchNavigator('hardwareConcurrency', opts.navigator_hardware_concurrency || 4); -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/navigator_languages.py b/TikTokApi/stealth/js/navigator_languages.py index d5871700..ee91a988 100644 --- a/TikTokApi/stealth/js/navigator_languages.py +++ b/TikTokApi/stealth/js/navigator_languages.py @@ -3,4 +3,4 @@ get: () => opts.languages || ['en-US', 'en'] }) -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/navigator_permissions.py b/TikTokApi/stealth/js/navigator_permissions.py index d3ece6ed..71ae8663 100644 --- a/TikTokApi/stealth/js/navigator_permissions.py +++ b/TikTokApi/stealth/js/navigator_permissions.py @@ -19,4 +19,4 @@ handler ) -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/navigator_platform.py b/TikTokApi/stealth/js/navigator_platform.py index 53522ad1..476d35a4 100644 --- a/TikTokApi/stealth/js/navigator_platform.py +++ b/TikTokApi/stealth/js/navigator_platform.py @@ -4,4 +4,4 @@ get: () => opts.navigator_plaftorm, }) } -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/navigator_plugins.py b/TikTokApi/stealth/js/navigator_plugins.py index fec302bc..688a7e7a 100644 --- a/TikTokApi/stealth/js/navigator_plugins.py +++ b/TikTokApi/stealth/js/navigator_plugins.py @@ -91,4 +91,4 @@ patchNavigator('mimeTypes', mimeTypes) patchNavigator('plugins', plugins) } -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/navigator_userAgent.py b/TikTokApi/stealth/js/navigator_userAgent.py index e156bd37..a89d2196 100644 --- a/TikTokApi/stealth/js/navigator_userAgent.py +++ b/TikTokApi/stealth/js/navigator_userAgent.py @@ -5,4 +5,4 @@ get: () => opts.navigator_user_agent || current_ua.replace('HeadlessChrome/', 'Chrome/') }) -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/navigator_vendor.py b/TikTokApi/stealth/js/navigator_vendor.py index 36446db9..aba039f2 100644 --- a/TikTokApi/stealth/js/navigator_vendor.py +++ b/TikTokApi/stealth/js/navigator_vendor.py @@ -3,4 +3,4 @@ get: () => opts.navigator_vendor || 'Google Inc.', }) -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/utils.py b/TikTokApi/stealth/js/utils.py index b0a6c4d2..88df9d98 100644 --- a/TikTokApi/stealth/js/utils.py +++ b/TikTokApi/stealth/js/utils.py @@ -455,4 +455,4 @@ // Stuff starting below this line is NodeJS specific. // -- // module.exports = utils -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/webgl_vendor.py b/TikTokApi/stealth/js/webgl_vendor.py index f769d55e..4003ad89 100644 --- a/TikTokApi/stealth/js/webgl_vendor.py +++ b/TikTokApi/stealth/js/webgl_vendor.py @@ -24,4 +24,4 @@ // For whatever weird reason loops don't play nice with Object.defineProperty, here's the next best thing: addProxy(WebGLRenderingContext.prototype, 'getParameter') addProxy(WebGL2RenderingContext.prototype, 'getParameter') -""" \ No newline at end of file +""" diff --git a/TikTokApi/stealth/js/window_outerdimensions.py b/TikTokApi/stealth/js/window_outerdimensions.py index 3d2e9dd1..c1577065 100644 --- a/TikTokApi/stealth/js/window_outerdimensions.py +++ b/TikTokApi/stealth/js/window_outerdimensions.py @@ -12,4 +12,4 @@ } catch (err) { } -""" \ No newline at end of file +""" diff --git a/TikTokApi/tiktok.py b/TikTokApi/tiktok.py index dc8e34bc..c52838b4 100644 --- a/TikTokApi/tiktok.py +++ b/TikTokApi/tiktok.py @@ -142,6 +142,7 @@ async def __create_session( context_options: dict = {}, sleep_after: int = 1, cookies: dict = None, + suppress_resource_load_types: list[str] = None, ): """Create a TikTokPlaywrightSession""" if ms_token is not None: @@ -168,6 +169,15 @@ def handle_request(request): request_headers = request.headers page.once("request", handle_request) + + if suppress_resource_load_types is not None: + await page.route( + "**/*", + lambda route, request: route.abort() + if request.resource_type in suppress_resource_load_types + else route.continue_(), + ) + await page.goto(url) session = TikTokPlaywrightSession( @@ -201,6 +211,7 @@ async def create_sessions( context_options: dict = {}, override_browser_args: list[dict] = None, cookies: list[dict] = None, + suppress_resource_load_types: list[str] = None, ): """ Create sessions for use within the TikTokApi class. @@ -218,6 +229,7 @@ async def create_sessions( context_options (dict): Options to pass to the playwright context. override_browser_args (list[dict]): A list of dictionaries containing arguments to pass to the browser. cookies (list[dict]): A list of cookies to use for the sessions, you can get these from your cookies after visiting TikTok. + 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. Example Usage: .. code-block:: python @@ -243,6 +255,7 @@ async def create_sessions( context_options=context_options, sleep_after=sleep_after, cookies=random_choice(cookies), + suppress_resource_load_types=suppress_resource_load_types, ) for _ in range(num_sessions) ) diff --git a/setup.py b/setup.py index 097ea2bb..6e906f53 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setuptools.setup( name="TikTokApi", packages=setuptools.find_packages(), - version="6.1.0", + version="6.1.1", license="MIT", description="The Unofficial TikTok API Wrapper in Python 3.", author="David Teather", @@ -20,7 +20,7 @@ keywords=["tiktok", "python3", "api", "unofficial", "tiktok-api", "tiktok api"], install_requires=["requests", "playwright"], classifiers=[ - "Development Status :: 6.1.0", + "Development Status :: 6.1.1", "Intended Audience :: Developers", "Topic :: Software Development :: Build Tools", "License :: OSI Approved :: MIT License", @@ -28,5 +28,5 @@ "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", ], - python_requires='>=3.9', + python_requires=">=3.9", ) diff --git a/tests/test_user.py b/tests/test_user.py index 5c36ba02..33f7d62b 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -41,7 +41,10 @@ async def test_user_likes(): api = TikTokApi() async with api: await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3) - user = api.user(username="publicliketest", sec_uid="MS4wLjABAAAAHjhwCIwmvzVZfRrDAZ2aZy74LciLnoyaPfM2rrX9N7bwbWMFuwTFG4YrByYvsH5c") + user = api.user( + username="publicliketest", + sec_uid="MS4wLjABAAAAHjhwCIwmvzVZfRrDAZ2aZy74LciLnoyaPfM2rrX9N7bwbWMFuwTFG4YrByYvsH5c", + ) count = 0 async for video in user.liked(count=30):