2022-08-29 11:27:18 +00:00
|
|
|
import logging
|
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
|
2022-08-29 19:20:03 +00:00
|
|
|
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 .. import CLIENT
|
|
|
|
from ..config import SETTINGS
|
2022-08-29 11:27:18 +00:00
|
|
|
from ..dav_file import DavFile
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/text", tags=["text"])
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
2022-08-29 19:20:03 +00:00
|
|
|
_md = Markdown()
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-08-31 01:29:44 +00:00
|
|
|
_message = ""
|
|
|
|
_ticker = ""
|
|
|
|
_title = ""
|
2022-08-29 11:27:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.on_event("startup")
|
2022-08-31 10:21:11 +00:00
|
|
|
async def on_startup() -> None:
|
2022-08-31 01:29:44 +00:00
|
|
|
global _message, _ticker, _title
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-08-31 08:42:17 +00:00
|
|
|
_message = DavFile(CLIENT.resource("message.txt"))
|
|
|
|
_ticker = DavFile(CLIENT.resource("ticker.txt"))
|
|
|
|
_title = DavFile(CLIENT.resource("title.txt"))
|
2022-08-31 01:29:44 +00:00
|
|
|
DavFile.refresh(60)
|
|
|
|
|
|
|
|
_logger.debug("text router started")
|
2022-08-29 11:27:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/message")
|
2022-08-31 10:21:11 +00:00
|
|
|
async def get_message() -> str:
|
2022-08-29 19:20:03 +00:00
|
|
|
return _md.convert(
|
|
|
|
str(_message)
|
|
|
|
)
|
2022-08-29 11:27:18 +00:00
|
|
|
|
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
async def get_ticker_lines() -> Iterator[str]:
|
|
|
|
return (
|
2022-08-29 19:20:03 +00:00
|
|
|
line.strip()
|
|
|
|
for line in str(_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:
|
2022-08-29 19:20:03 +00:00
|
|
|
return _md.convert(
|
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:
|
2022-08-29 19:20:03 +00:00
|
|
|
return _md.convert(
|
|
|
|
str(_title)
|
|
|
|
)
|