forked from Ikaros-521/AI-Vtuber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bilibili.py
46 lines (34 loc) · 1.15 KB
/
bilibili.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import logging
# 导入所需的库
from bilibili_api import live, sync
from utils.common import Common
from utils.logger import Configure_logger
from utils.my_handle import My_handle
def start_server():
common = Common()
# 日志文件路径
log_path = "./log/log-" + common.get_bj_time(1) + ".txt"
Configure_logger(log_path)
my_handle = My_handle("config.json")
if my_handle is None:
logging.info("程序初始化失败!")
exit(0)
# 初始化 Bilibili 直播间
room = live.LiveDanmaku(my_handle.get_room_id())
@room.on('DANMU_MSG')
async def on_danmaku(event):
"""
处理直播间弹幕事件
:param event: 弹幕事件数据
"""
content = event["data"]["info"][1] # 获取弹幕内容
user_name = event["data"]["info"][2][1] # 获取发送弹幕的用户昵称
my_handle.commit_handle(user_name, content)
try:
# 启动 Bilibili 直播间连接
sync(room.connect())
except KeyboardInterrupt:
logging.warning('程序被强行退出')
finally:
logging.warning('关闭连接...')
exit(0)