api/endpoints/rand_msg.py

105 lines
4.8 KiB
Python
Raw Normal View History

2024-08-11 13:49:07 -04:00
#!/usr/bin/env python3.12
import os
import random
import aiosqlite as sqlite3
from fastapi import FastAPI
class RandMsg(FastAPI):
"""Random Message Endpoint"""
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
self.app = app
self.util = util
self.constants = constants
self.endpoint_name = "randmsg"
2024-08-11 13:58:39 -04:00
app.add_api_route(f"/{self.endpoint_name}/", self.randmsg_handler, methods=["POST"])
2024-08-11 13:49:07 -04:00
async def randmsg_handler(self):
"""
Get a randomly generated message
"""
random.seed()
db_rand_selected = random.choice([0, 1, 3])
title_attr = "Unknown"
match db_rand_selected:
case 0:
randmsg_db_path = os.path.join("/",
"var",
"lib",
"singerdbs",
"qajoke.db") # For qajoke db
2024-08-11 13:55:11 -04:00
db_query = "SELECT id, ('<b>Q:</b> ' || question || '<br/><b>A:</b> ' \
|| answer) FROM jokes ORDER BY RANDOM() LIMIT 1" # For qajoke db
2024-08-11 13:49:07 -04:00
title_attr = "QA Joke DB"
case 1:
randmsg_db_path = os.path.join("/",
"var",
"lib",
"singerdbs",
"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-08-11 13:49:07 -04:00
title_attr = "Random Msg DB"
case 2:
randmsg_db_path = os.path.join("/",
"var",
"lib",
"singerdbs",
"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:
randmsg_db_path = os.path.join("/",
"var",
"lib",
"singerdbs",
"philo.db") # For Philo DB
2024-08-11 13:55:11 -04:00
db_query = "SELECT id, (content || '<br> - ' || speaker) FROM quotes \
ORDER BY RANDOM() LIMIT 1" # For Philo DB
2024-08-11 13:49:07 -04:00
title_attr = "Philosophical Quotes DB"
case 4:
randmsg_db_path = os.path.join("/",
"var",
"lib",
"singerdbs",
"hate.db") # For Hate DB
2024-08-11 13:55:11 -04:00
db_query = """SELECT id, ("<font color='#FF0000'>" || comment) FROM hate_speech \
WHERE length(comment) <= 180 ORDER BY RANDOM() LIMIT 1"""
2024-08-11 13:49:07 -04:00
title_attr = "Hate Speech DB"
case 5:
randmsg_db_path = os.path.join("/",
"var",
"lib",
"singerdbs",
"rjokes.db") # r/jokes DB
2024-08-11 13:55:11 -04:00
db_query = """SELECT id, (title || "<br>" || body) FROM jokes \
WHERE score >= 10000 ORDER BY RANDOM() LIMIT 1"""
2024-08-11 13:49:07 -04:00
title_attr = "r/jokes DB"
case 6:
randmsg_db_path = os.path.join("/",
"var",
"lib",
"singerdbs",
"donnies.db") # Donnies DB
random.seed()
twilight_or_mice = random.choice(["twilight", "mice"])
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 _db.execute(db_query) as _cursor:
result = await _cursor.fetchone()
(result_id, result_msg) = result
result_msg = result_msg.strip()
return {
"id": result_id,
"msg": result_msg,
'title': title_attr
}