doordesk-js/dennis/src/dennis.py

72 lines
1.8 KiB
Python
Raw Normal View History

import os
import markdown
2023-04-11 00:36:07 -04:00
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from datetime import date, datetime
STATIC_PATH = "./static"
2023-04-11 00:36:07 -04:00
api = FastAPI()
api.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
2023-06-17 00:21:50 -04:00
allow_methods=["GET"],
allow_headers=[],
2023-04-11 00:36:07 -04:00
)
2023-06-16 22:46:29 -04:00
class Article(BaseModel):
content_type: str
title: str
date: date
content: str
2023-06-27 18:01:14 -04:00
def get_articles_from_path(path: str) -> list[Article]:
md = markdown.Markdown(extensions=["meta"]);
articles: list[Article] = []
2023-06-27 18:01:14 -04:00
for root, _, filenames in os.walk(path):
for filename in filenames:
if filename[-2:] == "md":
with open(f"{root}/{filename}") as file:
html: str = md.convert(file.read());
meta: dict = md.Meta;
articles.append(
Article(
content_type=meta.get("content_type")[0],
title=meta.get("title")[0],
date=datetime.strptime(meta.get("date")[0], "%Y %m %d"),
content=html,
));
return articles;
DB = sorted(
2023-06-27 18:01:14 -04:00
get_articles_from_path(STATIC_PATH),
key=lambda article: article.date,
reverse=True
);
2023-06-16 22:46:29 -04:00
2023-06-18 15:40:30 -04:00
@api.get('/home')
2023-06-16 22:46:29 -04:00
async def serve_home():
return DB
2023-06-16 22:46:29 -04:00
2023-06-18 15:40:30 -04:00
@api.get('/blog')
2023-06-16 22:46:29 -04:00
async def serve_blog():
return [entry for entry in DB if entry.content_type == 'blog']
2023-06-16 22:46:29 -04:00
2023-06-18 15:40:30 -04:00
@api.get('/projects')
2023-06-16 22:46:29 -04:00
async def serve_projects():
return [entry for entry in DB if entry.content_type == 'project' or entry.content_type == 'game']
2023-06-16 22:46:29 -04:00
2023-06-18 15:40:30 -04:00
@api.get('/bots')
2023-06-16 22:46:29 -04:00
async def serve_bots():
return [entry for entry in DB if entry.content_type == 'chatbot']