2018-05-13 21:57:36 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2019-02-20 00:25:14 +08:00
|
|
|
|
import asyncio
|
2019-02-19 23:15:00 +08:00
|
|
|
|
import sys
|
|
|
|
|
from ssl import SSLError
|
2018-06-03 14:06:00 +08:00
|
|
|
|
|
2019-02-19 23:15:00 +08:00
|
|
|
|
from blivedm import BLiveClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyBLiveClient(BLiveClient):
|
|
|
|
|
|
|
|
|
|
async def _on_get_popularity(self, popularity):
|
|
|
|
|
print('当前人气值:', popularity)
|
|
|
|
|
|
|
|
|
|
async def _on_get_danmaku(self, content, user_name):
|
|
|
|
|
print(user_name, '说:', content)
|
|
|
|
|
|
|
|
|
|
def _on_stop(self, exc):
|
2019-02-20 00:25:14 +08:00
|
|
|
|
# 执行self.close,然后关闭事件循环
|
|
|
|
|
asyncio.ensure_future(
|
|
|
|
|
self.close(), loop=self._loop
|
|
|
|
|
).add_done_callback(
|
|
|
|
|
lambda future: self._loop.stop()
|
|
|
|
|
)
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
|
|
|
|
def _handle_error(self, exc):
|
|
|
|
|
print(exc, file=sys.stderr)
|
|
|
|
|
if isinstance(exc, SSLError):
|
|
|
|
|
print('SSL验证失败!', file=sys.stderr)
|
|
|
|
|
return False
|
2019-02-17 17:01:52 +08:00
|
|
|
|
|
2018-05-13 21:57:36 +08:00
|
|
|
|
|
|
|
|
|
def main():
|
2019-02-20 00:25:14 +08:00
|
|
|
|
loop = asyncio.get_event_loop()
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2019-02-20 00:25:14 +08:00
|
|
|
|
# 139是黑桐谷歌的直播间
|
2019-02-19 23:15:00 +08:00
|
|
|
|
# 如果SSL验证失败就把第二个参数设为False
|
|
|
|
|
client = MyBLiveClient(139, True)
|
|
|
|
|
client.start()
|
|
|
|
|
|
|
|
|
|
# 5秒后停止,测试用
|
|
|
|
|
# loop.call_later(5, client.stop)
|
|
|
|
|
# 按Ctrl+C停止
|
|
|
|
|
# import signal
|
|
|
|
|
# signal.signal(signal.SIGINT, lambda signum, frame: client.stop())
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
loop.run_forever()
|
|
|
|
|
finally:
|
|
|
|
|
loop.close()
|
2018-05-13 21:57:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|