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 # Some useful variables
PEEHAITCHPEA_ENDPOINT = "http://localhost:9000/api.php?cmd=getselectedmessage" PEEHAITCHPEA_ENDPOINT = "http://localhost:9000/api.php?cmd=getselectedmessage"
PYTHAGORAS_PORT = 9000 PYTHAGORAS_PORT = 8000
# Configure logging # Configure logging
logging.basicConfig( logging.basicConfig(
@@ -92,27 +92,15 @@ async def control_endpoint(request: Request):
content={"status": "error", "message": f"Failed to process request."} content={"status": "error", "message": f"Failed to process request."}
) )
@app.post("/subtitles") @app.post("/subtitles/update_current")
async def subtitles_endpoint(request: Request): async def subtitles_update_current_endpoint(request: Request):
"""Endpoint for subtitle data.""" """Endpoint for subtitle data - updating the current sentence as it comes."""
try: return await process_subtitles(request, "update_current")
text_content = await request.body()
subtitle_text = text_content.decode("utf-8") @app.post("/subtitles/submit_sentence")
logger.info(f"Received subtitle text: {subtitle_text}") async def subtitles_submit_sentence_endpoint(request: Request):
"""Endpoint for subtitle data - submitting the final version of a sentence."""
if manager.active_connections: return await process_subtitles(request, "submit_sentence")
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.websocket("/ws") @app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket): async def websocket_endpoint(websocket: WebSocket):
@@ -131,6 +119,26 @@ async def websocket_endpoint(websocket: WebSocket):
# Functions # 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(): async def fetch_selected_message():
""" """
Fetches a selected message from the specified endpoint. Fetches a selected message from the specified endpoint.