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-02-19 23:15:00 +08:00
|
|
|
|
from blivedm import BLiveClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyBLiveClient(BLiveClient):
|
2019-03-23 23:58:02 +08:00
|
|
|
|
# 演示如何自定义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']
|
|
|
|
|
)
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
|
|
|
|
async def _on_get_popularity(self, popularity):
|
2019-03-23 23:58:02 +08:00
|
|
|
|
print(f'当前人气值:{popularity}')
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
|
|
|
|
async def _on_get_danmaku(self, content, user_name):
|
2019-03-23 23:58:02 +08:00
|
|
|
|
print(f'{user_name}:{content}')
|
|
|
|
|
|
|
|
|
|
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})')
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
|
|
|
|
|
2019-02-20 14:12:11 +08:00
|
|
|
|
async def async_main():
|
2019-02-20 00:25:14 +08:00
|
|
|
|
# 139是黑桐谷歌的直播间
|
2019-02-19 23:15:00 +08:00
|
|
|
|
# 如果SSL验证失败就把第二个参数设为False
|
|
|
|
|
client = MyBLiveClient(139, True)
|
2019-02-20 14:12:11 +08:00
|
|
|
|
future = client.run()
|
|
|
|
|
try:
|
|
|
|
|
# 5秒后停止,测试用
|
|
|
|
|
# await asyncio.sleep(5)
|
|
|
|
|
# future.cancel()
|
|
|
|
|
|
|
|
|
|
await future
|
|
|
|
|
finally:
|
|
|
|
|
await client.close()
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
|
|
|
|
|
2019-02-20 14:12:11 +08:00
|
|
|
|
def main():
|
|
|
|
|
loop = asyncio.get_event_loop()
|
2019-02-19 23:15:00 +08:00
|
|
|
|
try:
|
2019-02-20 14:12:11 +08:00
|
|
|
|
loop.run_until_complete(async_main())
|
2019-02-19 23:15:00 +08:00
|
|
|
|
finally:
|
|
|
|
|
loop.close()
|
2018-05-13 21:57:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|