mirror of
https://github.com/Cp0204/quark-auto-save.git
synced 2026-01-16 09:20:43 +08:00
⚡️ 优化Alist配置及路径处理逻辑
This commit is contained in:
parent
179856adca
commit
d42e2f56d6
@ -1,11 +1,14 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
class Alist:
|
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
|
is_active = False
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
@ -21,8 +24,10 @@ class Alist:
|
|||||||
|
|
||||||
def run(self, task):
|
def run(self, task):
|
||||||
if task.get("savepath"):
|
if task.get("savepath"):
|
||||||
path = self._normalize_path(task["savepath"])
|
full_path = os.path.normpath(
|
||||||
self.refresh(path)
|
os.path.join(self.quark_root_path, task["savepath"].lstrip("/"))
|
||||||
|
).replace("\\", "/")
|
||||||
|
self.refresh(full_path)
|
||||||
|
|
||||||
def get_info(self):
|
def get_info(self):
|
||||||
url = f"{self.url}/api/admin/setting/list"
|
url = f"{self.url}/api/admin/setting/list"
|
||||||
@ -54,22 +59,20 @@ class Alist:
|
|||||||
"per_page": 0,
|
"per_page": 0,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
response = requests.request(
|
response = requests.request("POST", url, headers=headers, json=payload)
|
||||||
"POST", url, headers=headers, json=payload
|
|
||||||
)
|
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
response = response.json()
|
response = response.json()
|
||||||
if response.get("code") == 200:
|
if response.get("code") == 200:
|
||||||
print(f"📁 刷新Alist目录:{path} 成功✅")
|
print(f"📁 刷新Alist目录:[{path}] 成功✅")
|
||||||
return response.get("data")
|
return response.get("data")
|
||||||
elif "object not found" in response.get("message", ""):
|
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 配置")
|
print(f"📁 刷新Alist目录:根目录不存在,请检查 Alist 配置")
|
||||||
return False
|
return False
|
||||||
# 获取父目录
|
# 获取父目录
|
||||||
parent_path = os.path.dirname(path)
|
parent_path = os.path.dirname(path)
|
||||||
print(f"📁 刷新Alist目录:{path} 不存在,转父目录 {parent_path}")
|
print(f"📁 刷新Alist目录:[{path}] 不存在,转父目录 [{parent_path}]")
|
||||||
# 递归刷新父目录
|
# 递归刷新父目录
|
||||||
return self.refresh(parent_path)
|
return self.refresh(parent_path)
|
||||||
else:
|
else:
|
||||||
@ -77,9 +80,3 @@ class Alist:
|
|||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"刷新Alist目录出错: {e}")
|
print(f"刷新Alist目录出错: {e}")
|
||||||
return False
|
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