1
1
from TikTokLive import TikTokLiveClient
2
- from TikTokLive .types .events import CommentEvent , ConnectEvent , DisconnectEvent , JoinEvent
2
+ from TikTokLive .types .events import CommentEvent , ConnectEvent , DisconnectEvent , JoinEvent , GiftEvent
3
+ from TikTokLive .types .errors import LiveNotFound
3
4
4
- proxies = {
5
- "http://" : "http://127.0.0.1:10809" ,
6
- "https://" : "http://127.0.0.1:10809"
7
- }
5
+ # proxies = {
6
+ # "http://": "http://127.0.0.1:10809",
7
+ # "https://": "http://127.0.0.1:10809"
8
+ # }
8
9
9
- # Instantiate the client with the user's username
10
- client : TikTokLiveClient = TikTokLiveClient (unique_id = "@markus864" , proxies = proxies )
10
+ proxies = None
11
+
12
+
13
+ # 代理软件开启TUN模式进行代理,由于库的ws不走传入的代理参数,只能靠代理软件全代理了
14
+ client : TikTokLiveClient = TikTokLiveClient (unique_id = "@blacktiebreaks" , proxies = proxies )
11
15
12
16
13
17
# Define how you want to handle specific events via decorator
@@ -24,14 +28,39 @@ async def on_join(event: JoinEvent):
24
28
print (f"@{ event .user .unique_id } joined the stream!" )
25
29
26
30
# Notice no decorator?
31
+ @client .on ("comment" )
27
32
async def on_comment (event : CommentEvent ):
28
33
print (f"{ event .user .nickname } -> { event .comment } " )
29
34
35
+ @client .on ("gift" )
36
+ async def on_gift (event : GiftEvent ):
37
+ """
38
+ This is an example for the "gift" event to show you how to read gift data properly.
39
+
40
+ Important Note:
41
+
42
+ Gifts of type 1 can have streaks, so we need to check that the streak has ended
43
+ If the gift type isn't 1, it can't repeat. Therefore, we can go straight to printing
44
+
45
+ """
46
+
47
+ # Streakable gift & streak is over
48
+ if event .gift .streakable and not event .gift .streaking :
49
+ print (f"{ event .user .unique_id } sent { event .gift .count } x \" { event .gift .info .name } \" " )
50
+
51
+ # Non-streakable gift
52
+ elif not event .gift .streakable :
53
+ print (f"{ event .user .unique_id } sent \" { event .gift .info .name } \" " )
30
54
31
55
# Define handling an event via a "callback"
32
- client .add_listener ("comment" , on_comment )
56
+ # client.add_listener("comment", on_comment)
33
57
34
58
if __name__ == '__main__' :
35
59
# Run the client and block the main thread
36
60
# await client.start() to run non-blocking
37
- client .run ()
61
+ try :
62
+ client .run ()
63
+
64
+ except LiveNotFound :
65
+ print (f"User `@{ client .unique_id } ` seems to be offline, retrying after 1 minute..." )
66
+
0 commit comments