Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions TikTokApi/api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,52 @@ async def users(search_term, count=10, cursor=0, **kwargs) -> Iterator[User]:
):
yield user

@staticmethod
async def general_search(search_term,cursor=0,**kwargs):

params = {
"keyword": search_term,
"cursor": cursor,
"from_page": "search",
"web_search_code": """{"tiktok":{"client_params_x":{"search_engine":{"ies_mt_user_live_video_card_use_libra":1,"mt_search_general_user_live_card":1}},"search_server":{}}}""",
}
resp = await Search.parent.make_request(
url=f"https://www.tiktok.com/api/search/general/full/",
params=params,
headers=kwargs.get("headers"),
session_index=kwargs.get("session_index"),
)
if resp is None:
raise InvalidResponseException(
resp, "TikTok returned an invalid response."
)


return resp

@staticmethod
async def preview_search(search_term,cursor=0,**kwargs):

params = {
"keyword": search_term,
"cursor": cursor,
"from_page": "search",
"web_search_code": """{"tiktok":{"client_params_x":{"search_engine":{"ies_mt_user_live_video_card_use_libra":1,"mt_search_general_user_live_card":1}},"search_server":{}}}""",
}
resp = await Search.parent.make_request(
url=f"https://www.tiktok.com/api/search/general/preview/",
params=params,
headers=kwargs.get("headers"),
session_index=kwargs.get("session_index"),
)
if resp is None:
raise InvalidResponseException(
resp, "TikTok returned an invalid response."
)


return resp

@staticmethod
async def search_type(
search_term, obj_type, count=10, cursor=0, **kwargs
Expand Down
15 changes: 6 additions & 9 deletions TikTokApi/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,14 @@ def __create_logger(self, name: str, level: int = logging.DEBUG):

async def __set_session_params(self, session: TikTokPlaywrightSession):
"""Set the session params for a TikTokPlaywrightSession"""
user_agent = await session.page.evaluate("() => navigator.userAgent")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to pull the params from the page itself

language = await session.page.evaluate(
"() => navigator.language || navigator.userLanguage"
)
platform = await session.page.evaluate("() => navigator.platform")
user_agent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36"
language = "ja-JP"
platform = "web_pc"
device_id = str(random.randint(10**18, 10**19 - 1)) # Random device id
history_len = str(random.randint(1, 10)) # Random history length
screen_height = str(random.randint(600, 1080)) # Random screen height
screen_width = str(random.randint(800, 1920)) # Random screen width
timezone = await session.page.evaluate(
"() => Intl.DateTimeFormat().resolvedOptions().timeZone"
)
timezone = "Asia/Tokyo"

session_params = {
"aid": "1988",
Expand Down Expand Up @@ -207,7 +203,7 @@ async def create_sessions(
ms_tokens: list[str] = None,
proxies: list = None,
sleep_after=1,
starting_url="https://www.tiktok.com",
starting_url="https://www.tiktok.com/login",
context_options: dict = {},
override_browser_args: list[dict] = None,
cookies: list[dict] = None,
Expand All @@ -216,6 +212,7 @@ async def create_sessions(
executable_path: str = None
):
"""
Its MIneeeeeeeeeeeeee
Create sessions for use within the TikTokApi class.

These sessions are what will carry out requesting your data from TikTok.
Expand Down