88 lines
1.9 KiB
Python
88 lines
1.9 KiB
Python
import re
|
|
from typing import Iterator
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from markdown import markdown
|
|
from pydantic import BaseModel
|
|
|
|
from ..config import SETTINGS
|
|
from ..dav_file import DavFile
|
|
|
|
router = APIRouter(prefix="/text", tags=["text"])
|
|
|
|
|
|
@router.get("/message")
|
|
async def get_message() -> str:
|
|
message = await DavFile("message.txt").string
|
|
|
|
return markdown(
|
|
message
|
|
)
|
|
|
|
|
|
async def get_ticker_lines() -> Iterator[str]:
|
|
ticker = await DavFile("ticker.txt").string
|
|
|
|
return (
|
|
line.strip()
|
|
for line in ticker.split("\n")
|
|
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(".")
|
|
)
|
|
|
|
|
|
@router.get("/ticker/content")
|
|
async def get_ticker_content(
|
|
ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines),
|
|
) -> str:
|
|
return markdown(
|
|
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))
|
|
)
|
|
|
|
|
|
@router.get("/ticker/commands", response_model=list[TickerCommand])
|
|
async def get_ticker_commands(
|
|
ticker_commands: Iterator[str] = Depends(get_ticker_commands)
|
|
) -> list[TickerCommand]:
|
|
return list(ticker_commands)
|
|
|
|
|
|
@router.get("/title")
|
|
async def get_title() -> str:
|
|
title = await DavFile("title.txt").string
|
|
|
|
return markdown(
|
|
title
|
|
)
|