-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (72 loc) · 3.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from typing import Union
from fastapi import FastAPI, Query, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from insult import *
tags_metadata = [
{
"name": "nederen",
"description": "request a Danish insult",
},
{
"name": "historisk",
"description": "get insult history for an id"
}
]
description = """
## IaaS (Insults as a Service)
This API insults a subject of your choice (default=you) in Danish
<br>
<br>
<br>
The amazing [Web UI](https://edderma.me) uses the recommended setting unique=true.
With the amazing API you get to decide yourself and if you do not enable nolog,
you can always query the history of your insults.
You can also query past insults generated by the Web UI - just save the uuid if you run into a particularly nice one. Or a particularly mean one.
Please do [contribute](https://github.com/RadicalPet/danish_insulter) your favorite Danish insults and swearwords, as well as any feature you might be missing.
"""
app = FastAPI(
title="Danish Insulter",
description=description,
version="0.0.1",
openapi_tags=tags_metadata
)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse, tags=["default"])
def read_root(request: Request, insult:bool = False, id:Union[str, None] = None, subject:Union[str, None] = None, unique:bool = False, alliteration:bool = False):
insult_result=""
if insult:
if id == "None":
id = None
if subject=="me":
subject=None
insult = Insult(id=id, subject=subject, unique=True, alliteration=alliteration)
try:
error, id, insult = insult.get_insult()
except ListExhaustedException as e:
insult = Insult(id=None, subject=subject, unique=unique, alliteration=alliteration)
error, id, insult = insult.get_insult()
insult_result = insult
return templates.TemplateResponse("insult.html", {"request": request, "id": id, "subject": subject, "unique": unique, "insult_result": insult_result })
@app.get("/nederen", tags=["nederen"])
def read_nederen(
id: Union[str, None] = Query(None, description="uuid to log the insult to - a uuid gets generated with every request without this parameter", ),
subject: Union[str, None] = Query(None, description='add subject for the insult (han/hen/hun/min chef etc.) - default is "du"', ),
unique: bool = Query(False, description='keep insult values unique to uuid until shortest list exhausted', ),
alliteration: bool = Query(False, description='will us alliterations where possible', ),
nolog: bool = Query(False, description='do not log insult - nolog false required by unique', ),
):
insult = Insult(id, subject, unique, alliteration, nolog)
try:
error, id, insult = insult.get_insult()
except ListExhaustedException as e:
return {"error": e, "id": id, "insult": None}
return {"error": error, "id": id, "insult": insult}
@app.get("/historisk/{id}", tags=["historisk"])
def read_item(id):
print(id)
insult = Insult(id=id)
all_insults = insult.get_all_logged_insults()
return {"insults": all_insults}