Closed KalvinThien closed 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())
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)
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.