blivedm/sample.py

49 lines
1.5 KiB
Python
Raw Normal View History

2018-05-13 21:57:36 +08:00
# -*- coding: utf-8 -*-
2019-02-20 00:25:14 +08:00
import asyncio
2019-06-06 21:50:51 +08:00
import blivedm
2019-06-06 21:50:51 +08:00
class MyBLiveClient(blivedm.BLiveClient):
2019-03-23 23:58:02 +08:00
# 演示如何自定义handler
2019-06-06 21:50:51 +08:00
_COMMAND_HANDLERS = blivedm.BLiveClient._COMMAND_HANDLERS.copy()
2019-06-06 21:50:51 +08:00
async def __on_vip_enter(self, command):
print(command)
_COMMAND_HANDLERS['WELCOME'] = __on_vip_enter # 老爷入场
async def _on_receive_popularity(self, popularity: int):
2019-03-23 23:58:02 +08:00
print(f'当前人气值:{popularity}')
2019-06-06 21:50:51 +08:00
async def _on_receive_danmaku(self, danmaku: blivedm.DanmakuMessage):
print(f'{danmaku.uname}{danmaku.msg}')
async def _on_receive_gift(self, gift: blivedm.GiftMessage):
print(f'{gift.uname} 赠送{gift.gift_name}x{gift.num} {gift.coin_type}币x{gift.total_coin}')
2019-03-23 23:58:02 +08:00
2019-06-06 21:50:51 +08:00
async def _on_buy_guard(self, message: blivedm.GuardBuyMessage):
print(f'{message.username} 购买{message.gift_name}')
2019-09-23 21:43:37 +08:00
async def _on_super_chat(self, message: blivedm.SuperChatMessage):
print(f'醒目留言 ¥{message.price} {message.uname}{message.message}')
2019-06-06 21:50:51 +08:00
async def main():
2021-12-12 19:28:12 +08:00
# 直播间ID的取值看直播间URL
# 如果SSL验证失败就把ssl设为FalseB站真的有过忘续证书的情况
client = MyBLiveClient(room_id=21449083, ssl=True)
2019-06-06 21:50:51 +08:00
future = client.start()
try:
# 5秒后停止测试用
# await asyncio.sleep(5)
2019-06-06 21:50:51 +08:00
# future = client.stop()
await future
finally:
await client.close()
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())