- 基于OpenResty的RESTful API服务 - 支持Cookie/Token等数据类型的存储和管理 - 登录认证保护 - Web管理界面 - 数据过期管理
97 lines
2.8 KiB
Python
97 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
数据迁移脚本 - 从旧的 cookie-receiver 迁移到新服务
|
|
"""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from datetime import datetime, timedelta
|
|
import shutil
|
|
|
|
OLD_DIR = Path('/home/hermes/projects/cookie-receiver/data')
|
|
NEW_DIR = Path('/home/hermes/projects/data-sync-service/data')
|
|
INDEX_FILE = NEW_DIR / 'index.json'
|
|
|
|
|
|
def migrate():
|
|
"""执行迁移"""
|
|
print("🔄 开始迁移数据...\n")
|
|
|
|
# 确保新目录存在
|
|
NEW_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
# 初始化索引
|
|
if INDEX_FILE.exists():
|
|
with open(INDEX_FILE, 'r', encoding='utf-8') as f:
|
|
index = json.load(f)
|
|
else:
|
|
index = {"version": "1.0.0", "tags": {}}
|
|
|
|
# 迁移通义听悟数据
|
|
old_file = OLD_DIR / 'tingwu_cookies.json'
|
|
if old_file.exists():
|
|
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')
|
|
tag = f"tingwu:{account}"
|
|
|
|
# 转换为新格式
|
|
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
expires_at = (datetime.now() + timedelta(days=7)).strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
new_data = {
|
|
'tag': tag,
|
|
'type': 'cookie',
|
|
'data': {
|
|
'cookies': old_data.get('cookies', []),
|
|
'timestamp': old_data.get('timestamp', now)
|
|
},
|
|
'metadata': {
|
|
'created_at': old_data.get('timestamp', now),
|
|
'expires_in': 7 * 24 * 3600,
|
|
'expires_at': expires_at,
|
|
'note': '从旧 cookie-receiver 迁移',
|
|
'login_url': 'https://tingwu.aliyun.com/'
|
|
}
|
|
}
|
|
|
|
# 保存新数据
|
|
new_filename = 'tingwu_ykaayk_qq_com.json'
|
|
new_filepath = NEW_DIR / new_filename
|
|
|
|
with open(new_filepath, 'w', encoding='utf-8') as f:
|
|
json.dump(new_data, f, indent=2, ensure_ascii=False)
|
|
|
|
print(f"✅ 已保存: {new_filepath}")
|
|
|
|
# 更新索引
|
|
index['tags'][tag] = {
|
|
'type': 'cookie',
|
|
'file': new_filename,
|
|
'created_at': now,
|
|
'expires_at': expires_at,
|
|
'last_accessed': now
|
|
}
|
|
|
|
print(f"✅ 已更新索引: {tag}")
|
|
|
|
else:
|
|
print(f"⚠️ 未找到旧数据文件: {old_file}")
|
|
|
|
# 保存索引
|
|
with open(INDEX_FILE, 'w', encoding='utf-8') as f:
|
|
json.dump(index, f, indent=2, ensure_ascii=False)
|
|
|
|
print(f"\n✅ 迁移完成!")
|
|
print(f" 新数据目录: {NEW_DIR}")
|
|
print(f" 索引文件: {INDEX_FILE}")
|
|
print(f" 共迁移: {len(index['tags'])} 个数据")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
migrate()
|