Update subtitle processing

This commit is contained in:
2025-05-03 12:40:30 +02:00
parent 5da123ad57
commit 727c663d33

52
main.py
View File

@@ -10,7 +10,7 @@ import asyncio
# Some useful variables
PEEHAITCHPEA_ENDPOINT = "http://localhost:9000/api.php?cmd=getselectedmessage"
PYTHAGORAS_PORT = 9000
PYTHAGORAS_PORT = 8000
# Configure logging
logging.basicConfig(
@@ -92,27 +92,15 @@ async def control_endpoint(request: Request):
content={"status": "error", "message": f"Failed to process request."}
)
@app.post("/subtitles")
async def subtitles_endpoint(request: Request):
"""Endpoint for subtitle data."""
try:
text_content = await request.body()
subtitle_text = text_content.decode("utf-8")
logger.info(f"Received subtitle text: {subtitle_text}")
if manager.active_connections:
await manager.broadcast(json.dumps({"type": "subtitle", "text": subtitle_text}))
return JSONResponse(
status_code=200,
content={"status": "success", "message": "Subtitle text received"}
)
except Exception as e:
logger.error(f"Error processing subtitle data: {str(e)}")
return JSONResponse(
status_code=400,
content={"status": "error", "message": f"Failed to process request."}
)
@app.post("/subtitles/update_current")
async def subtitles_update_current_endpoint(request: Request):
"""Endpoint for subtitle data - updating the current sentence as it comes."""
return await process_subtitles(request, "update_current")
@app.post("/subtitles/submit_sentence")
async def subtitles_submit_sentence_endpoint(request: Request):
"""Endpoint for subtitle data - submitting the final version of a sentence."""
return await process_subtitles(request, "submit_sentence")
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
@@ -131,6 +119,26 @@ async def websocket_endpoint(websocket: WebSocket):
# Functions
async def process_subtitles(request: Request, sub_type: str):
try:
text_content = await request.body()
subtitle_text = text_content.decode("utf-8")
logger.info(f"Received subtitle text: {subtitle_text}, request type: {sub_type}")
if manager.active_connections:
await manager.broadcast(json.dumps({"type": f"subtitle_{sub_type}", "text": subtitle_text}))
return JSONResponse(
status_code=200,
content={"status": "success", "message": "Subtitle text received"}
)
except Exception as e:
logger.error(f"Error processing {sub_type} subtitle data: {str(e)}")
return JSONResponse(
status_code=400,
content={"status": "error", "message": f"Failed to process request."}
)
async def fetch_selected_message():
"""
Fetches a selected message from the specified endpoint.