Skip to content

Commit

Permalink
🎉 V6.1.1 - Add an option to suppress resource types from loading in p…
Browse files Browse the repository at this point in the history
…laywright (#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 <[email protected]>
Co-authored-by: davidteather <[email protected]>
  • Loading branch information
3 people authored Aug 20, 2023
1 parent 81bb842 commit 31587d7
Show file tree
Hide file tree
Showing 25 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
project = "TikTokAPI"
copyright = "2023, David Teather"
author = "David Teather"
release = "v6.1.0"

This comment has been minimized.

Copy link
@200ameg

200ameg Oct 16, 2024

fejrp]lrf[lgn,l;rf[,sw[,gp;lbgf

release = "v6.1.1"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/main/usage/configuration.html#general-configuration
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions TikTokApi/api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion TikTokApi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SoundRemovedException(TikTokException):


class InvalidJSONException(TikTokException):
"""TikTok returned invalid JSON."""
"""TikTok returned invalid JSON."""


class InvalidResponseException(TikTokException):
Expand Down
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/chrome_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
}
utils.patchToStringNested(window.chrome.app)
}
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/chrome_csi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
}
utils.patchToString(window.chrome.csi)
}
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/chrome_hairline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
return elementDescriptor.get.apply(this);
},
});
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/chrome_load_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@
}
utils.patchToString(window.chrome.loadTimes)
}
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/chrome_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,4 @@
}
}
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/generate_magic_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@
}
})
}
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/iframe_contentWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@
} catch (err) {
// console.warn(err)
}
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/media_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@
'canPlayType',
canPlayType
)
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/navigator_hardwareConcurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
})
patchNavigator('hardwareConcurrency', opts.navigator_hardware_concurrency || 4);
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/navigator_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
get: () => opts.languages || ['en-US', 'en']
})
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/navigator_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
handler
)
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/navigator_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
get: () => opts.navigator_plaftorm,
})
}
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/navigator_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@
patchNavigator('mimeTypes', mimeTypes)
patchNavigator('plugins', plugins)
}
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/navigator_userAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
get: () => opts.navigator_user_agent || current_ua.replace('HeadlessChrome/', 'Chrome/')
})
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/navigator_vendor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
get: () => opts.navigator_vendor || 'Google Inc.',
})
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,4 @@
// Stuff starting below this line is NodeJS specific.
// --
// module.exports = utils
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/webgl_vendor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
"""
"""
2 changes: 1 addition & 1 deletion TikTokApi/stealth/js/window_outerdimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
} catch (err) {
}
"""
"""
13 changes: 13 additions & 0 deletions TikTokApi/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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)
)
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -20,13 +20,13 @@
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",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
],
python_requires='>=3.9',
python_requires=">=3.9",
)
5 changes: 4 additions & 1 deletion tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 31587d7

Please sign in to comment.