void commited on
Commit
7667461
1 Parent(s): 7fa3d74

add sample app

Browse files
Files changed (5) hide show
  1. README.md +3 -0
  2. app.py +90 -0
  3. bubu.wav +0 -0
  4. dingding.wav +0 -0
  5. po.wav +0 -0
README.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ ```python
2
+ python app.py
3
+ ```
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from dm_player import DmPlayer
4
+
5
+ import asyncio
6
+ import http.cookies
7
+ import random
8
+ from typing import *
9
+
10
+ import aiohttp
11
+
12
+ import blivedm
13
+ import blivedm.models.web as web_models
14
+
15
+ # 直播间ID的取值看直播间URL
16
+ TEST_ROOM_ID = 9792252
17
+
18
+ # 这里填一个已登录账号的cookie的SESSDATA字段的值。不填也可以连接,但是收到弹幕的用户名会打码,UID会变成0
19
+ SESSDATA = ''
20
+
21
+ session: Optional[aiohttp.ClientSession] = None
22
+
23
+ dmplayer = DmPlayer()
24
+
25
+ async def dm_monitor():
26
+ init_session()
27
+ try:
28
+ await run_single_client()
29
+ finally:
30
+ await session.close()
31
+
32
+
33
+ def init_session():
34
+ cookies = http.cookies.SimpleCookie()
35
+ cookies['SESSDATA'] = SESSDATA
36
+ cookies['SESSDATA']['domain'] = 'bilibili.com'
37
+
38
+ global session
39
+ session = aiohttp.ClientSession()
40
+ session.cookie_jar.update_cookies(cookies)
41
+
42
+
43
+ async def run_single_client():
44
+ room_id = TEST_ROOM_ID
45
+ client = blivedm.BLiveClient(room_id, session=session)
46
+ handler = MyHandler()
47
+ client.set_handler(handler)
48
+
49
+ client.start()
50
+ try:
51
+ await client.join()
52
+ finally:
53
+ await client.stop_and_close()
54
+
55
+ def test_callback(user, msg):
56
+ # 可以给消息写类似的callback,从而设置l2d表情、换人说话等
57
+ print(f'{user}: {msg}')
58
+
59
+ class MyHandler(blivedm.BaseHandler):
60
+ _CMD_CALLBACK_DICT = blivedm.BaseHandler._CMD_CALLBACK_DICT.copy()
61
+
62
+ def __interact_word_callback(self, client: blivedm.BLiveClient, command: dict):
63
+ uname = command['data']['uname']
64
+ msg_type = command['data']['msg_type']
65
+ if msg_type == 2:
66
+ dmplayer.Add(f'谢谢{uname}的关注喵', "bubu.wav")
67
+
68
+ _CMD_CALLBACK_DICT['INTERACT_WORD'] = __interact_word_callback
69
+
70
+ def _on_heartbeat(self, client: blivedm.BLiveClient, message: web_models.HeartbeatMessage):
71
+ pass
72
+
73
+ def _on_danmaku(self, client: blivedm.BLiveClient, message: web_models.DanmakuMessage):
74
+ dmplayer.Add(message.msg, "bubu.wav", test_callback, message.uname, message.msg) # 带有callback的加入队列
75
+
76
+ def _on_gift(self, client: blivedm.BLiveClient, message: web_models.GiftMessage):
77
+ dmplayer.Add(f'感谢{message.uname}赠送的{message.num}个{message.gift_name},阿里嘎多!', "po.wav") # 普通的加入队列
78
+
79
+ def _on_buy_guard(self, client: blivedm.BLiveClient, message: web_models.GuardBuyMessage):
80
+ dmplayer.Add(f'感谢{message.uname}的{message.gift_name},阿里嘎多!', "po.wav")
81
+
82
+ def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperChatMessage):
83
+ dmplayer.Add(f'{message.uname}说:{message.message}', "dingding.wav")
84
+
85
+
86
+ if __name__ == '__main__':
87
+ dmplayer.Start()
88
+ dmplayer.LoadCharacter("圣园未花")
89
+ dmplayer.SetVoiceOption(0.6, 0.668, 0.9)
90
+ asyncio.run(dm_monitor())
bubu.wav ADDED
Binary file (260 kB). View file
 
dingding.wav ADDED
Binary file (120 kB). View file
 
po.wav ADDED
Binary file (70 kB). View file