Compare commits

...

2 Commits

Author SHA1 Message Date
559673e5ad Update README with info about the two new subtitle endpoints. 2025-05-03 12:41:08 +02:00
727c663d33 Update subtitle processing 2025-05-03 12:40:30 +02:00
2 changed files with 40 additions and 24 deletions

View File

@@ -42,10 +42,18 @@ To connect to the WebSocket for receiving subtitles and messages, simply use the
websocat ws://localhost:8000/ws
`
To push new subtitles onto the server, use the `/subtitles` endpoint, for example like this:
To push new subtitles onto the server, use the `/subtitles` endpoints. There are currently two different ones, and each produces different response type. Here are some examples:
- The first is `/subtitles/update_current`, which serves the purpose of updating the current sentence as it comes. Here is an example:
`
curl -X POST http://localhost:8000/subtitles -H "Content-Type: text/plain" -d 'I love pushing subtitles to servers!'
curl -X POST http://localhost:8000/subtitles/update_current -H "Content-Type: text/plain" -d 'I love pushing '
`
- The second is `/subtitles/submit_sentence`, which is used to submit the next finished sentence. Here is an example:
`
curl -X POST http://localhost:8000/subtitles/submit_sentence -H "Content-Type: text/plain" -d 'I love pushing sentences to servers!'
`
To control the server using commands, use the `/control` endpoint, for example like this:

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.