diff --git a/blivedm/clients/web.py b/blivedm/clients/web.py index e62313e..f9da291 100644 --- a/blivedm/clients/web.py +++ b/blivedm/clients/web.py @@ -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 diff --git a/blivedm/clients/ws_base.py b/blivedm/clients/ws_base.py index 41495b9..b569e02 100644 --- a/blivedm/clients/ws_base.py +++ b/blivedm/clients/ws_base.py @@ -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: diff --git a/blivedm/utils.py b/blivedm/utils.py new file mode 100644 index 0000000..088aedb --- /dev/null +++ b/blivedm/utils.py @@ -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' +)