mirror of
https://github.com/Cp0204/quark-auto-save.git
synced 2026-01-12 07:10:44 +08:00
⚡️ 优化Alist配置及路径处理逻辑
This commit is contained in:
parent
179856adca
commit
d42e2f56d6
@ -1,11 +1,14 @@
|
||||
import os
|
||||
import re
|
||||
import requests
|
||||
|
||||
|
||||
class Alist:
|
||||
|
||||
default_config = {"url": "", "token": "", "path_prefix": "/quark"}
|
||||
default_config = {
|
||||
"url": "", # Alist服务器URL
|
||||
"token": "", # Alist服务器Token
|
||||
"quark_root_path": "/quark", # 夸克根目录在Alist中的挂载路径
|
||||
}
|
||||
is_active = False
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
@ -21,8 +24,10 @@ class Alist:
|
||||
|
||||
def run(self, task):
|
||||
if task.get("savepath"):
|
||||
path = self._normalize_path(task["savepath"])
|
||||
self.refresh(path)
|
||||
full_path = os.path.normpath(
|
||||
os.path.join(self.quark_root_path, task["savepath"].lstrip("/"))
|
||||
).replace("\\", "/")
|
||||
self.refresh(full_path)
|
||||
|
||||
def get_info(self):
|
||||
url = f"{self.url}/api/admin/setting/list"
|
||||
@ -54,22 +59,20 @@ class Alist:
|
||||
"per_page": 0,
|
||||
}
|
||||
try:
|
||||
response = requests.request(
|
||||
"POST", url, headers=headers, json=payload
|
||||
)
|
||||
response = requests.request("POST", url, headers=headers, json=payload)
|
||||
response.raise_for_status()
|
||||
response = response.json()
|
||||
if response.get("code") == 200:
|
||||
print(f"📁 刷新Alist目录:{path} 成功✅")
|
||||
print(f"📁 刷新Alist目录:[{path}] 成功✅")
|
||||
return response.get("data")
|
||||
elif "object not found" in response.get("message", ""):
|
||||
# 如果是根目录就不再往上查找
|
||||
if path == "/" or path == self.path_prefix:
|
||||
if path == "/" or path == self.quark_root_path:
|
||||
print(f"📁 刷新Alist目录:根目录不存在,请检查 Alist 配置")
|
||||
return False
|
||||
# 获取父目录
|
||||
parent_path = os.path.dirname(path)
|
||||
print(f"📁 刷新Alist目录:{path} 不存在,转父目录 {parent_path}")
|
||||
print(f"📁 刷新Alist目录:[{path}] 不存在,转父目录 [{parent_path}]")
|
||||
# 递归刷新父目录
|
||||
return self.refresh(parent_path)
|
||||
else:
|
||||
@ -77,9 +80,3 @@ class Alist:
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"刷新Alist目录出错: {e}")
|
||||
return False
|
||||
|
||||
def _normalize_path(self, path):
|
||||
"""标准化路径格式"""
|
||||
if not path.startswith(self.path_prefix):
|
||||
path = f"/{self.path_prefix}/{path}"
|
||||
return re.sub(r"/{2,}", "/", path)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user