This commit is contained in:
2025-02-14 16:07:24 -05:00
parent 00af36703a
commit 60416c493f
19 changed files with 204 additions and 308 deletions

View File

@ -2,29 +2,28 @@
import os
import random
from typing import LiteralString
from typing import LiteralString, Optional
import aiosqlite as sqlite3
from fastapi import FastAPI
from .constructors import RandMsgRequest
class RandMsg(FastAPI):
"""Random Message Endpoint"""
def __init__(self, app: FastAPI, util, constants, glob_state): # pylint: disable=super-init-not-called
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
self.app = app
self.util = util
self.constants = constants
self.glob_state = glob_state
self.endpoint_name = "randmsg"
app.add_api_route(f"/{self.endpoint_name}", self.randmsg_handler, methods=["POST"])
async def randmsg_handler(self, data: RandMsgRequest = None):
async def randmsg_handler(self, data: RandMsgRequest):
"""
Get a randomly generated message
"""
random.seed()
short: bool = data.short if data else False
short: bool = data.short if data.short else False
if not short:
db_rand_selected = random.choice([0, 1, 3])
else:
@ -33,15 +32,13 @@ class RandMsg(FastAPI):
match db_rand_selected:
case 0:
randmsg_db_path = os.path.join("/",
"usr", "local", "share",
randmsg_db_path = os.path.join("/usr/local/share",
"sqlite_dbs", "qajoke.db") # For qajoke db
db_query = "SELECT id, ('<b>Q:</b> ' || question || '<br/><b>A:</b> ' \
|| answer) FROM jokes ORDER BY RANDOM() LIMIT 1" # For qajoke db
title_attr = "QA Joke DB"
case 1 | 9:
randmsg_db_path = os.path.join("/",
"usr", "local", "share",
randmsg_db_path = os.path.join("/usr/local/share",
"sqlite_dbs",
"randmsg.db") # For randmsg db
db_query = "SELECT id, msg FROM msgs WHERE \
@ -50,46 +47,41 @@ class RandMsg(FastAPI):
db_query = db_query.replace("<= 180", "<= 126")
title_attr = "Random Msg DB"
case 2:
randmsg_db_path = os.path.join("/",
"usr", "local", "share",
randmsg_db_path = os.path.join("/usr/local/share",
"sqlite_dbs",
"trump.db") # For Trump Tweet DB
db_query = "SELECT id, content FROM tweets \
ORDER BY RANDOM() LIMIT 1" # For Trump Tweet DB
title_attr = "Trump Tweet DB"
case 3:
randmsg_db_path: str|LiteralString = os.path.join("/",
"usr", "local", "share",
randmsg_db_path = os.path.join("/usr/local/share",
"sqlite_dbs",
"philo.db") # For Philo DB
db_query: str = "SELECT id, (content || '<br> - ' || speaker) FROM quotes \
db_query = "SELECT id, (content || '<br> - ' || speaker) FROM quotes \
ORDER BY RANDOM() LIMIT 1" # For Philo DB
title_attr: str = "Philosophical Quotes DB"
title_attr = "Philosophical Quotes DB"
case 4:
randmsg_db_path: str|LiteralString = os.path.join("/",
"usr", "local", "share",
randmsg_db_path = os.path.join("/usr/local/share",
"sqlite_dbs",
"hate.db") # For Hate DB
db_query: str = """SELECT id, ("<font color='#FF0000'>" || comment) FROM hate_speech \
db_query = """SELECT id, ("<font color='#FF0000'>" || comment) FROM hate_speech \
WHERE length(comment) <= 180 ORDER BY RANDOM() LIMIT 1"""
title_attr: str = "Hate Speech DB"
title_attr = "Hate Speech DB"
case 5:
randmsg_db_path: str|LiteralString = os.path.join("/",
"usr", "local", "share",
randmsg_db_path = os.path.join("/usr/local/share",
"sqlite_dbs",
"rjokes.db") # r/jokes DB
db_query: str = """SELECT id, (title || "<br>" || body) FROM jokes \
db_query = """SELECT id, (title || "<br>" || body) FROM jokes \
WHERE score >= 10000 ORDER BY RANDOM() LIMIT 1"""
title_attr: str = "r/jokes DB"
title_attr = "r/jokes DB"
case 6:
randmsg_db_path: str|LiteralString = os.path.join("/",
"usr", "local", "share",
randmsg_db_path = os.path.join("/usr/local/share",
"sqlite_dbs",
"donnies.db") # Donnies DB
random.seed()
twilight_or_mice: str = random.choice(["twilight", "mice"])
db_query: str = f"SELECT id, text FROM {twilight_or_mice} ORDER BY RANDOM() LIMIT 1"
title_attr: str = "Donnies DB"
db_query = f"SELECT id, text FROM {twilight_or_mice} ORDER BY RANDOM() LIMIT 1"
title_attr = "Donnies DB"
async with sqlite3.connect(database=randmsg_db_path, timeout=1) as _db:
async with await _db.execute(db_query) as _cursor: