def get_files_list(path):
url = f"{OPENLIST_URL}/api/fs/list"
try:
res = requests.post(url, headers=headers, json={"path": path}, timeout=30)
data = res.json()
if data.get("code") == 200:
payload = data.get("data")
if payload and isinstance(payload.get("content"), list):
return [f['name'] for f in payload['content']]
return []
except:
return []
for item in items:
name = item['name']
match = folder_pattern.match(name)
if item['is_dir'] and match:
year, month, day, hour = match.groups()
target_full = f"{TARGET_PATH}/{year}年/{month}月/{day}日/{hour}时"
make_dir(target_full)
old_path = f"{SOURCE_PATH}/{name}"
src_files = get_files_list(old_path)
dst_files = get_files_list(target_full)
to_copy = [f for f in src_files if f not in dst_files]
if to_copy:
print(f"📄 处理: {name}, 发现新文件: {len(to_copy)}个")
res = copy_files(old_path, target_full, to_copy)
if res and res.get("code") == 200:
logs.append(f"✅ {name}: 已同步 {len(to_copy)} 个")
else:
msg = res.get("message", "未知错误") if res else "连接超时"
logs.append(f"❌ {name}: 失败 ({msg})")
else:
print(f"⏩ 跳过: {name} (无更新)")
return "\n".join(logs) if logs else "本次扫描完成,无需备份新文件。"
if __name__ == "__main__":
print("🚀 任务启动")
if not OPENLIST_TOKEN:
print("❌ 错误:未设置 OPENLIST_TOKEN")
else:
try:
msg = run_organize()
print(msg)
send_wxpusher(msg)
except Exception as e:
err = f"脚本执行崩溃: {str(e)}"
print(err)
send_wxpusher(err)
print("✨ 任务结束")