From f636d344975b977587dde2492777cc698a6ac6b6 Mon Sep 17 00:00:00 2001 From: hisatri Date: Sun, 16 Feb 2025 22:37:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=94=82API=E5=AF=B9?= =?UTF-8?q?=E6=8E=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mod/args/__init__.py | 11 +++++++++-- mod/searchx/api.py | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/mod/args/__init__.py b/mod/args/__init__.py index dbff463..eba82cc 100644 --- a/mod/args/__init__.py +++ b/mod/args/__init__.py @@ -13,7 +13,7 @@ parser.add_argument('--auth', type=str, default='', help='用于验证Header.Authentication字段,建议纯ASCII字符') parser.add_argument("--debug", action="store_true", help="Enable debug mode") parser.add_argument('--ip', type=str, default='*', help='服务器监听IP,默认*') -parser.add_argument('--token', type=str, default='', help='用于翻译歌词的API Token') +parser.add_argument('--token', type=str, default='', help='锂API接口的Token') parser.add_argument('--ai-type', type=str, default='openai', help='AI类型,默认openai') parser.add_argument('--ai-model', type=str, default='gpt-4o-mini', help='AI模型,默认gpt-4o-mini') parser.add_argument('--ai-base-url', type=str, default='https://api.openai.com/v1', help='AI基础URL,默认https://api.openai.com/v1') @@ -181,6 +181,7 @@ def __load_yaml() -> dict|None: def __load_env(self): auth = os.environ.get('API_AUTH', None) port = os.environ.get('API_PORT', None) + api_token = os.environ.get('API_TOKEN', None) ai_type = os.environ.get('API_AI_TYPE', None) ai_model = os.environ.get('API_AI_MODEL', None) ai_base_url = os.environ.get('API_AI_BASE', None) @@ -191,6 +192,8 @@ def __load_env(self): if not isinstance(self.__data.get("server"), dict): self.__data["server"] = {"ip": "*"} self.__data["server"]["port"] = port + if api_token: + self.__data["token"] = api_token if not isinstance(self.__data.get("ai"), dict): self.__data["ai"] = {} if ai_type: @@ -206,6 +209,7 @@ def __load_arg(self): auth = kw_args.auth port = kw_args.port ip = kw_args.ip + token = kw_args.token ai_type = kw_args.ai_type ai_model = kw_args.ai_model ai_base_url = kw_args.ai_base_url @@ -221,6 +225,8 @@ def __load_arg(self): if not isinstance(self.__data.get("server"), dict): self.__data["server"] = {"ip": "*"} self.__data["server"]["ip"] = ip + if token: + self.__data["token"] = token if not isinstance(self.__data.get("ai"), dict): self.__data["ai"] = {} if ai_type: @@ -253,7 +259,8 @@ def __call__(self, *args): "ip": "*", "port": 28883 }, - "auth": {} + "auth": {}, + "token": "" } config = Args(default=default) ~config diff --git a/mod/searchx/api.py b/mod/searchx/api.py index fb99808..98af037 100644 --- a/mod/searchx/api.py +++ b/mod/searchx/api.py @@ -1,9 +1,15 @@ import requests +import logging +from mod.args import args + + +logger = logging.getLogger(__name__) headers = { "Host": "127.0.0.1", - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 LrcAPI" + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 LrcAPI", + "Authorization": args("token") } @@ -11,9 +17,16 @@ def search(title='', artist='', album='') -> list: try: url = f"https://api.lrc.cx/jsonapi?title={title}&artist={artist}&album={album}&path=None&limit=1&api=lrcapi" response = requests.get(url, headers=headers) - return response.json() + if response.status_code == 200: + return response.json() + elif response.status_code == 401: + logger.warning("锂API接口的Token无效,请检查配置") + return [] + else: + logger.warning(f"锂API接口请求失败,状态码:{response.status_code}") + return [] except Exception as e: - print(f"LrcAPI Server - Request failed: {e}") + logger.error(f"锂API接口请求失败,错误:{e}") return []