RockCoder23 commited on
Commit
4bf0b6a
·
verified ·
1 Parent(s): a14f3d7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from typing import List, Any, Optional
4
+ import uvicorn
5
+ from fastapi import FastAPI, Header, HTTPException
6
+ from pydantic import BaseModel
7
+ import httpx
8
+
9
+ app = FastAPI(title="Roblox Data Migration Bridge")
10
+
11
+ # Secrets configured in HF Space Settings -> Repository secrets
12
+ OLD_UNIVERSE_ID = os.getenv("OLD_UNIVERSE_ID")
13
+ NEW_UNIVERSE_ID = os.getenv("NEW_UNIVERSE_ID")
14
+ OLD_API_KEY = os.getenv("OLD_API_KEY")
15
+ NEW_API_KEY = os.getenv("NEW_API_KEY")
16
+ SERVER_SECRET = os.getenv("SERVER_SECRET")
17
+
18
+ BASE_URL = "https://apis.roblox.com/datastores/v1/universes"
19
+
20
+ class ReadRequest(BaseModel):
21
+ user_id: int
22
+ datastore_names: List[str]
23
+ scope: Optional[str] = "global"
24
+
25
+ class TransferRequest(BaseModel):
26
+ user_id: int
27
+ datastore_names: List[str]
28
+ scope: Optional[str] = "global"
29
+
30
+ def verify_auth(auth_header: Optional[str]):
31
+ if not SERVER_SECRET:
32
+ return
33
+ if not auth_header or auth_header != f"Bearer {SERVER_SECRET}":
34
+ raise HTTPException(status_code=401, detail="Unauthorized request")
35
+
36
+ async def fetch_opencloud_entry(client: httpx.AsyncClient, universe_id: str, api_key: str, ds_name: str, key: str, scope: str) -> Any:
37
+ url = f"{BASE_URL}/{universe_id}/standard-datastores/datastore/entries/entry"
38
+ params = {"datastoreName": ds_name, "entryKey": key, "scope": scope}
39
+ headers = {"x-api-key": api_key}
40
+
41
+ res = await client.get(url, params=params, headers=headers)
42
+ if res.status_code == 200:
43
+ try:
44
+ return res.json()
45
+ except json.JSONDecodeError:
46
+ return res.text
47
+ elif res.status_code == 404:
48
+ return None
49
+ else:
50
+ raise HTTPException(status_code=res.status_code, detail=f"Open Cloud Read Error ({ds_name}): {res.text}")
51
+
52
+ async def write_opencloud_entry(client: httpx.AsyncClient, universe_id: str, api_key: str, ds_name: str, key: str, scope: str, data: Any) -> bool:
53
+ url = f"{BASE_URL}/{universe_id}/standard-datastores/datastore/entries/entry"
54
+ params = {"datastoreName": ds_name, "entryKey": key, "scope": scope}
55
+ headers = {
56
+ "x-api-key": api_key,
57
+ "content-type": "application/json"
58
+ }
59
+ payload_str = json.dumps(data)
60
+
61
+ res = await client.post(url, params=params, headers=headers, content=payload_str)
62
+ if res.status_code in (200, 204):
63
+ return True
64
+ raise HTTPException(status_code=res.status_code, detail=f"Open Cloud Write Error ({ds_name}): {res.text}")
65
+
66
+ @app.get("/")
67
+ def read_root():
68
+ return {"status": "online", "message": "Roblox Data Transfer API is running"}
69
+
70
+ @app.post("/read-old-datastores")
71
+ async def read_old_datastores(payload: ReadRequest, authorization: Optional[str] = Header(None)):
72
+ verify_auth(authorization)
73
+ key = str(payload.user_id)
74
+ results = {}
75
+
76
+ async with httpx.AsyncClient(timeout=10.0) as client:
77
+ for ds in payload.datastore_names:
78
+ data = await fetch_opencloud_entry(client, OLD_UNIVERSE_ID, OLD_API_KEY, ds, key, payload.scope)
79
+ results[ds] = data
80
+
81
+ return {"status": "success", "user_id": payload.user_id, "data": results}
82
+
83
+ @app.post("/transfer-datastores")
84
+ async def transfer_datastores(payload: TransferRequest, authorization: Optional[str] = Header(None)):
85
+ verify_auth(authorization)
86
+ key = str(payload.user_id)
87
+ transfer_results = {}
88
+
89
+ async with httpx.AsyncClient(timeout=15.0) as client:
90
+ for ds in payload.datastore_names:
91
+ old_data = await fetch_opencloud_entry(client, OLD_UNIVERSE_ID, OLD_API_KEY, ds, key, payload.scope)
92
+ if old_data is not None:
93
+ success = await write_opencloud_entry(client, NEW_UNIVERSE_ID, NEW_API_KEY, ds, key, payload.scope, old_data)
94
+ transfer_results[ds] = {"migrated": success, "data": old_data}
95
+ else:
96
+ transfer_results[ds] = {"migrated": False, "reason": "No old data found"}
97
+
98
+ return {"status": "success", "user_id": payload.user_id, "results": transfer_results}
99
+
100
+ if __name__ == "__main__":
101
+ uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)