Skip to content

Commit

Permalink
增加锂API对接功能
Browse files Browse the repository at this point in the history
  • Loading branch information
HisAtri committed Feb 16, 2025
1 parent a76fba7 commit f636d34
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
11 changes: 9 additions & 2 deletions mod/args/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -253,7 +259,8 @@ def __call__(self, *args):
"ip": "*",
"port": 28883
},
"auth": {}
"auth": {},
"token": ""
}
config = Args(default=default)
~config
Expand Down
19 changes: 16 additions & 3 deletions mod/searchx/api.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
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")
}


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 []


Expand Down

0 comments on commit f636d34

Please sign in to comment.