From 63ef4ac97183f02f88a2aa97515e3d0c19d5a7cc Mon Sep 17 00:00:00 2001 From: John Smith Date: Wed, 13 Sep 2023 00:45:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E8=BA=AB=E4=BB=BD=E7=A0=81=EF=BC=8C=E9=81=BF=E5=85=8D=E6=97=A7?= =?UTF-8?q?=E7=89=88=E7=9A=84=E8=AF=B7=E6=B1=82=E4=BC=A0=E5=88=B0=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/open_live.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/api/open_live.py b/api/open_live.py index 325cd2a..8180cb3 100644 --- a/api/open_live.py +++ b/api/open_live.py @@ -10,6 +10,7 @@ import re from typing import * import aiohttp +import cachetools import tornado.web import api.base @@ -27,6 +28,8 @@ START_GAME_COMMON_SERVER_URL = COMMON_SERVER_BASE_URL + '/api/internal/open_live END_GAME_COMMON_SERVER_URL = COMMON_SERVER_BASE_URL + '/api/internal/open_live/end_game' GAME_HEARTBEAT_COMMON_SERVER_URL = COMMON_SERVER_BASE_URL + '/api/internal/open_live/game_heartbeat' +_error_auth_code_cache = cachetools.LRUCache(256) + class TransportError(Exception): """网络错误或HTTP状态码错误""" @@ -66,7 +69,10 @@ async def _request_open_live(url, body: dict) -> dict: # 输错身份码的人太多了,临时处理屏蔽请求,不然要被B站下架了 if url == START_GAME_OPEN_LIVE_URL: - _validate_auth_code(body.get('code', '')) + auth_code = body.get('code', '') + _validate_auth_code(auth_code) + else: + auth_code = '' body_bytes = json.dumps(body).encode('utf-8') headers = { @@ -98,6 +104,8 @@ async def _request_open_live(url, body: dict) -> dict: raise except BusinessError as e: logger.warning('Request open live failed: %s', e) + if e.code == 7007: + _error_auth_code_cache[auth_code] = True raise @@ -115,15 +123,17 @@ async def _read_response(req_ctx_mgr: AsyncContextManager[aiohttp.ClientResponse def _validate_auth_code(auth_code): - # 我也不知道是不是一定是这个格式,先临时这么处理 - if re.fullmatch(r'[0-9A-Z]{12,14}', auth_code): - return - raise BusinessError({ - 'code': 7007, - 'message': '身份码错误', - 'request_id': '0', - 'data': None - }) + if ( + auth_code in _error_auth_code_cache + # 我也不知道是不是一定是这个格式,先临时这么处理 + or not re.fullmatch(r'[0-9A-Z]{12,14}', auth_code) + ): + raise BusinessError({ + 'code': 7007, + 'message': 'oi!oi!oi!你的身份码错误了!别再重试了!!!!!!!!!!', + 'request_id': '0', + 'data': None + }) class _OpenLiveHandlerBase(api.base.ApiHandler):