api/endpoints/rand_msg.py

92 lines
4.4 KiB
Python
Raw Normal View History

2024-08-11 13:49:07 -04:00
import os
import random
2025-02-16 14:12:49 -05:00
from typing import LiteralString, Optional, Union
2025-01-11 20:59:10 -05:00
import aiosqlite as sqlite3
from fastapi import FastAPI
2025-02-15 21:09:33 -05:00
from fastapi.responses import JSONResponse
2025-02-11 11:19:52 -05:00
from .constructors import RandMsgRequest
2024-08-11 13:49:07 -04:00
class RandMsg(FastAPI):
2025-02-15 21:09:33 -05:00
"""
Random Message Endpoint
"""
def __init__(self, app: FastAPI,
2025-02-16 08:50:53 -05:00
util, constants) -> None:
2025-02-15 21:09:33 -05:00
self.app: FastAPI = app
2024-08-11 13:49:07 -04:00
self.util = util
self.constants = constants
self.endpoint_name = "randmsg"
app.add_api_route(f"/{self.endpoint_name}", self.randmsg_handler, methods=["POST"])
2024-08-11 13:49:07 -04:00
2025-02-16 14:12:49 -05:00
async def randmsg_handler(self, data: Optional[RandMsgRequest] = None) -> JSONResponse:
2024-08-11 13:49:07 -04:00
"""
Get a randomly generated message
2025-02-16 08:17:27 -05:00
- **short**: Optional, if True, will limit length of returned random messages to <=126 characters (Discord restriction related)
2024-08-11 13:49:07 -04:00
"""
random.seed()
2025-02-16 14:12:49 -05:00
short: Optional[bool] = False
if isinstance(data, RandMsgRequest):
short = data.short
2025-02-16 14:20:39 -05:00
if short:
db_rand_selected: int = 9
2025-02-16 14:12:49 -05:00
db_rand_selected = random.choice([0, 1, 3])
2025-02-15 21:09:33 -05:00
title_attr: str = "Unknown"
2024-08-11 13:49:07 -04:00
match db_rand_selected:
case 0:
2025-02-15 21:09:33 -05:00
randmsg_db_path: Union[str, LiteralString] = os.path.join("/usr/local/share",
2025-01-24 19:26:07 -05:00
"sqlite_dbs", "qajoke.db") # For qajoke db
2025-02-15 21:09:33 -05:00
db_query: str = "SELECT id, ('<b>Q:</b> ' || question || '<br/><b>A:</b> ' \
2024-08-11 13:55:11 -04:00
|| answer) FROM jokes ORDER BY RANDOM() LIMIT 1" # For qajoke db
2024-08-11 13:49:07 -04:00
title_attr = "QA Joke DB"
2024-11-14 14:37:32 -05:00
case 1 | 9:
2025-02-14 16:07:24 -05:00
randmsg_db_path = os.path.join("/usr/local/share",
2025-01-24 19:26:07 -05:00
"sqlite_dbs",
2024-08-11 13:49:07 -04:00
"randmsg.db") # For randmsg db
2024-08-11 13:55:11 -04:00
db_query = "SELECT id, msg FROM msgs WHERE \
LENGTH(msg) <= 180 ORDER BY RANDOM() LIMIT 1" # For randmsg db
2024-11-14 14:37:32 -05:00
if db_rand_selected == 9:
db_query = db_query.replace("<= 180", "<= 126")
2024-08-11 13:49:07 -04:00
title_attr = "Random Msg DB"
case 2:
2025-02-14 16:07:24 -05:00
randmsg_db_path = os.path.join("/usr/local/share",
2025-01-24 19:26:07 -05:00
"sqlite_dbs",
2024-08-11 13:49:07 -04:00
"trump.db") # For Trump Tweet DB
2024-08-11 13:55:11 -04:00
db_query = "SELECT id, content FROM tweets \
ORDER BY RANDOM() LIMIT 1" # For Trump Tweet DB
2024-08-11 13:49:07 -04:00
title_attr = "Trump Tweet DB"
case 3:
2025-02-14 16:07:24 -05:00
randmsg_db_path = os.path.join("/usr/local/share",
2025-01-24 19:26:07 -05:00
"sqlite_dbs",
2024-08-11 13:49:07 -04:00
"philo.db") # For Philo DB
2025-02-14 16:07:24 -05:00
db_query = "SELECT id, (content || '<br> - ' || speaker) FROM quotes \
2024-08-11 13:55:11 -04:00
ORDER BY RANDOM() LIMIT 1" # For Philo DB
2025-02-14 16:07:24 -05:00
title_attr = "Philosophical Quotes DB"
2024-08-11 13:49:07 -04:00
case 4:
2025-02-14 16:07:24 -05:00
randmsg_db_path = os.path.join("/usr/local/share",
2025-01-24 19:26:07 -05:00
"sqlite_dbs",
2024-08-11 13:49:07 -04:00
"hate.db") # For Hate DB
2025-02-14 16:07:24 -05:00
db_query = """SELECT id, ("<font color='#FF0000'>" || comment) FROM hate_speech \
2024-08-11 13:55:11 -04:00
WHERE length(comment) <= 180 ORDER BY RANDOM() LIMIT 1"""
2025-02-14 16:07:24 -05:00
title_attr = "Hate Speech DB"
2024-08-11 13:49:07 -04:00
case 5:
2025-02-14 16:07:24 -05:00
randmsg_db_path = os.path.join("/usr/local/share",
2025-01-24 19:26:07 -05:00
"sqlite_dbs",
2024-08-11 13:49:07 -04:00
"rjokes.db") # r/jokes DB
2025-02-14 16:07:24 -05:00
db_query = """SELECT id, (title || "<br>" || body) FROM jokes \
2024-08-11 13:55:11 -04:00
WHERE score >= 10000 ORDER BY RANDOM() LIMIT 1"""
2025-02-14 16:07:24 -05:00
title_attr = "r/jokes DB"
2024-08-11 13:49:07 -04:00
async with sqlite3.connect(database=randmsg_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-15 21:09:33 -05:00
result: sqlite3.Row = await _cursor.fetchone()
2024-08-11 13:49:07 -04:00
(result_id, result_msg) = result
result_msg = result_msg.strip()
2025-02-15 21:09:33 -05:00
return JSONResponse(content=
{
"id": result_id,
"msg": result_msg,
"title": title_attr,
})
2024-08-11 13:49:07 -04:00