File size: 1,249 Bytes
945b70e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

from fastapi import APIRouter,WebSocket,WebSocketDisconnect
from fastapi.responses import FileResponse
import requests
import json


router = APIRouter(
    prefix="/watermelon",
    tags=["watermelon"],
)
game = "watermelon"
header = {'accept': 'application/json', 'Content-Type': 'application/json'}
url_base = "https://pgsoft-ai-patcher.hf.space/watermelon"


@router.get("/")
def index():
    return FileResponse(path="static/watermelon/index.html", media_type="text/html")


@router.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        try:
            data = await websocket.receive_json()
        except WebSocketDisconnect:
            continue
        print(f"received data: {data}")
        url = f"{url_base}/{data['url']}"
        myobj = data["data"]
        try:
            res = requests.post(url, headers=header, json=myobj)
            print(res.status_code)
            await websocket.send_text(json.dumps(json.loads(res.text),indent=4))
        except Exception as e:
            res = {
                "result": "Failure",
                "detail": f"failed to request, {e}",
            }
            await websocket.send_text(json.dumps(res, indent=4))