ovdashboard/api/ovdashboard_api/routers/text.py

89 lines
1.9 KiB
Python
Raw Normal View History

2022-08-31 01:55:17 +00:00
import re
from typing import Iterator
2022-08-29 11:27:18 +00:00
2022-08-31 01:55:17 +00:00
from fastapi import APIRouter, Depends
from markdown import markdown
2022-08-31 01:55:17 +00:00
from pydantic import BaseModel
2022-08-29 11:27:18 +00:00
2022-08-31 01:29:44 +00:00
from ..config import SETTINGS
2022-08-29 11:27:18 +00:00
from ..dav_file import DavFile
router = APIRouter(prefix="/text", tags=["text"])
@router.get("/message")
2022-08-31 10:21:11 +00:00
async def get_message() -> str:
message = await DavFile("message.txt").string
return markdown(
message
2022-08-29 19:20:03 +00:00
)
2022-08-29 11:27:18 +00:00
2022-08-31 01:55:17 +00:00
async def get_ticker_lines() -> Iterator[str]:
ticker = await DavFile("ticker.txt").string
2022-08-31 01:55:17 +00:00
return (
2022-08-29 19:20:03 +00:00
line.strip()
for line in ticker.split("\n")
2022-08-31 01:55:17 +00:00
if line.strip()
)
async def get_ticker_content_lines(
ticker_lines: Iterator[str] = Depends(get_ticker_lines),
) -> Iterator[str]:
return (
line
for line in ticker_lines
if not line.startswith(".")
2022-08-29 19:20:03 +00:00
)
2022-08-31 01:55:17 +00:00
@router.get("/ticker/content")
async def get_ticker_content(
ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines),
) -> str:
return markdown(
2022-08-31 01:55:17 +00:00
SETTINGS.ticker_separator.join(ticker_content_lines)
)
_re_ticker_command_line = re.compile(
r"^\.([a-z]+)\s+(.*)$",
flags=re.IGNORECASE,
)
class TickerCommand(BaseModel):
command: str
argument: str
async def get_ticker_commands(
ticker_lines: Iterator[str] = Depends(get_ticker_lines),
) -> Iterator[TickerCommand]:
return (
TickerCommand(
command=match.group(1),
argument=match.group(2),
)
for line in ticker_lines
if (match := _re_ticker_command_line.match(line))
2022-08-29 19:20:03 +00:00
)
2022-08-29 11:27:18 +00:00
2022-08-31 01:55:17 +00:00
@router.get("/ticker/commands", response_model=list[TickerCommand])
async def get_ticker_commands(
ticker_commands: Iterator[str] = Depends(get_ticker_commands)
2022-08-31 10:21:11 +00:00
) -> list[TickerCommand]:
2022-08-31 01:55:17 +00:00
return list(ticker_commands)
2022-08-29 11:27:18 +00:00
@router.get("/title")
2022-08-31 10:21:11 +00:00
async def get_title() -> str:
title = await DavFile("title.txt").string
return markdown(
title
2022-08-29 19:20:03 +00:00
)