api/endpoints/transcriptions.py

141 lines
5.5 KiB
Python
Raw Normal View History

2024-08-13 17:49:51 -04:00
#!/usr/bin/env python3.12
import os
import aiosqlite as sqlite3
from fastapi import FastAPI
from pydantic import BaseModel
class ValidShowEpisodeListRequest(BaseModel):
"""
- **s**: show id
"""
s: int
class ValidShowEpisodeLineRequest(BaseModel):
"""
- **s**: show id
- **e**: episode id
"""
2024-08-13 18:00:29 -04:00
s: int
e: int
2024-08-13 17:49:51 -04:00
class Transcriptions(FastAPI):
"""Transcription Endpoints"""
2024-08-13 19:21:48 -04:00
def __init__(self, app: FastAPI, util, constants, glob_state): # pylint: disable=super-init-not-called
2024-08-13 17:49:51 -04:00
self.app = app
self.util = util
self.constants = constants
2024-08-13 19:21:48 -04:00
self.glob_state = glob_state
2024-08-13 17:49:51 -04:00
self.endpoints = {
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():
app.add_api_route(f"/{endpoint}/", handler, methods=["POST"])
async def get_episodes_handler(self, data: ValidShowEpisodeListRequest):
"""
2024-08-13 17:51:35 -04:00
/transcriptions/get_episodes/
2024-08-13 17:49:51 -04:00
Get list of episodes by show id
"""
show_id = data.s
db_path = None
db_query = None
show_title = None
if show_id is None:
return {
'err': True,
'errorText': 'Invalid request'
}
show_id = int(show_id)
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,
'errorText': 'Show not found.'
}
match show_id:
case 0:
2025-01-24 19:26:07 -05:00
db_path = os.path.join("/", "usr", "local", "share",
"sqlite_dbs", "sp.db")
2024-08-13 17:49:51 -04:00
db_query = """SELECT DISTINCT(("S" || Season || "E" || Episode || " " || Title)), ID FROM SP_DAT ORDER BY Season, Episode"""
show_title = "South Park"
case 1:
2025-01-24 19:26:07 -05:00
db_path = os.path.join("/", "usr", "local", "share",
"sqlite_dbs", "futur.db")
2024-08-13 17:49:51 -04: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"
case 2:
2025-01-24 19:26:07 -05:00
db_path = os.path.join("/", "usr", "local", "share",
"sqlite_dbs", "parks.db")
2024-08-13 17:49:51 -04: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"
case _:
return {
'err': True,
2024-08-13 18:00:29 -04:00
'errorText': 'Unknown error.'
2024-08-13 17:49:51 -04:00
}
2024-08-13 19:21:48 -04:00
await self.glob_state.increment_counter('transcript_list_requests')
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:
2024-08-13 17:49:51 -04:00
result = await _cursor.fetchall()
return {
"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):
2024-08-13 17:51:35 -04:00
"""/transcriptions/get_episode_lines/
2024-08-13 17:49:51 -04:00
Get lines for a particular episode
"""
2024-08-13 18:00:29 -04:00
show_id = data.s
episode_id = 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-01-24 19:26:07 -05:00
db_path = os.path.join("/", "usr", "local", "share",
"sqlite_dbs", "sp.db")
2024-08-13 18:00:29 -04:00
db_query = """SELECT ("S" || Season || "E" || Episode || " " || Title), Character, Line FROM SP_DAT WHERE ID = ?"""
case 1:
2025-01-24 19:26:07 -05:00
db_path = os.path.join("/", "usr", "local", "share",
"sqlite_dbs", "futur.db")
2024-08-13 18:00:29 -04: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"""
case 2:
2025-01-24 19:26:07 -05:00
db_path = os.path.join("/", "usr", "local", "share",
"sqlite_dbs", "parks.db")
2024-08-13 18:00:29 -04: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"""
case _:
return {
'err': True,
'errorText': 'Unknown error'
}
2024-08-13 19:21:48 -04:00
await self.glob_state.increment_counter('transcript_requests')
2024-08-13 18:00:29 -04:00
async with sqlite3.connect(database=db_path, timeout=1) as _db:
params = (episode_id,)
2025-01-23 13:02:03 -05:00
async with await _db.execute(db_query, params) as _cursor:
2024-08-13 18:00:29 -04:00
result = await _cursor.fetchall()
2024-12-02 11:04:24 -05:00
first_result = 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]
}