You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I try to retrieve public user data for a list of TikTok accounts. This usually works fine, but there are some issues with this library, which should be fixed.
The buggy code
async with TikTokApi() as api:
await api.create_sessions(
ms_tokens=[ms_token],
num_sessions=1,
sleep_after=3,
executable_path="/opt/google/chrome/google-chrome",
)
user = api.user(username=username)
user_info = None
try:
user_info = await user.info()
print(f"SUCCESS: {username}")
except (KeyError, exceptions.InvalidResponseException, exceptions.EmptyResponseException, Exception) as e:
print(f"ERROR: {e} :: https://www.tiktok.com/@{username} :: {user_info}")
Expected behavior
If the account does not exist, I expect to get a meaningful exception. But when running the code above for a non-existing account I get the following response from TikTok:
In my code example above this response triggers a KeyError instead of a meaningful exception. So I had a look into this library. make_request() in tiktok.py checks, if the result is None or an empty string or if we can decode the JSON or if status_code != 0.
raiseException("TikTokApi.run_fetch_script returned None")
ifresult=="":
raiseEmptyResponseException(result, "TikTok returned an empty response")
try:
data=json.loads(result)
ifdata.get("status_code") !=0:
self.logger.error(f"Got an unexpected status code: {data}")
returndata
exceptjson.decoder.JSONDecodeError:
ifretry_count==retries:
self.logger.error(f"Failed to decode json response: {result}")
raiseInvalidJSONException()
None of this error conditions is true for this response. Then info() in user.py tries to extract data with __extract_from_data() not present in the response, so a KeyError is raised.
By the way, if the account does exist "statusCode": 0 is in the response.
But there is another issue, which makes it hard to distinguish between a non-existing account and this other case. When retrieving user data for several accounts, sometimes this error occurs:
2024-06-04 17:36:14,559 - TikTokApi.tiktok - ERROR - Got an unexpected status code: {'userInfo': {'user': {}, 'stats': {}, 'shareMeta': {}}}
And again in this case I get a KeyError instead of an meaningful exception, since none of the error conditions apply. For both cases user.info() return None by the way.
Thank you for this great library. But the error handling needs some updates, probably because TikTok slightly changed its API.
Desktop (please complete the following information):
OS: Linux 5.15.146.1-microsoft-standard-WSL2
Python 3.12.2
TikTokApi Version 6.3.0
The text was updated successfully, but these errors were encountered:
Describe the bug
I try to retrieve public user data for a list of TikTok accounts. This usually works fine, but there are some issues with this library, which should be fixed.
The buggy code
Expected behavior
If the account does not exist, I expect to get a meaningful exception. But when running the code above for a non-existing account I get the following response from TikTok:
{"extra":{"fatal_item_ids":[],"logid":"...","now":1717514022000},"log_pb": "impr_id":"..."},"statusCode":10221,"status_code":0,"status_msg":"","userInfo":{}}
In my code example above this response triggers a KeyError instead of a meaningful exception. So I had a look into this library.
make_request()
intiktok.py
checks, if the result is None or an empty string or if we can decode the JSON or ifstatus_code != 0
.TikTok-Api/TikTokApi/tiktok.py
Lines 433 to 451 in 5a249c4
None of this error conditions is true for this response. Then
info()
inuser.py
tries to extract data with__extract_from_data()
not present in the response, so a KeyError is raised.By the way, if the account does exist
"statusCode": 0
is in the response.But there is another issue, which makes it hard to distinguish between a non-existing account and this other case. When retrieving user data for several accounts, sometimes this error occurs:
2024-06-04 17:36:14,559 - TikTokApi.tiktok - ERROR - Got an unexpected status code: {'userInfo': {'user': {}, 'stats': {}, 'shareMeta': {}}}
And again in this case I get a KeyError instead of an meaningful exception, since none of the error conditions apply. For both cases
user.info()
return None by the way.Thank you for this great library. But the error handling needs some updates, probably because TikTok slightly changed its API.
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: