初始提交: 通用数据同步服务
- 基于OpenResty的RESTful API服务 - 支持Cookie/Token等数据类型的存储和管理 - 登录认证保护 - Web管理界面 - 数据过期管理
This commit is contained in:
Executable
+140
@@ -0,0 +1,140 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
数据迁移脚本 - 从 cookie-receiver 迁移到 data-sync-service
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
OLD_DATA_DIR = Path("/home/hermes/projects/cookie-receiver/data")
|
||||
NEW_DATA_DIR = Path("/home/hermes/projects/data-sync-service/data")
|
||||
|
||||
|
||||
def migrate_tingwu_cookies():
|
||||
"""迁移通义听悟 Cookie 数据"""
|
||||
|
||||
old_file = OLD_DATA_DIR / "tingwu_cookies.json"
|
||||
|
||||
if not old_file.exists():
|
||||
print("⚠️ 旧数据文件不存在,跳过迁移")
|
||||
return
|
||||
|
||||
print(f"📂 读取旧数据: {old_file}")
|
||||
|
||||
with open(old_file, 'r', encoding='utf-8') as f:
|
||||
old_data = json.load(f)
|
||||
|
||||
# 提取账号信息(如果存在)
|
||||
account = old_data.get("account", "ykaayk@qq.com")
|
||||
|
||||
# 构建新的数据格式
|
||||
new_data = {
|
||||
"tag": f"tingwu:{account}",
|
||||
"type": "cookie",
|
||||
"data": old_data,
|
||||
"metadata": {
|
||||
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"updated_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"expires_in": 604800, # 7天
|
||||
"note": "从 cookie-receiver 迁移",
|
||||
"login_url": "https://tingwu.aliyun.com/"
|
||||
}
|
||||
}
|
||||
|
||||
# 生成新文件名
|
||||
filename = f"tingwu_{account.replace('@', '_').replace('.', '_')}.json"
|
||||
new_file = NEW_DATA_DIR / filename
|
||||
|
||||
print(f"💾 保存新数据: {new_file}")
|
||||
|
||||
with open(new_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(new_data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
# 更新索引
|
||||
update_index(new_data, filename)
|
||||
|
||||
print(f"✅ 迁移成功: tingwu_cookies.json → {filename}")
|
||||
|
||||
|
||||
def update_index(data, filename):
|
||||
"""更新索引文件"""
|
||||
|
||||
index_file = NEW_DATA_DIR / "index.json"
|
||||
|
||||
# 读取现有索引
|
||||
if index_file.exists():
|
||||
with open(index_file, 'r', encoding='utf-8') as f:
|
||||
index = json.load(f)
|
||||
else:
|
||||
index = {"tags": []}
|
||||
|
||||
# 添加新 tag
|
||||
tag_entry = {
|
||||
"tag": data["tag"],
|
||||
"type": data["type"],
|
||||
"file": filename,
|
||||
"created_at": data["metadata"]["created_at"],
|
||||
"updated_at": data["metadata"]["updated_at"],
|
||||
"expires_at": None
|
||||
}
|
||||
|
||||
# 计算过期时间
|
||||
if data["metadata"].get("expires_in"):
|
||||
from datetime import datetime, timedelta
|
||||
created = datetime.strptime(data["metadata"]["created_at"], "%Y-%m-%d %H:%M:%S")
|
||||
expires = created + timedelta(seconds=data["metadata"]["expires_in"])
|
||||
tag_entry["expires_at"] = expires.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 检查是否已存在
|
||||
existing = False
|
||||
for i, item in enumerate(index["tags"]):
|
||||
if item["tag"] == data["tag"]:
|
||||
index["tags"][i] = tag_entry
|
||||
existing = True
|
||||
break
|
||||
|
||||
if not existing:
|
||||
index["tags"].append(tag_entry)
|
||||
|
||||
# 保存索引
|
||||
with open(index_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(index, f, indent=2, ensure_ascii=False)
|
||||
|
||||
print(f"📋 索引已更新")
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
|
||||
print("=" * 60)
|
||||
print("数据迁移工具 - cookie-receiver → data-sync-service")
|
||||
print("=" * 60)
|
||||
|
||||
# 检查目录
|
||||
if not OLD_DATA_DIR.exists():
|
||||
print(f"❌ 旧数据目录不存在: {OLD_DATA_DIR}")
|
||||
return
|
||||
|
||||
# 创建新数据目录
|
||||
NEW_DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
print(f"✅ 新数据目录: {NEW_DATA_DIR}")
|
||||
|
||||
# 迁移数据
|
||||
print("\n📦 开始迁移...")
|
||||
migrate_tingwu_cookies()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("✅ 迁移完成!")
|
||||
print("\n下一步:")
|
||||
print("1. 停止旧服务: cd /home/hermes/projects/cookie-receiver && docker-compose down")
|
||||
print("2. 启动新服务: cd /home/hermes/projects/data-sync-service && docker-compose up -d")
|
||||
print("3. 验证数据: curl http://localhost:5001/api/data")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user