支持自动获取UID、添加使用已登录账号连接的演示
This commit is contained in:
22
sample.py
22
sample.py
@@ -1,7 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import asyncio
|
||||
import http.cookies
|
||||
import random
|
||||
|
||||
import aiohttp
|
||||
|
||||
import blivedm
|
||||
|
||||
# 直播间ID的取值看直播间URL
|
||||
@@ -13,19 +16,34 @@ TEST_ROOM_IDS = [
|
||||
23105590,
|
||||
]
|
||||
|
||||
session = None
|
||||
|
||||
|
||||
async def main():
|
||||
init_session()
|
||||
|
||||
await run_single_client()
|
||||
await run_multi_clients()
|
||||
|
||||
|
||||
def init_session():
|
||||
# 这里填一个已登录账号的cookie。不填cookie也可以连接,但是收到弹幕的用户名会打码,UID会变成0
|
||||
cookies = http.cookies.SimpleCookie()
|
||||
cookies['SESSDATA'] = ''
|
||||
cookies['SESSDATA']['domain'] = 'bilibili.com'
|
||||
|
||||
global session
|
||||
session = aiohttp.ClientSession()
|
||||
session.cookie_jar.update_cookies(cookies)
|
||||
|
||||
|
||||
async def run_single_client():
|
||||
"""
|
||||
演示监听一个直播间
|
||||
"""
|
||||
room_id = random.choice(TEST_ROOM_IDS)
|
||||
# 如果SSL验证失败就把ssl设为False,B站真的有过忘续证书的情况
|
||||
client = blivedm.BLiveClient(room_id, ssl=True)
|
||||
client = blivedm.BLiveClient(room_id, session=session, ssl=True)
|
||||
handler = MyHandler()
|
||||
client.add_handler(handler)
|
||||
|
||||
@@ -44,7 +62,7 @@ async def run_multi_clients():
|
||||
"""
|
||||
演示同时监听多个直播间
|
||||
"""
|
||||
clients = [blivedm.BLiveClient(room_id) for room_id in TEST_ROOM_IDS]
|
||||
clients = [blivedm.BLiveClient(room_id, session=session) for room_id in TEST_ROOM_IDS]
|
||||
handler = MyHandler()
|
||||
for client in clients:
|
||||
client.add_handler(handler)
|
||||
|
||||
Reference in New Issue
Block a user