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