api/endpoints/transcriptions.py

153 lines
5.8 KiB
Python
Raw Normal View History

2024-08-13 17:49:51 -04:00
import os
import aiosqlite as sqlite3
from fastapi import FastAPI
2025-02-15 21:09:33 -05:00
from fastapi.responses import JSONResponse
from typing import Optional, LiteralString, Union
2025-02-11 11:19:52 -05:00
from .constructors import ValidShowEpisodeLineRequest, ValidShowEpisodeListRequest
2024-08-13 17:49:51 -04:00
2024-08-13 17:49:51 -04:00
class Transcriptions(FastAPI):
2025-02-15 21:09:33 -05:00
"""
Transcription Endpoints
"""
2025-02-16 08:50:53 -05:00
def __init__(self, app: FastAPI, util, constants) -> None:
2025-02-15 21:09:33 -05:00
self.app: FastAPI = app
2024-08-13 17:49:51 -04:00
self.util = util
self.constants = constants
2025-02-11 20:01:07 -05:00
self.endpoints: dict = {
2024-08-13 17:51:35 -04:00
"transcriptions/get_episodes": self.get_episodes_handler,
"transcriptions/get_episode_lines": self.get_episode_lines_handler,
# tbd
}
2024-08-13 17:49:51 -04:00
for endpoint, handler in self.endpoints.items():
app.add_api_route(
f"/{endpoint}", handler, methods=["POST"], include_in_schema=True
)
async def get_episodes_handler(
self, data: ValidShowEpisodeListRequest
) -> JSONResponse:
2025-02-15 21:09:33 -05:00
"""
Get list of episodes by show id
2025-02-16 08:17:27 -05:00
- **s**: Show ID to query
2025-02-15 21:09:33 -05:00
"""
2025-02-11 20:01:07 -05:00
show_id: int = data.s
2025-02-15 21:09:33 -05:00
db_path: Optional[Union[str, LiteralString]] = None
2025-02-11 20:01:07 -05:00
db_query: Optional[str] = None
show_title: Optional[str] = None
2025-02-26 20:47:29 -05:00
if not isinstance(show_id, int):
return JSONResponse(
status_code=500,
content={
"err": True,
"errorText": "Invalid request",
},
)
2025-02-14 16:07:24 -05:00
show_id = int(show_id)
if not (str(show_id).isnumeric()) or show_id not in [0, 1, 2]:
return JSONResponse(
status_code=500,
content={
"err": True,
"errorText": "Show not found.",
},
)
2024-08-13 17:49:51 -04:00
match show_id:
case 0:
db_path = os.path.join("/usr/local/share", "sqlite_dbs", "sp.db")
2025-02-14 16:07:24 -05:00
db_query = """SELECT DISTINCT(("S" || Season || "E" || Episode || " " || Title)), ID FROM SP_DAT ORDER BY Season, Episode"""
show_title = "South Park"
2024-08-13 17:49:51 -04:00
case 1:
db_path = os.path.join("/usr/local/share", "sqlite_dbs", "futur.db")
2025-02-14 16:07:24 -05:00
db_query = """SELECT DISTINCT(("S" || EP_S || "E" || EP_EP || " " || EP_TITLE)), EP_ID FROM clean_dialog ORDER BY EP_S, EP_EP"""
show_title = "Futurama"
2024-08-13 17:49:51 -04:00
case 2:
db_path = os.path.join("/usr/local/share", "sqlite_dbs", "parks.db")
2025-02-14 16:07:24 -05:00
db_query = """SELECT DISTINCT(("S" || EP_S || "E" || EP_EP || " " || EP_TITLE)), EP_ID FROM clean_dialog ORDER BY EP_S, EP_EP"""
show_title = "Parks And Rec"
2024-08-13 17:49:51 -04:00
case _:
return JSONResponse(
status_code=500,
content={
"err": True,
"errorText": "Unknown error.",
},
)
2024-08-13 17:49:51 -04:00
async with sqlite3.connect(database=db_path, timeout=1) as _db:
2025-01-23 13:02:03 -05:00
async with await _db.execute(db_query) as _cursor:
2025-02-11 20:01:07 -05:00
result: list[tuple] = await _cursor.fetchall()
return JSONResponse(
content={
"show_title": show_title,
"episodes": [
{
"id": item[1],
"ep_friendly": item[0],
}
for item in result
],
}
)
async def get_episode_lines_handler(
self, data: ValidShowEpisodeLineRequest
) -> JSONResponse:
2025-02-15 21:09:33 -05:00
"""
Get lines for a particular episode
2025-02-16 08:17:27 -05:00
- **s**: Show ID to query
- **e**: Episode ID to query
2025-02-15 21:09:33 -05:00
"""
show_id: int = int(data.s)
episode_id: int = int(data.e)
2024-08-13 18:00:29 -04:00
match show_id:
case 0:
db_path: Union[str, LiteralString] = os.path.join(
"/usr/local/share", "sqlite_dbs", "sp.db"
)
db_query: str = (
"""SELECT ("S" || Season || "E" || Episode || " " || Title), Character, Line FROM SP_DAT WHERE ID = ?"""
)
2024-08-13 18:00:29 -04:00
case 1:
db_path = os.path.join("/usr/local/share", "sqlite_dbs", "futur.db")
2025-02-14 16:07:24 -05:00
db_query = """SELECT ("S" || EP_S || "E" || EP_EP || " " || EP_TITLE || "<br><em>Opener: " || EP_OPENER || "</em>"), EP_LINE_SPEAKER, EP_LINE FROM clean_dialog WHERE EP_ID = ? ORDER BY LINE_ID ASC"""
2024-08-13 18:00:29 -04:00
case 2:
db_path = os.path.join("/usr/local/share", "sqlite_dbs", "parks.db")
2025-02-14 16:07:24 -05:00
db_query = """SELECT ("S" || EP_S || "E" || EP_EP || " " || EP_TITLE), EP_LINE_SPEAKER, EP_LINE FROM clean_dialog WHERE EP_ID = ? ORDER BY id ASC"""
2024-08-13 18:00:29 -04:00
case _:
return JSONResponse(
status_code=500,
content={
"err": True,
"errorText": "Unknown error",
},
)
async with sqlite3.connect(database=db_path, timeout=1) as _db:
2025-02-11 20:01:07 -05:00
params: tuple = (episode_id,)
2025-01-23 13:02:03 -05:00
async with await _db.execute(db_query, params) as _cursor:
2025-02-11 20:01:07 -05:00
result: list[tuple] = await _cursor.fetchall()
first_result: tuple = result[0]
return JSONResponse(
content={
"episode_id": episode_id,
"ep_friendly": first_result[0].strip(),
"lines": [
{
"speaker": item[1].strip(),
"line": item[2].strip(),
}
for item in result
],
}
)