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

V4.0.2 - Fix Methods With x-tt-params #700

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
4 changes: 4 additions & 0 deletions TikTokApi/get_acrawler.py

Large diffs are not rendered by default.

30 changes: 18 additions & 12 deletions TikTokApi/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, **kwargs):
# Some Instance Vars
self.executablePath = kwargs.get("executablePath", None)

if kwargs.get("custom_device_id") != None:
if kwargs.get("custom_did") != None:
raise Exception("Please use custom_device_id instead of custom_device_id")
self.custom_device_id = kwargs.get("custom_device_id", None)
self.userAgent = (
Expand Down Expand Up @@ -230,9 +230,10 @@ def get_data(self, **kwargs) -> dict:
else:
verifyFp = kwargs.get("custom_verifyFp")

tt_params = None
if self.signer_url is None:
kwargs["custom_verifyFp"] = verifyFp
verify_fp, device_id, signature = self.browser.sign_url(**kwargs)
verify_fp, device_id, signature, tt_params = self.browser.sign_url(**kwargs)
userAgent = self.browser.userAgent
referrer = self.browser.referrer
else:
Expand All @@ -242,6 +243,10 @@ def get_data(self, **kwargs) -> dict:
verifyFp=kwargs.get("custom_verifyFp", verifyFp),
)

if not kwargs.get("send_tt_params", False):
tt_params = None


query = {"verifyFp": verify_fp, "device_id": device_id, "_signature": signature}
url = "{}&{}".format(kwargs["url"], urlencode(query))

Expand Down Expand Up @@ -273,6 +278,7 @@ def get_data(self, **kwargs) -> dict:
"sec-gpc": "1",
"user-agent": userAgent,
"x-secsdk-csrf-token": csrf_token,
"x-tt-params": tt_params
},
cookies=self.get_cookies(**kwargs),
proxies=self.__format_proxy(proxy),
Expand Down Expand Up @@ -428,7 +434,7 @@ def by_trending(self, count=30, **kwargs) -> dict:
for t in res.get("itemList", []):
response.append(t)

if not res["hasMore"] and not first:
if not res.get("hasMore", False) and not first:
logging.info("TikTok isn't sending more TikToks beyond this point.")
return response[:count]

Expand Down Expand Up @@ -579,13 +585,13 @@ def user_posts(self, userID, secUID, count=30, cursor=0, **kwargs) -> dict:
BASE_URL, self.__add_url_params__(), urlencode(query)
)

res = self.get_data(url=api_url, **kwargs)
res = self.get_data(url=api_url, send_tt_params=True, **kwargs)

if "itemList" in res.keys():
for t in res.get("itemList", []):
response.append(t)

if not res["hasMore"] and not first:
if not res.get("hasMore", False) and not first:
logging.info("TikTok isn't sending more TikToks beyond this point.")
return response

Expand Down Expand Up @@ -663,7 +669,7 @@ def user_page(self, userID, secUID, page_size=30, cursor=0, **kwargs) -> dict:
)
)

return self.get_data(url=api_url, **kwargs)
return self.get_data(url=api_url, send_tt_params=True, **kwargs)

def get_user_pager(self, username, page_size=30, cursor=0, **kwargs):
"""Returns a generator to page through a user's feed
Expand Down Expand Up @@ -769,7 +775,7 @@ def user_liked(self, userID, secUID, count=30, cursor=0, **kwargs) -> dict:
for t in res.get("itemList", []):
response.append(t)

if not res["hasMore"] and not first:
if not res.get("hasMore", False) and not first:
logging.info("TikTok isn't sending more TikToks beyond this point.")
return response

Expand Down Expand Up @@ -846,7 +852,7 @@ def by_sound(self, id, count=30, offset=0, **kwargs) -> dict:
BASE_URL, self.__add_url_params__(), urlencode(query)
)

res = self.get_data(url=api_url, **kwargs)
res = self.get_data(url=api_url, send_tt_params=True, **kwargs)

try:
for t in res["items"]:
Expand All @@ -855,7 +861,7 @@ def by_sound(self, id, count=30, offset=0, **kwargs) -> dict:
for t in res.get("itemList", []):
response.append(t)

if not res["hasMore"]:
if not res.get("hasMore", False):
logging.info("TikTok isn't sending more TikToks beyond this point.")
return response

Expand Down Expand Up @@ -974,7 +980,7 @@ def by_hashtag(self, hashtag, count=30, offset=0, **kwargs) -> dict:
for t in res.get("itemList", []):
response.append(t)

if not res["hasMore"]:
if not res.get("hasMore", False):
logging.info("TikTok isn't sending more TikToks beyond this point.")
return response

Expand Down Expand Up @@ -1054,7 +1060,7 @@ def get_recommended_tiktoks_by_video_id(self, id, count=30, **kwargs) -> dict:
for t in res.get("itemList", []):
response.append(t)

if not res["hasMore"] and not first:
if not res.get("hasMore", False) and not first:
logging.info("TikTok isn't sending more TikToks beyond this point.")
return response[:count]

Expand Down Expand Up @@ -1696,4 +1702,4 @@ def __process_kwargs__(self, kwargs):
device_id = self.custom_device_id
else:
device_id = "".join(random.choice(string.digits) for num in range(19))
return region, language, proxy, maxCount, device_id
return region, language, proxy, maxCount, device_id
4 changes: 2 additions & 2 deletions examples/demo_user_pager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def printPage(page):
user = api.get_user_object(username)
page = api.user_page(user["id"], user["secUid"], page_size=30, after=APR_24)

printPage(page["items"])
new_posts = len(page["items"])
printPage(page['itemList'])
new_posts = len(page['itemList'])
print("{} has {} posts after {}".format(username, new_posts, APR_24))


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
setuptools.setup(
name="TikTokApi",
packages=["TikTokApi"],
version="4.0.1",
version="4.0.2",
license="MIT",
description="The Unofficial TikTok API Wrapper in Python 3.",
author="David Teather",
Expand Down
5 changes: 1 addition & 4 deletions tests/test_by_hashtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ def unique_count(tiktoks):

def test_hashtag():
assert len(api.by_hashtag("funny", 5)) == 5
assert len(api.by_hashtag("funny", 10)) == 10
assert len(api.by_hashtag("funny", 20)) == 20
# Grant A Little Lenience of at most a 15 difference
assert abs(len(unique_count(api.by_hashtag("funny", 500))) - 500) <= 15
assert len(api.by_hashtag("funny", 50)) == 50


def test_non_latin1():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_suggested.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def test_suggested():
assert len(api.get_suggested_users_by_id()) > 0
assert len(api.get_suggested_hashtags_by_id()) > 0
assert len(api.get_suggested_users_by_id()) > 0
assert len(api.get_suggested_music_by_id()) > 0


def test_suggested_crawlers():
Expand Down