2018-05-13 21:57:36 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2019-02-20 00:25:14 +08:00
|
|
|
|
import asyncio
|
2023-09-02 12:40:38 +08:00
|
|
|
|
import http.cookies
|
2021-12-18 18:05:07 +08:00
|
|
|
|
import random
|
2023-09-03 16:53:08 +08:00
|
|
|
|
from typing import *
|
2018-06-03 14:06:00 +08:00
|
|
|
|
|
2023-09-02 12:40:38 +08:00
|
|
|
|
import aiohttp
|
|
|
|
|
|
2019-06-06 21:50:51 +08:00
|
|
|
|
import blivedm
|
2023-09-03 16:53:08 +08:00
|
|
|
|
import blivedm.models.web as web_models
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2021-12-18 18:05:07 +08:00
|
|
|
|
# 直播间ID的取值看直播间URL
|
|
|
|
|
TEST_ROOM_IDS = [
|
|
|
|
|
12235923,
|
|
|
|
|
14327465,
|
|
|
|
|
21396545,
|
|
|
|
|
21449083,
|
|
|
|
|
23105590,
|
|
|
|
|
]
|
|
|
|
|
|
2023-09-03 18:09:12 +08:00
|
|
|
|
# 这里填一个已登录账号的cookie。不填cookie也可以连接,但是收到弹幕的用户名会打码,UID会变成0
|
|
|
|
|
SESSDATA = ''
|
|
|
|
|
|
2023-09-03 16:53:08 +08:00
|
|
|
|
session: Optional[aiohttp.ClientSession] = None
|
2023-09-02 12:40:38 +08:00
|
|
|
|
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2021-12-12 21:54:07 +08:00
|
|
|
|
async def main():
|
2023-09-02 12:40:38 +08:00
|
|
|
|
init_session()
|
2023-09-03 16:53:08 +08:00
|
|
|
|
try:
|
|
|
|
|
await run_single_client()
|
|
|
|
|
await run_multi_clients()
|
|
|
|
|
finally:
|
|
|
|
|
await session.close()
|
2021-12-18 18:05:07 +08:00
|
|
|
|
|
|
|
|
|
|
2023-09-02 12:40:38 +08:00
|
|
|
|
def init_session():
|
|
|
|
|
cookies = http.cookies.SimpleCookie()
|
2023-09-03 18:09:12 +08:00
|
|
|
|
cookies['SESSDATA'] = SESSDATA
|
2023-09-02 12:40:38 +08:00
|
|
|
|
cookies['SESSDATA']['domain'] = 'bilibili.com'
|
|
|
|
|
|
|
|
|
|
global session
|
|
|
|
|
session = aiohttp.ClientSession()
|
|
|
|
|
session.cookie_jar.update_cookies(cookies)
|
|
|
|
|
|
|
|
|
|
|
2021-12-18 18:05:07 +08:00
|
|
|
|
async def run_single_client():
|
|
|
|
|
"""
|
|
|
|
|
演示监听一个直播间
|
|
|
|
|
"""
|
|
|
|
|
room_id = random.choice(TEST_ROOM_IDS)
|
2023-09-03 18:09:12 +08:00
|
|
|
|
client = blivedm.BLiveClient(room_id, session=session)
|
2021-12-13 00:07:00 +08:00
|
|
|
|
handler = MyHandler()
|
2023-09-03 18:09:12 +08:00
|
|
|
|
client.set_handler(handler)
|
2021-12-13 00:07:00 +08:00
|
|
|
|
|
2021-12-15 00:09:07 +08:00
|
|
|
|
client.start()
|
2021-12-12 21:54:07 +08:00
|
|
|
|
try:
|
2021-12-18 18:05:07 +08:00
|
|
|
|
# 演示5秒后停止
|
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
|
client.stop()
|
2021-12-12 21:54:07 +08:00
|
|
|
|
|
2021-12-15 00:09:07 +08:00
|
|
|
|
await client.join()
|
2021-12-12 21:54:07 +08:00
|
|
|
|
finally:
|
2021-12-18 18:05:07 +08:00
|
|
|
|
await client.stop_and_close()
|
2021-12-12 21:54:07 +08:00
|
|
|
|
|
|
|
|
|
|
2023-03-25 18:28:32 +08:00
|
|
|
|
async def run_multi_clients():
|
2021-12-18 18:05:07 +08:00
|
|
|
|
"""
|
|
|
|
|
演示同时监听多个直播间
|
|
|
|
|
"""
|
2023-09-02 12:40:38 +08:00
|
|
|
|
clients = [blivedm.BLiveClient(room_id, session=session) for room_id in TEST_ROOM_IDS]
|
2021-12-18 18:05:07 +08:00
|
|
|
|
handler = MyHandler()
|
|
|
|
|
for client in clients:
|
2023-09-03 18:09:12 +08:00
|
|
|
|
client.set_handler(handler)
|
2021-12-18 18:05:07 +08:00
|
|
|
|
client.start()
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2021-12-18 18:05:07 +08:00
|
|
|
|
try:
|
|
|
|
|
await asyncio.gather(*(
|
|
|
|
|
client.join() for client in clients
|
|
|
|
|
))
|
|
|
|
|
finally:
|
|
|
|
|
await asyncio.gather(*(
|
|
|
|
|
client.stop_and_close() for client in clients
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyHandler(blivedm.BaseHandler):
|
|
|
|
|
# # 演示如何添加自定义回调
|
|
|
|
|
# _CMD_CALLBACK_DICT = blivedm.BaseHandler._CMD_CALLBACK_DICT.copy()
|
|
|
|
|
#
|
|
|
|
|
# # 入场消息回调
|
2023-09-03 18:09:12 +08:00
|
|
|
|
# def __interact_word_callback(self, client: blivedm.BLiveClient, command: dict):
|
2021-12-18 18:05:07 +08:00
|
|
|
|
# print(f"[{client.room_id}] INTERACT_WORD: self_type={type(self).__name__}, room_id={client.room_id},"
|
|
|
|
|
# f" uname={command['data']['uname']}")
|
|
|
|
|
# _CMD_CALLBACK_DICT['INTERACT_WORD'] = __interact_word_callback # noqa
|
2019-06-06 21:50:51 +08:00
|
|
|
|
|
2023-09-03 18:09:12 +08:00
|
|
|
|
def _on_heartbeat(self, client: blivedm.BLiveClient, message: web_models.HeartbeatMessage):
|
2023-09-03 16:53:08 +08:00
|
|
|
|
print(f'[{client.room_id}] 心跳')
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2023-09-03 18:09:12 +08:00
|
|
|
|
def _on_danmaku(self, client: blivedm.BLiveClient, message: web_models.DanmakuMessage):
|
2021-12-18 18:05:07 +08:00
|
|
|
|
print(f'[{client.room_id}] {message.uname}:{message.msg}')
|
2019-06-06 21:50:51 +08:00
|
|
|
|
|
2023-09-03 18:09:12 +08:00
|
|
|
|
def _on_gift(self, client: blivedm.BLiveClient, message: web_models.GiftMessage):
|
2021-12-18 18:05:07 +08:00
|
|
|
|
print(f'[{client.room_id}] {message.uname} 赠送{message.gift_name}x{message.num}'
|
|
|
|
|
f' ({message.coin_type}瓜子x{message.total_coin})')
|
2019-03-23 23:58:02 +08:00
|
|
|
|
|
2023-09-03 18:09:12 +08:00
|
|
|
|
def _on_buy_guard(self, client: blivedm.BLiveClient, message: web_models.GuardBuyMessage):
|
2021-12-18 18:05:07 +08:00
|
|
|
|
print(f'[{client.room_id}] {message.username} 购买{message.gift_name}')
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2023-09-03 18:09:12 +08:00
|
|
|
|
def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperChatMessage):
|
2021-12-18 18:05:07 +08:00
|
|
|
|
print(f'[{client.room_id}] 醒目留言 ¥{message.price} {message.uname}:{message.message}')
|
2019-09-23 21:43:37 +08:00
|
|
|
|
|
2019-02-19 23:15:00 +08:00
|
|
|
|
|
2018-05-13 21:57:36 +08:00
|
|
|
|
if __name__ == '__main__':
|
2023-03-26 00:51:50 +08:00
|
|
|
|
asyncio.run(main())
|