2024-08-13 17:49:51 -04:00
|
|
|
#!/usr/bin/env python3.12
|
|
|
|
|
|
|
|
import os
|
|
|
|
import aiosqlite as sqlite3
|
|
|
|
from fastapi import FastAPI
|
2025-02-11 20:01:07 -05:00
|
|
|
from typing import Optional, LiteralString
|
2025-02-11 11:19:52 -05:00
|
|
|
from .constructors import ValidShowEpisodeLineRequest, ValidShowEpisodeListRequest
|
2024-08-13 17:49:51 -04:00
|
|
|
|
|
|
|
class Transcriptions(FastAPI):
|
|
|
|
"""Transcription Endpoints"""
|
2025-02-14 16:07:24 -05:00
|
|
|
def __init__(self, app: FastAPI, util, constants) -> None: # pylint: disable=super-init-not-called
|
2024-08-13 17:49:51 -04:00
|
|
|
self.app = app
|
|
|
|
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,
|
2024-08-13 17:49:51 -04:00
|
|
|
#tbd
|
|
|
|
}
|
|
|
|
|
|
|
|
for endpoint, handler in self.endpoints.items():
|
2025-01-29 15:48:47 -05:00
|
|
|
app.add_api_route(f"/{endpoint}", handler, methods=["POST"],
|
|
|
|
include_in_schema=False)
|
2024-08-13 17:49:51 -04:00
|
|
|
|
2025-02-11 20:01:07 -05:00
|
|
|
async def get_episodes_handler(self, data: ValidShowEpisodeListRequest) -> dict:
|
|
|
|
"""Get list of episodes by show id"""
|
|
|
|
show_id: int = data.s
|
|
|
|
db_path: Optional[str|LiteralString] = None
|
|
|
|
db_query: Optional[str] = None
|
|
|
|
show_title: Optional[str] = None
|
2024-08-13 17:49:51 -04:00
|
|
|
|
|
|
|
if show_id is None:
|
|
|
|
return {
|
|
|
|
'err': True,
|
2025-02-14 16:07:24 -05:00
|
|
|
'errorText': 'Invalid request',
|
2024-08-13 17:49:51 -04:00
|
|
|
}
|
|
|
|
|
2025-02-14 16:07:24 -05:00
|
|
|
show_id = int(show_id)
|
2024-08-13 17:49:51 -04:00
|
|
|
|
2025-01-11 20:59:10 -05:00
|
|
|
if not(str(show_id).isnumeric()) or show_id not in [0, 1, 2]:
|
2024-08-13 17:49:51 -04:00
|
|
|
return {
|
|
|
|
'err': True,
|
2025-02-14 16:07:24 -05:00
|
|
|
'errorText': 'Show not found.',
|
2024-08-13 17:49:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
match show_id:
|
|
|
|
case 0:
|
2025-02-14 16:07:24 -05:00
|
|
|
db_path = os.path.join("/usr/local/share",
|
2025-01-24 19:26:07 -05:00
|
|
|
"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:
|
2025-02-14 16:07:24 -05:00
|
|
|
db_path = os.path.join("/usr/local/share",
|
2025-01-24 19:26:07 -05:00
|
|
|
"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:
|
2025-02-14 16:07:24 -05:00
|
|
|
db_path = os.path.join("/usr/local/share",
|
2025-01-24 19:26:07 -05:00
|
|
|
"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 {
|
|
|
|
'err': True,
|
2024-08-13 18:00:29 -04:00
|
|
|
'errorText': 'Unknown error.'
|
2024-08-13 17:49:51 -04:00
|
|
|
}
|
2025-02-14 16:07:24 -05:00
|
|
|
|
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()
|
2024-08-13 17:49:51 -04:00
|
|
|
return {
|
|
|
|
"show_title": show_title,
|
|
|
|
"episodes": [
|
|
|
|
{
|
|
|
|
'id': item[1],
|
|
|
|
'ep_friendly': item[0]
|
|
|
|
} for item in result]
|
|
|
|
}
|
|
|
|
|
2025-02-11 20:01:07 -05:00
|
|
|
async def get_episode_lines_handler(self, data: ValidShowEpisodeLineRequest) -> dict:
|
|
|
|
"""Get lines for a particular episode"""
|
|
|
|
show_id: int = data.s
|
|
|
|
episode_id: int = data.e
|
2025-01-11 20:59:10 -05:00
|
|
|
# pylint: disable=line-too-long
|
2024-08-13 18:00:29 -04:00
|
|
|
match show_id:
|
|
|
|
case 0:
|
2025-02-14 16:07:24 -05:00
|
|
|
db_path = os.path.join("/usr/local/share",
|
2025-01-24 19:26:07 -05:00
|
|
|
"sqlite_dbs", "sp.db")
|
2025-02-14 16:07:24 -05:00
|
|
|
db_query = """SELECT ("S" || Season || "E" || Episode || " " || Title), Character, Line FROM SP_DAT WHERE ID = ?"""
|
2024-08-13 18:00:29 -04:00
|
|
|
case 1:
|
2025-02-14 16:07:24 -05:00
|
|
|
db_path = os.path.join("/usr/local/share",
|
2025-01-24 19:26:07 -05:00
|
|
|
"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:
|
2025-02-14 16:07:24 -05:00
|
|
|
db_path = os.path.join("/usr/local/share",
|
2025-01-24 19:26:07 -05:00
|
|
|
"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 {
|
|
|
|
'err': True,
|
|
|
|
'errorText': 'Unknown error'
|
|
|
|
}
|
2024-08-13 19:21:48 -04:00
|
|
|
|
2024-08-13 18:00:29 -04:00
|
|
|
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]
|
2024-08-13 18:00:29 -04:00
|
|
|
return {
|
2024-12-02 11:04:24 -05:00
|
|
|
'episode_id': episode_id,
|
|
|
|
'ep_friendly': first_result[0].strip(),
|
|
|
|
'lines': [
|
2024-08-13 18:00:29 -04:00
|
|
|
{
|
|
|
|
'speaker': item[1].strip(),
|
|
|
|
'line': item[2].strip()
|
|
|
|
} for item in result]
|
|
|
|
}
|
|
|
|
|