各种方便的小修改

This commit is contained in:
John Smith
2019-06-06 21:50:51 +08:00
parent 10a6a91e74
commit fe5b4ca2c9
2 changed files with 284 additions and 72 deletions

View File

@@ -2,35 +2,40 @@
import asyncio
from blivedm import BLiveClient
import blivedm
class MyBLiveClient(BLiveClient):
class MyBLiveClient(blivedm.BLiveClient):
# 演示如何自定义handler
_COMMAND_HANDLERS = BLiveClient._COMMAND_HANDLERS.copy()
_COMMAND_HANDLERS['SEND_GIFT'] = lambda client, command: client._my_on_gift(
command['data']['giftName'], command['data']['num'], command['data']['uname'],
command['data']['coin_type'], command['data']['total_coin']
)
_COMMAND_HANDLERS = blivedm.BLiveClient._COMMAND_HANDLERS.copy()
async def _on_get_popularity(self, popularity):
async def __on_vip_enter(self, command):
print(command)
_COMMAND_HANDLERS['WELCOME'] = __on_vip_enter # 老爷入场
async def _on_receive_popularity(self, popularity: int):
print(f'当前人气值:{popularity}')
async def _on_get_danmaku(self, content, user_name):
print(f'{user_name}{content}')
async def _on_receive_danmaku(self, danmaku: blivedm.DanmakuMessage):
print(f'{danmaku.uname}{danmaku.msg}')
async def _my_on_gift(self, gift_name, gift_num, user_name, coin_type, total_coin):
print(f'{user_name} 赠送{gift_name}x{gift_num} {coin_type}币x{total_coin}')
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}')
async def _on_buy_guard(self, message: blivedm.GuardBuyMessage):
print(f'{message.username} 购买{message.gift_name}')
async def async_main():
async def main():
# 139是黑桐谷歌的直播间
# 如果SSL验证失败就把第二个参数设为False
client = MyBLiveClient(139, True)
future = client.run()
future = client.start()
try:
# 5秒后停止测试用
# await asyncio.sleep(5)
# future = client.stop()
# 或者
# future.cancel()
await future
@@ -38,13 +43,5 @@ async def async_main():
await client.close()
def main():
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(async_main())
finally:
loop.close()
if __name__ == '__main__':
main()
asyncio.get_event_loop().run_until_complete(main())