isaackogan / TikTokLive

Python library to receive live stream events (comments, gifts, etc.) in realtime from TikTok LIVE.
https://isaackogan.github.io/TikTokLive/
MIT License
752 stars 154 forks source link

. #195

Closed KalvinThien closed 2 months ago

KalvinThien commented 2 months ago

    while True:
        while not await client.is_live():
            client.logger.info(f"{RED}This user has not livestreamed, try again in 60 seconds.")
            await asyncio.sleep(60)  

client.logger.info(f"{GREEN}Valid livestream session, preparing to connect to the livestream session.")
        while True:
            try:
                await client.connect()
                Break
            except Exception as e:
                client.logger.info(f"{RED}The connection has been lost, retrying in 60 seconds... ")
                await asyncio.sleep(60)

await client.run(fetch_room_info=True)
        open("log.txt", "w", encoding="utf-8").close()
        print(f"{RED} saved the active process to the log.txt file, the directory containing the file IS {os.getcwd()}log.txt{RESET}")```

I want to create 1 loop, for example: at the beginning I fill in @user. If not, try again in 60 seconds.
If accessible, call the livestream function.
But if you @user offline, you will wait and try again until @user online livestream, then continue recording.

I tried doing this but it didn't work?

Can you guide me?
isaackogan commented 2 months ago

import asyncio
import logging

from TikTokLive import TikTokLiveClient
from TikTokLive.events import ConnectEvent

client: TikTokLiveClient = TikTokLiveClient(
    unique_id="https://www.tiktok.com/@jarretchambers/live"
)

@client.on(ConnectEvent)
async def on_connect(e: ConnectEvent):
    print(f"Connected to @{e.unique_id}")

async def run(retry_after: int = 60) -> None:
    """
    Loop forever.

    :param retry_after: How long to wait (sec) between retries
    :return: None

    """

    while True:

        # Wait for the client to go live 24/7
        while not await client.is_live():
            client.logger.info(f"The user has not livestreamed, try again in {retry_after} seconds")
            await asyncio.sleep(retry_after)

        # Connect once they're live
        try:
            await client.connect(callback=lambda: print("Live!"))
        except Exception:
            client.logger.info(f"The connection has been lost, retrying in 60 seconds... ")
            await asyncio.sleep(60)

if __name__ == '__main__':
    client.logger.setLevel(logging.INFO)
    asyncio.run(run())
KalvinThien commented 2 months ago

The problem is that after the connection the user is livestreaming, it will record automatically, but if that user turns off the livestream or pauses the livestream

then the program will try to connect again automatically, I tried many ways but when the user finished the livestream nothing happened, the program stood still without trying to connect to the livestream again (delay 60 seconds until reconnecting)

isaackogan commented 2 months ago

Don't you think that's something you should've mentioned...?

Listen to the LiveEndEvent and manually disconnect. Also, stream pause is not the same as stream end.