| 
									
										
										
										
											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-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
										 |  |  | 
 | 
					
						
							|  |  |  | class Transcriptions(FastAPI): | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Transcription Endpoints | 
					
						
							|  |  |  |     """   
 | 
					
						
							| 
									
										
										
										
											2025-02-14 16:07:24 -05:00
										 |  |  |     def __init__(self, app: FastAPI, util, constants) -> None:  # pylint: disable=super-init-not-called | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											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"], | 
					
						
							| 
									
										
										
										
											2025-02-16 07:56:33 -05:00
										 |  |  |                               include_in_schema=True) | 
					
						
							| 
									
										
										
										
											2024-08-13 17:49:51 -04:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |     async def get_episodes_handler(self, data: ValidShowEpisodeListRequest) -> JSONResponse: | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         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 | 
					
						
							| 
									
										
										
										
											2024-08-13 17:49:51 -04:00
										 |  |  |                      | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |         if not show_id: | 
					
						
							|  |  |  |             return JSONResponse(status_code=500, content={ | 
					
						
							| 
									
										
										
										
											2024-08-13 17:49:51 -04:00
										 |  |  |                 'err': True, | 
					
						
							| 
									
										
										
										
											2025-02-14 16:07:24 -05:00
										 |  |  |                 'errorText': 'Invalid request', | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											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]: | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |             return JSONResponse(status_code=500, content={ | 
					
						
							| 
									
										
										
										
											2024-08-13 17:49:51 -04:00
										 |  |  |                 'err': True, | 
					
						
							| 
									
										
										
										
											2025-02-14 16:07:24 -05:00
										 |  |  |                 'errorText': 'Show not found.', | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											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 _: | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |                 return JSONResponse(status_code=500, content={ | 
					
						
							| 
									
										
										
										
											2024-08-13 17:49:51 -04:00
										 |  |  |                     'err': True, | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |                     'errorText': 'Unknown error.', | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |                 return JSONResponse(content={ | 
					
						
							| 
									
										
										
										
											2024-08-13 17:49:51 -04:00
										 |  |  |                     "show_title": show_title, | 
					
						
							|  |  |  |                     "episodes": [ | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                             'id': item[1], | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |                             'ep_friendly': item[0], | 
					
						
							|  |  |  |                             } for item in result], | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2024-08-13 17:49:51 -04:00
										 |  |  |              | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |     async def get_episode_lines_handler(self, data: ValidShowEpisodeLineRequest) -> JSONResponse: | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         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) | 
					
						
							| 
									
										
										
										
											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-15 21:09:33 -05:00
										 |  |  |                 db_path: Union[str, LiteralString] = os.path.join("/usr/local/share",                            | 
					
						
							| 
									
										
										
										
											2025-01-24 19:26:07 -05:00
										 |  |  |                                        "sqlite_dbs", "sp.db") | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |                 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: | 
					
						
							| 
									
										
										
										
											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 _: | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |                 return JSONResponse(status_code=500, content={ | 
					
						
							| 
									
										
										
										
											2024-08-13 18:00:29 -04:00
										 |  |  |                     'err': True, | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |                     'errorText': 'Unknown error', | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2024-08-13 19:21:48 -04:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05: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] | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |                 return JSONResponse(content={ | 
					
						
							| 
									
										
										
										
											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(), | 
					
						
							| 
									
										
										
										
											2025-02-15 21:09:33 -05:00
										 |  |  |                             'line': item[2].strip(), | 
					
						
							|  |  |  |                             } for item in result], | 
					
						
							| 
									
										
										
										
											2025-02-16 08:17:27 -05:00
										 |  |  |                 }) |