karma
This commit is contained in:
@ -2,10 +2,19 @@
|
||||
|
||||
import os
|
||||
import random
|
||||
import traceback
|
||||
import aiosqlite as sqlite3
|
||||
|
||||
from fastapi import FastAPI
|
||||
from typing import Optional
|
||||
from fastapi import FastAPI, Request
|
||||
from pydantic import BaseModel
|
||||
|
||||
class RandMsgRequest(BaseModel):
|
||||
"""
|
||||
- **short**: Short randmsg?
|
||||
"""
|
||||
|
||||
short: Optional[bool] = False
|
||||
|
||||
class RandMsg(FastAPI):
|
||||
"""Random Message Endpoint"""
|
||||
@ -19,12 +28,16 @@ class RandMsg(FastAPI):
|
||||
|
||||
app.add_api_route(f"/{self.endpoint_name}/", self.randmsg_handler, methods=["POST"])
|
||||
|
||||
async def randmsg_handler(self):
|
||||
async def randmsg_handler(self, data: RandMsgRequest = None):
|
||||
"""
|
||||
Get a randomly generated message
|
||||
"""
|
||||
random.seed()
|
||||
db_rand_selected = random.choice([0, 1, 3])
|
||||
short = data.short if data else False
|
||||
if not short:
|
||||
db_rand_selected = random.choice([0, 1, 3])
|
||||
else:
|
||||
db_rand_selected = 9
|
||||
title_attr = "Unknown"
|
||||
|
||||
match db_rand_selected:
|
||||
@ -37,7 +50,7 @@ class RandMsg(FastAPI):
|
||||
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:
|
||||
case 1 | 9:
|
||||
randmsg_db_path = os.path.join("/",
|
||||
"var",
|
||||
"lib",
|
||||
@ -45,6 +58,8 @@ class RandMsg(FastAPI):
|
||||
"randmsg.db") # For randmsg db
|
||||
db_query = "SELECT id, msg FROM msgs WHERE \
|
||||
LENGTH(msg) <= 180 ORDER BY RANDOM() LIMIT 1" # For randmsg db
|
||||
if db_rand_selected == 9:
|
||||
db_query = db_query.replace("<= 180", "<= 126")
|
||||
title_attr = "Random Msg DB"
|
||||
case 2:
|
||||
randmsg_db_path = os.path.join("/",
|
||||
|
Reference in New Issue
Block a user