修复连接WebSocket时用的UA

This commit is contained in:
John Smith 2023-09-03 12:48:04 +08:00
parent a52af864ad
commit ff766d68db
3 changed files with 11 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import aiohttp
import yarl
from . import ws_base
from .. import utils
__all__ = (
'BLiveClient',
@ -15,10 +16,6 @@ __all__ = (
logger = logging.getLogger('blivedm')
USER_AGENT = (
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
)
UID_INIT_URL = 'https://api.bilibili.com/x/web-interface/nav'
BUVID_INIT_URL = 'https://data.bilibili.com/v/'
ROOM_INIT_URL = 'https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom'
@ -123,7 +120,7 @@ class BLiveClient(ws_base.WebSocketClientBase):
try:
async with self._session.get(
UID_INIT_URL,
headers={'User-Agent': USER_AGENT},
headers={'User-Agent': utils.USER_AGENT},
ssl=self._ssl
) as res:
if res.status != 200:
@ -162,7 +159,7 @@ class BLiveClient(ws_base.WebSocketClientBase):
try:
async with self._session.get(
BUVID_INIT_URL,
headers={'User-Agent': USER_AGENT},
headers={'User-Agent': utils.USER_AGENT},
ssl=self._ssl
) as res:
if res.status != 200:
@ -176,7 +173,7 @@ class BLiveClient(ws_base.WebSocketClientBase):
try:
async with self._session.get(
ROOM_INIT_URL,
headers={'User-Agent': USER_AGENT},
headers={'User-Agent': utils.USER_AGENT},
params={
'room_id': self._tmp_room_id
},
@ -209,7 +206,7 @@ class BLiveClient(ws_base.WebSocketClientBase):
try:
async with self._session.get(
DANMAKU_SERVER_CONF_URL,
headers={'User-Agent': USER_AGENT},
headers={'User-Agent': utils.USER_AGENT},
params={
'id': self._room_id,
'type': 0

View File

@ -11,7 +11,7 @@ from typing import *
import aiohttp
import brotli
from .. import handlers
from .. import handlers, utils
logger = logging.getLogger('blivedm')
@ -259,6 +259,7 @@ class WebSocketClientBase:
# 连接
async with self._session.ws_connect(
self._get_ws_url(retry_count),
headers={'User-Agent': utils.USER_AGENT}, # web端的token也会签名UA
receive_timeout=self._heartbeat_interval + 5,
ssl=self._ssl
) as websocket:

4
blivedm/utils.py Normal file
View File

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
USER_AGENT = (
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
)