Files
gitlab-instance-0a899031_ti…/main.py
2025-08-03 16:45:06 +08:00

68 lines
2.2 KiB
Python

import asyncio
from TikTokLive import TikTokLiveClient
from TikTokLive.client.logger import LogLevel
from TikTokLive.events import ConnectEvent, GiftEvent,JoinEvent, LikeEvent
client: TikTokLiveClient = TikTokLiveClient(
unique_id="@jejaksikemal3"
)
# 加入直播间
@client.on(JoinEvent)
async def on_join(event: JoinEvent) -> None:
client.logger.info(f"User {event.user.unique_id} joined the live room!")
print(f"加入直播间User {event.user.unique_id} joined the live room!")
# You can also access the user object directly
# print(event.user)
# 点赞
@client.on(LikeEvent)
async def on_like(event: LikeEvent) -> None:
client.logger.info(f"Received likes from {event.user.unique_id}!")
print(f"点赞===Received likes from {event.user.unique_id}!")
# You can also access the user object directly
# print(event.user)
# 监听礼物打赏
@client.on(GiftEvent)
async def on_gift(event: GiftEvent):
client.logger.info("Received a gift!")
# Can have a streak and streak is over
if event.gift.streakable and not event.streaking:
print(f"{event.user.unique_id} sent {event.repeat_count}x \"{event.gift.name}\"")
print('连续礼物打赏')
# Cannot have a streak
elif not event.gift.streakable:
print(f"{event.user.unique_id} sent \"{event.gift.name}\"")
print('礼物打赏')
# 检查用户直播是否在线
@client.on(ConnectEvent)
async def on_connect(event: ConnectEvent):
client.logger.info(f"Connected to @{event.unique_id}!")
print(f"Connected to @{event.unique_id}!")
async def check_loop():
# Run 24/7
while True:
# Check if they're live
while not await client.is_live():
client.logger.info("Client is currently not live. Checking again in 60 seconds.")
await asyncio.sleep(60) # Spamming the endpoint will get you blocked
# Connect once they become live
client.logger.info("Requested client is live!")
await client.connect()
if __name__ == '__main__':
client.logger.setLevel(LogLevel.INFO.value)
asyncio.run(check_loop())
# Set the login session ID token BEFORE connecting
# client.web.set_session("session-id-here")
# Connect
client.run()