ovdashboard/api/ovdashboard_api/routers/v1/ticker.py

90 lines
2.2 KiB
Python

"""
Router "ticker" provides:
- getting the ticker's raw content
- getting the ticker's HTML content (using Markdown)
- getting the ticker's UI config
"""
from logging import getLogger
from typing import Iterator
from fastapi import APIRouter, Depends
from markdown import markdown
from ...core.config import Config, TickerUIConfig, get_config
from ...core.dav_common import webdav_ensure_files, webdav_ensure_path
from ...core.webdav import WebDAV
from ._common import LM_TEXT, RP_TEXT
_logger = getLogger(__name__)
router = APIRouter(prefix="/ticker", tags=["text"])
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
remote_path = await RP_TEXT()
if not webdav_ensure_path(remote_path):
webdav_ensure_files(
remote_path,
"ticker.txt",
)
async def get_ticker_lines() -> Iterator[str]:
cfg = await get_config()
file_name = await LM_TEXT.getter.func(cfg.ticker.file_name)
remote_path = await RP_TEXT()
ticker = await WebDAV.read_str(f"{remote_path}/{file_name}")
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]:
cfg = await get_config()
return (
line for line in ticker_lines if not line.startswith(cfg.ticker.comment_marker)
)
async def get_ticker_content(
ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines),
) -> str:
ticker_content_padded = ["", *ticker_content_lines, ""]
if len(ticker_content_padded) == 2:
return ""
cfg = await get_config()
ticker_content = cfg.ticker.separator.join(
ticker_content_padded,
)
return ticker_content.strip()
@router.get("/html")
async def get_ticker(
ticker_content: str = Depends(get_ticker_content),
) -> str:
return markdown(ticker_content)
@router.get("/raw")
async def get_raw_ticker(
ticker_content: str = Depends(get_ticker_content),
) -> str:
return ticker_content
@router.get("/config")
async def get_ui_config(
cfg: Config = Depends(get_config),
) -> TickerUIConfig:
return cfg.ticker