Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api Update: New endpoints using x-tt-params #699

Merged
merged 8 commits into from
Sep 21, 2021
Merged
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
30 changes: 24 additions & 6 deletions TikTokApi/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import time
import datetime
import random
import json
from urllib.parse import splitquery, parse_qs, parse_qsl


# Import Detection From Stealth
from .stealth import stealth
from .get_acrawler import get_acrawler
from .get_acrawler import get_acrawler, get_tt_params_script
from playwright.sync_api import sync_playwright

playwright = None
Expand Down Expand Up @@ -116,6 +118,8 @@ def create_context(self, set_useragent=False):
iphone["is_mobile"] = random.randint(1, 2) == 1
iphone["has_touch"] = random.randint(1, 2) == 1

iphone['bypass_csp'] = True

context = self.browser.new_context(**iphone)
if set_useragent:
self.userAgent = iphone["user_agent"]
Expand Down Expand Up @@ -165,6 +169,9 @@ def sign_url(self, **kwargs):
raise Exception("sign_url required a url parameter")
context = self.create_context()
page = context.new_page()

page.goto(kwargs.get('default_url', 'https://www.tiktok.com/@redbull'), wait_until='load')

verifyFp = "".join(
random.choice(
string.ascii_lowercase + string.ascii_uppercase + string.digits
Expand All @@ -186,25 +193,36 @@ def sign_url(self, **kwargs):
else:
device_id = self.device_id

page.set_content("<script> " + get_acrawler() + " </script>")
url = '{}&verifyFp={}&device_id={}'.format(url, verifyFp, device_id)

page.add_script_tag(content=get_acrawler())
evaluatedPage = page.evaluate(
'''() => {
var url = "'''
+ url
+ "&verifyFp="
+ verifyFp
+ """&device_id="""
+ device_id
+ """"
var token = window.byted_acrawler.sign({url: url});

return token;
}"""
)

url = '{}&_signature={}'.format(url, evaluatedPage)
page.add_script_tag(content=get_tt_params_script())

tt_params = page.evaluate(
'''() => {
return window.genXTTParams(''' + json.dumps(dict(parse_qsl(splitquery(url)[1]))) + ''');

}'''
)

context.close()
return (
verifyFp,
device_id,
evaluatedPage,
tt_params
)

def clean_up(self):
Expand Down
Loading