初始提交: 通用数据同步服务
- 基于OpenResty的RESTful API服务 - 支持Cookie/Token等数据类型的存储和管理 - 登录认证保护 - Web管理界面 - 数据过期管理
This commit is contained in:
Executable
+85
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
清理过期数据脚本
|
||||
删除已过期超过指定天数的数据
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
DATA_DIR = Path("/home/hermes/projects/data-sync-service/data")
|
||||
DELETE_AFTER_DAYS = 30 # 过期后保留天数
|
||||
|
||||
|
||||
def parse_datetime(dt_str):
|
||||
"""解析时间字符串"""
|
||||
try:
|
||||
return datetime.strptime(dt_str, "%Y-%m-%d %H:%M:%S")
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def clean_expired():
|
||||
"""清理过期数据"""
|
||||
|
||||
index_file = DATA_DIR / "index.json"
|
||||
|
||||
if not index_file.exists():
|
||||
print("⚠️ 索引文件不存在")
|
||||
return
|
||||
|
||||
with open(index_file, 'r', encoding='utf-8') as f:
|
||||
index = json.load(f)
|
||||
|
||||
now = datetime.now()
|
||||
threshold = now - timedelta(days=DELETE_AFTER_DAYS)
|
||||
|
||||
deleted_count = 0
|
||||
new_tags = []
|
||||
|
||||
for item in index.get("tags", []):
|
||||
tag = item.get("tag")
|
||||
expires_at_str = item.get("expires_at")
|
||||
filename = item.get("file")
|
||||
|
||||
if not expires_at_str:
|
||||
# 无过期时间,保留
|
||||
new_tags.append(item)
|
||||
continue
|
||||
|
||||
expires_at = parse_datetime(expires_at_str)
|
||||
if not expires_at:
|
||||
# 解析失败,保留
|
||||
new_tags.append(item)
|
||||
continue
|
||||
|
||||
# 检查是否过期超过阈值
|
||||
if expires_at < threshold:
|
||||
print(f"🗑️ 删除过期数据: {tag} (过期于 {expires_at_str})")
|
||||
|
||||
# 删除数据文件
|
||||
filepath = DATA_DIR / filename
|
||||
if filepath.exists():
|
||||
os.remove(filepath)
|
||||
|
||||
deleted_count += 1
|
||||
else:
|
||||
new_tags.append(item)
|
||||
|
||||
# 更新索引
|
||||
index["tags"] = new_tags
|
||||
with open(index_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(index, f, indent=2, ensure_ascii=False)
|
||||
|
||||
print(f"\n✅ 清理完成: 删除 {deleted_count} 个过期数据")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("=" * 60)
|
||||
print(f"清理过期数据 (过期超过 {DELETE_AFTER_DAYS} 天)")
|
||||
print("=" * 60)
|
||||
|
||||
clean_expired()
|
||||
Reference in New Issue
Block a user