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

[BUG] - TypeError: a bytes-like object is required, not 'coroutine' #1042

Open
mrthevinh opened this issue Aug 14, 2023 · 3 comments
Open

[BUG] - TypeError: a bytes-like object is required, not 'coroutine' #1042

mrthevinh opened this issue Aug 14, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@mrthevinh
Copy link

mrthevinh commented Aug 14, 2023

Fill Out the template :)

Describe the bug

A clear and concise description of what the bug is.

** File "C:\Program Files\Python310\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Program Files\Python310\lib\asyncio\base_events.py", line 649, in run_until_complete
return future.result()
File "E:\Documents\GitHub\tooltik\toptop.py", line 15, in trending_videos
out_file.write(video_data)
TypeError: a bytes-like object is required, not 'coroutine'
sys:1: RuntimeWarning: coroutine 'Video.bytes' was never awaited**

I cant save tiktok video with tiktok api:
`from TikTokApi import TikTokApi
import asyncio
import os

ms_token = os.environ.get("ms-token", None) # get your own ms_token from your cookies on tiktok.com

async def trending_videos():
async with TikTokApi() as api:
await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3)
tag = api.hashtag(name="funny")
async for video in tag.videos(count=10):
video_data = video.bytes()
with open(f"out{video.id}.mp4", "wb") as out_file:
out_file.write(video_data)

if name == "main":

asyncio.run(trending_videos())`


Desktop (please complete the following information):

  • OS: [e.g. Windows 10]
  • TikTokApi Version [e.g. 5.0.0] - if out of date upgrade before posting an issue

Additional context

Add any other context about the problem here.

@mrthevinh mrthevinh added the bug Something isn't working label Aug 14, 2023
@HowdyEarth
Copy link

Your first hint is right there in your error message... you should await the video.bytes coroutine.

After doing that, you will get a "NotImplementedError," and looking through the code, you will find that this error is forcibly raised. In other words, the current version does not have this functionality at all. It is not a bug.

It would be nice if it was implemented soon.

@Ahelsamahy
Copy link

I found this answer from @razgino11 in #1040

from TikTokApi import TikTokApi
from yt_dlp import YoutubeDL
import asyncio
import os

ms_token = os.environ.get("msToken", None) # get your own ms_token from your cookies on tiktok.com
ydl_opts = {
    'outtmpl': '%(uploader)s_%(id)s_%(timestamp)s.%(ext)s',
}

async def trending_videos():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, 
                                  headless=False, suppress_resource_load_types=["image", "media", "font", "stylesheet"])


        async for video in api.trending.videos():
            print("username: " + video.author.username)
            print("video id: " + video.id)
            print("stats: " + str(video.stats))

            with YoutubeDL(ydl_opts) as ydl:
                ydl.download(["https://www.tiktok.com/@" + video.author.username + "/video/" + video.id])



if __name__ == "__main__":
    asyncio.run(trending_videos())

make sure to run pip install yt-dlp first

@VMASPAD
Copy link

VMASPAD commented Jun 20, 2024

IN THE CASE THAT IT BITES YOU TO INSTALL LIBRARIES LIKE PANDAS OR SOME OTHER LIBRARY, JUST INSTALL

Repository Repository

I had to install this (latest)

pip install browser_cookie3
pip install pandas

``I found the solution to the problem, I had the same errors but making some modifications and changing the type and the way in which I obtained the videos, I modified:

add two variables

        user = api.user(username)
        videos = user.videos()

change the for to iterate over the videos found in the account and then another variable

        async for video in videos:
            print(f"Username: {video.author.username}")
            print(f"Video ID: {video.id}")
            print(f"Stats: {video.stats}")

            video_url = f"https://www.tiktok.com/@{username}/video/{video.id}"

sincerely I do not know what differences there are with the other method (speaking of the variable video_url) but well it works and then I leave as this the part that downloads the video from YoutubeDL

and at the end of the code add the variable username in which will start the code that is the user that we want to download the videos, I clarify that this will download all the videos of the user from top to bottom.

if __name__ == "__main__":
    username = 'username'
    asyncio.run(download_user_videos(username))

and regarding the token, honestly I did not use the ms_token and I do not know what it is for hahahahahah xd but the token that worked for me was the "multi_sids" that is the domain tiktok.com SPECIFICALLY THAT one

code complete:

from TikTokApi import TikTokApi
from yt_dlp import YoutubeDL
import asyncio
import os

ms_token = os.environ.get("multi_sids", None) 
ydl_opts = {
    'outtmpl': '%(uploader)s_%(id)s_%(timestamp)s.%(ext)s',
}

async def download_user_videos(username):
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, 
                                  headless=False, suppress_resource_load_types=["image", "media", "font", "stylesheet"])

        user = api.user(username)
        videos = user.videos()

        async for video in videos:
            print(f"Username: {video.author.username}")
            print(f"Video ID: {video.id}")
            print(f"Stats: {video.stats}")

            video_url = f"https://www.tiktok.com/@{username}/video/{video.id}"
            with YoutubeDL(ydl_opts) as ydl:
                ydl.download([video_url])

if __name__ == "__main__":
    username = 'username'
    asyncio.run(download_user_videos(username))

I clarify that the verisions of the libraries I am using are:

Name: TikTokApi
Version: 6.3.0
Name: yt-dlp
Version: 2024.5.27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants