mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-05-06 20:02:51 +08:00
refactor(routes): #13 优化 Sub2API 导出格式为合并数组
This commit is contained in:
@@ -412,16 +412,11 @@ async def export_accounts_csv(request: BatchExportRequest):
|
||||
|
||||
@router.post("/export/sub2api")
|
||||
async def export_accounts_sub2api(request: BatchExportRequest):
|
||||
"""导出账号为 Sub2Api 格式(每个账号单独一个 JSON 文件,多个打包为 ZIP)"""
|
||||
import io
|
||||
import zipfile
|
||||
"""导出账号为 Sub2Api 格式(所有选中账号合并到一个 JSON 的 accounts 数组中)"""
|
||||
|
||||
def make_sub2api_json(acc) -> dict:
|
||||
def make_account_entry(acc) -> dict:
|
||||
expires_at = int(acc.expires_at.timestamp()) if acc.expires_at else 0
|
||||
return {
|
||||
"proxies": [],
|
||||
"accounts": [
|
||||
{
|
||||
"name": acc.email,
|
||||
"platform": "openai",
|
||||
"type": "oauth",
|
||||
@@ -438,7 +433,10 @@ async def export_accounts_sub2api(request: BatchExportRequest):
|
||||
"gpt-5.1-codex-max": "gpt-5.1-codex-max",
|
||||
"gpt-5.1-codex-mini": "gpt-5.1-codex-mini",
|
||||
"gpt-5.2": "gpt-5.2",
|
||||
"gpt-5.2-codex": "gpt-5.2-codex"
|
||||
"gpt-5.2-codex": "gpt-5.2-codex",
|
||||
"gpt-5.3": "gpt-5.3",
|
||||
"gpt-5.3-codex": "gpt-5.3-codex",
|
||||
"gpt-5.4": "gpt-5.4"
|
||||
},
|
||||
"organization_id": acc.workspace_id or "",
|
||||
"refresh_token": acc.refresh_token or ""
|
||||
@@ -449,8 +447,6 @@ async def export_accounts_sub2api(request: BatchExportRequest):
|
||||
"rate_multiplier": 1,
|
||||
"auto_pause_on_expired": True
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
with get_db() as db:
|
||||
ids = resolve_account_ids(
|
||||
@@ -460,31 +456,23 @@ async def export_accounts_sub2api(request: BatchExportRequest):
|
||||
accounts = db.query(Account).filter(Account.id.in_(ids)).all()
|
||||
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
payload = {
|
||||
"proxies": [],
|
||||
"accounts": [make_account_entry(acc) for acc in accounts]
|
||||
}
|
||||
content = json.dumps(payload, ensure_ascii=False, indent=2)
|
||||
|
||||
if len(accounts) == 1:
|
||||
acc = accounts[0]
|
||||
content = json.dumps(make_sub2api_json(acc), ensure_ascii=False, indent=2)
|
||||
filename = f"{acc.email}_sub2api.json"
|
||||
filename = f"{accounts[0].email}_sub2api.json"
|
||||
else:
|
||||
filename = f"sub2api_tokens_{timestamp}.json"
|
||||
|
||||
return StreamingResponse(
|
||||
iter([content]),
|
||||
media_type="application/json",
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
||||
)
|
||||
|
||||
zip_buffer = io.BytesIO()
|
||||
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
for acc in accounts:
|
||||
content = json.dumps(make_sub2api_json(acc), ensure_ascii=False, indent=2)
|
||||
zf.writestr(f"{acc.email}_sub2api.json", content)
|
||||
|
||||
zip_buffer.seek(0)
|
||||
zip_filename = f"sub2api_tokens_{timestamp}.zip"
|
||||
return StreamingResponse(
|
||||
zip_buffer,
|
||||
media_type="application/zip",
|
||||
headers={"Content-Disposition": f"attachment; filename={zip_filename}"}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/export/cpa")
|
||||
async def export_accounts_cpa(request: BatchExportRequest):
|
||||
|
||||
Reference in New Issue
Block a user