2018-05-13 21:57:36 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2019-02-20 00:25:14 +08:00
|
|
|
|
import asyncio
|
2018-06-03 14:06:00 +08:00
|
|
|
|
|
2019-06-06 21:50:51 +08:00
|
|
|
|
import blivedm
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-12 21:54:07 +08:00
|
|
|
|
async def main():
|
|
|
|
|
# 直播间ID的取值看直播间URL
|
|
|
|
|
# 如果SSL验证失败就把ssl设为False,B站真的有过忘续证书的情况
|
2021-12-15 00:09:07 +08:00
|
|
|
|
client = blivedm.BLiveClient(room_id=21449083, ssl=True)
|
2021-12-13 00:07:00 +08:00
|
|
|
|
handler = MyHandler()
|
|
|
|
|
client.add_handler(handler)
|
|
|
|
|
|
2021-12-15 00:09:07 +08:00
|
|
|
|
client.start()
|
2021-12-12 21:54:07 +08:00
|
|
|
|
try:
|
|
|
|
|
# 5秒后停止,测试用
|
|
|
|
|
# await asyncio.sleep(5)
|
2021-12-15 00:09:07 +08:00
|
|
|
|
# client.stop()
|
2021-12-12 21:54:07 +08:00
|
|
|
|
|
2021-12-15 00:09:07 +08:00
|
|
|
|
await client.join()
|
2021-12-12 21:54:07 +08:00
|
|
|
|
finally:
|
|
|
|
|
await client.close()
|
|
|
|
|
|
|
|
|
|
|
2021-12-13 00:07:00 +08:00
|
|
|
|
class MyHandler(blivedm.BaseHandler):
|
|
|
|
|
# 演示如何添加自定义回调
|
|
|
|
|
_CMD_CALLBACK_DICT = blivedm.BaseHandler._CMD_CALLBACK_DICT.copy()
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2021-12-13 00:07:00 +08:00
|
|
|
|
# 入场消息回调
|
|
|
|
|
async def __interact_word_callback(self, client: blivedm.BLiveClient, command: dict):
|
2021-12-15 00:09:07 +08:00
|
|
|
|
print(f"INTERACT_WORD: self_type={type(self).__name__}, room_id={client.room_id},"
|
|
|
|
|
f" uname={command['data']['uname']}")
|
2021-12-13 00:07:00 +08:00
|
|
|
|
_CMD_CALLBACK_DICT['INTERACT_WORD'] = __interact_word_callback # noqa
|
2019-06-06 21:50:51 +08:00
|
|
|
|
|
2021-12-15 00:09:07 +08:00
|
|
|
|
async def _on_heartbeat(self, client: blivedm.BLiveClient, message: blivedm.HeartbeatMessage):
|
2021-12-13 00:07:00 +08:00
|
|
|
|
print(f'当前人气值:{message.popularity}')
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2021-12-13 00:07:00 +08:00
|
|
|
|
async def _on_danmaku(self, client: blivedm.BLiveClient, message: blivedm.DanmakuMessage):
|
|
|
|
|
print(f'{message.uname}:{message.msg}')
|
2019-06-06 21:50:51 +08:00
|
|
|
|
|
2021-12-13 00:07:00 +08:00
|
|
|
|
async def _on_gift(self, client: blivedm.BLiveClient, message: blivedm.GiftMessage):
|
|
|
|
|
print(f'{message.uname} 赠送{message.gift_name}x{message.num} ({message.coin_type}币x{message.total_coin})')
|
2019-03-23 23:58:02 +08:00
|
|
|
|
|
2021-12-13 00:07:00 +08:00
|
|
|
|
async def _on_buy_guard(self, client: blivedm.BLiveClient, message: blivedm.GuardBuyMessage):
|
2019-06-06 21:50:51 +08:00
|
|
|
|
print(f'{message.username} 购买{message.gift_name}')
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2021-12-13 00:07:00 +08:00
|
|
|
|
async def _on_super_chat(self, client: blivedm.BLiveClient, message: blivedm.SuperChatMessage):
|
2019-09-23 21:43:37 +08:00
|
|
|
|
print(f'醒目留言 ¥{message.price} {message.uname}:{message.message}')
|
|
|
|
|
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2018-05-13 21:57:36 +08:00
|
|
|
|
if __name__ == '__main__':
|
2019-06-06 21:50:51 +08:00
|
|
|
|
asyncio.get_event_loop().run_until_complete(main())
|