blivedm/sample.py

48 lines
1.3 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-06-06 21:50:51 +08:00
async def main():
2019-02-20 00:25:14 +08:00
# 139是黑桐谷歌的直播间
# 如果SSL验证失败就把第二个参数设为False
client = MyBLiveClient(139, 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()
# 或者
# future.cancel()
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())