Files
2026-01-27 17:40:37 +01:00

36 lines
1.1 KiB
Python

from fastapi import APIRouter, UploadFile, File, HTTPException, status, Depends
from typing import Any
from app.services.script_parser import parser_service
from app.schemas.script import ScriptAnalysisResponse
router = APIRouter()
@router.post("/parse", response_model=ScriptAnalysisResponse)
async def parse_script(
file: UploadFile = File(...)
) -> Any:
if not file.content_type in ["text/plain", "text/markdown", "application/octet-stream"]:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Only text files are supported for now."
)
content = await file.read()
try:
text_content = content.decode("utf-8")
except UnicodeDecodeError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="File must be UTF-8 encoded text."
)
try:
result = await parser_service.parse_script(text_content)
return result
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error parsing script: {str(e)}"
)