samirelanduk / tiktok-save

A Python tool for backing up your liked and bookmarked videos on tiktok to your computer.
92 stars 13 forks source link

Tiktok API not responding to requests #13

Open Gabe-Kubiak opened 1 year ago

Gabe-Kubiak commented 1 year ago

Hi Sam, Your tool is a huge convenience and I have been using it for the better part of 8 months. I went to use it again this morning and believe the Tiktok API must have changed their JSON formatting a bit as I'm getting empty responses. I have tried rerunning and believe this is the issue. Please let me know when a fix is available or if you are planning on updating. Thanks.

C:\Users\Gabek\Documents\Projects\Tiktok>save.py bookmarked user_data.json saved 0%| | 0/1027 [00:00<?, ?it/s]ERROR:root:TikTok response: 0%| | 0/1027 [00:00<?, ?it/s] Traceback (most recent call last): File "C:\Users\Gabek\Documents\Projects\Tiktok\save.py", line 49, in tiktok_dict = api.get_tiktok_by_id(tiktok_id, custom_did=did) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Gabek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\TikTokApi\tiktok.py", line 1077, in get_tiktok_by_id return self.get_data(url=api_url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Gabek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\TikTokApi\tiktok.py", line 298, in get_data raise EmptyResponseError( TikTokApi.exceptions.EmptyResponseError: Empty response from Tiktok to https://m.tiktok.com/api/item/detail/?aid=1988&app_name=tiktok_web&device_platform=web&referer=&root_referer=&user_agent=Mozilla%252F5.0%2B%28iPhone%253B%2BCPU%2BiPhone%2BOS%2B12_2%2Blike%2BMac%2BOS%2BX%29%2BAppleWebKit%252F605.1.15%2B%28KHTML%2C%2Blike%2BGecko%29%2BVersion%252F16.4%2BMobile%252F15E148%2BSafari%252F604.1&cookie_enabled=true&screen_width=1531&screen_height=1165&browser_language=&browser_platform=&browser_name=&browser_version=&browser_online=true&ac=4g&timezone_name=&appId=1233&appType=m&isAndroid=False&isMobile=False&isIOS=False&OS=windows&itemId=7255830375207800069&language=en&verifyFp=verify_khr3jabg_V7ucdslq_Vrw9_4KPb_AJ1b_Ks706M8zIJTq&did=837347611&_signature=_02B4Z6wo00f01oSvMbQAAIBCjl6J2SQEMLaEvhUAAMWp71

samirelanduk commented 1 year ago

Thanks! I just tried to do my six-weekly backup and yes, I get the same issue. It seems according to https://github.com/davidteather/TikTok-Api/issues/1035 that this is an issue with the TikTok API itself trying to stop these kinds of requests.

There may be a workaround that involves not using the API at all, since the JSON file has all the URLs for the video pages - just need to find a way to turn those HTML file links into MP4 links (which is what the API was doing). Will post back here if I get anything working.

ksykes commented 1 year ago

@samirelanduk, I believe I’m having the same issue. Here’s the errors I’m getting:

0%|                                                 | 0/21277 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "/Users/kait/Sites/fun/tiktok-save/./save.py", line 49, in <module>
    tiktok_dict = api.get_tiktok_by_id(tiktok_id, custom_did=did)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/TikTokApi/tiktok.py", line 1077, in get_tiktok_by_id
    return self.get_data(url=api_url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/TikTokApi/tiktok.py", line 229, in get_data
    verify_fp, did, signature = self.browser.sign_url(**kwargs)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/TikTokApi/browser.py", line 188, in sign_url
    evaluatedPage = page.evaluate(
                    ^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/playwright/sync_api/_generated.py", line 8701, in evaluate
    self._sync(
  File "/opt/homebrew/lib/python3.11/site-packages/playwright/_impl/_sync_base.py", line 109, in _sync
    return task.result()
           ^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/playwright/_impl/_page.py", line 395, in evaluate
    return await self._main_frame.evaluate(expression, arg)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/playwright/_impl/_frame.py", line 277, in evaluate
    await self._channel.send(
  File "/opt/homebrew/lib/python3.11/site-packages/playwright/_impl/_connection.py", line 61, in send
    return await self._connection.wrap_api_call(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/playwright/_impl/_connection.py", line 482, in wrap_api_call
    return await cb()
           ^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/playwright/_impl/_connection.py", line 97, in inner_send
    result = next(iter(done)).result()
             ^^^^^^^^^^^^^^^^^^^^^^^^^
playwright._impl._api_types.Error: TypeError: undefined is not an object (evaluating 'S[A][m(a[oprand[1]],oprand[1])]')

This is on MacOS Ventura 13.4.1.

It also looks like a problem parsing the JSON file to me, but I’m not very familiar with Python, so let me know if I should open a separate issue. I hope your workaround works out. :)

jonasthepony commented 1 year ago

It's a bug in the Python API Wrapper https://github.com/davidteather/TikTok-Api/issues/1012

There is a workaround (downgrading playwright and using login cookies) https://github.com/davidteather/TikTok-Api/issues/1012#issuecomment-1607264627

Or you can upgrade the TikTok API Wrapper to V6 where this issue is fixed with pip install --upgrade TikTokApi which requires code changes to tiktok-save

Edit: I tried to get it to run, but it seems the Wrappter API V6 does currently not support saving the video. (video as bytes is not implemented) Instead I will use yt-dlp to save my favorites now