davidteather / TikTok-Api

The Unofficial TikTok API Wrapper In Python
https://davidteather.github.io/TikTok-Api
MIT License
4.52k stars 928 forks source link

[INSTALLATION] - I managed to run my script perfectly! but seems that we are stuck with webpage limit? #1166

Open vagvalas opened 1 week ago

vagvalas commented 1 week ago

Please first check the closed issues on GitHub for people with similar problems to you. If you'd like more instant help from the community consider joining the discord

I had overcome headless=false on macOS , i modify my script using a help here and chat's gpt but even its working perfect and fetching ALL videos from a hashtag, it seems that only fetches 45-50 videos, (every-time the same) as i browse in the webpage https://tiktok.com/tag/.... and i can also see these videos. The point is that even if i logged in, and inject cookie again from browser only this amount can be fetched? Am i missing something? So only through app can we get more? Is there any workaround?

Put the error trace below if there's any error thrown.

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_hashtag_videos(hashtag):
    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"])

        tag = api.hashtag(name=hashtag)
        cursor = 0

        while True:
            try:
                videos = tag.videos(count=100, cursor=cursor)
                fetched_any = False
                async for video in videos:
                    fetched_any = True
                    video_url = f"https://www.tiktok.com/@{video.author.username}/video/{video.id}"
                    print(video_url)
                    with YoutubeDL(ydl_opts) as ydl:
                        ydl.download([video_url])
                if not fetched_any:
                    break
                cursor += 100  # or use the next cursor value provided in the response
            except Exception as e:
                print(f"An error occurred: {e}")
                break

if __name__ == "__main__":
    hashtag = 'coldplayathens'
    asyncio.run(download_hashtag_videos(hashtag))