-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
33 lines (27 loc) · 903 Bytes
/
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
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
import os
import random
app = FastAPI()
@app.get("/")
async def home():
return RedirectResponse("/docs")
@app.get("/random")
async def random_expansion(acr=None):
try:
if acr is not None:
selected_file = f'{acr.lower()}.txt'
filepath = os.path.join('expansions', selected_file)
else:
files = os.listdir('expansions')
selected_file = random.choice(files)
filepath = os.path.join('expansions', selected_file)
with open(filepath, 'r') as file:
lines = file.readlines()
selected_line = random.choice(lines)
return {
"acronym": selected_file.replace('.txt', '').upper(),
"expansion": selected_line.strip()
}
except Exception as e:
return {"error": str(e)}