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

91 lines
2.2 KiB
Python
Raw Normal View History

2022-09-08 23:44:03 +00:00
"""
2022-09-09 02:23:48 +00:00
Router "ticker" provides:
- getting the ticker's raw content
- getting the ticker's HTML content (using Markdown)
- getting the ticker's UI config
2022-09-08 23:44:03 +00:00
"""
2023-10-26 16:21:07 +00:00
import logging
2022-09-08 23:44:03 +00:00
from typing import Iterator
2023-10-26 16:21:07 +00:00
import markdown
2022-09-08 23:44:03 +00:00
from fastapi import APIRouter, Depends
2023-10-26 16:26:51 +00:00
from ...core.config import Config, TickerUIConfig
from ...core.dav_common import webdav_ensure_files, webdav_ensure_path
from ...core.webdav import WebDAV
2023-10-26 16:26:51 +00:00
from ._common import LM_TEXT, RP_TEXT, get_config
2022-09-08 23:44:03 +00:00
2023-10-26 16:21:07 +00:00
_logger = logging.getLogger(__name__)
2022-09-08 23:44:03 +00:00
router = APIRouter(prefix="/ticker", tags=["text"])
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
2023-10-26 14:31:12 +00:00
remote_path = await RP_TEXT()
2023-10-23 21:32:25 +00:00
if not webdav_ensure_path(remote_path):
webdav_ensure_files(
remote_path,
"ticker.txt",
)
2022-09-19 11:42:43 +00:00
2022-09-08 23:44:03 +00:00
async def get_ticker_lines() -> Iterator[str]:
cfg = await get_config()
2023-10-26 14:31:12 +00:00
file_name = await LM_TEXT.getter.func(cfg.ticker.file_name)
remote_path = await RP_TEXT()
2022-09-08 23:44:03 +00:00
2023-10-23 21:32:25 +00:00
ticker = await WebDAV.read_str(f"{remote_path}/{file_name}")
2022-09-08 23:44:03 +00:00
2023-10-17 12:55:38 +00:00
return (line.strip() for line in ticker.split("\n") if line.strip())
2022-09-08 23:44:03 +00:00
async def get_ticker_content_lines(
ticker_lines: Iterator[str] = Depends(get_ticker_lines),
) -> Iterator[str]:
cfg = await get_config()
2022-09-08 23:44:03 +00:00
return (
2023-10-17 12:55:38 +00:00
line for line in ticker_lines if not line.startswith(cfg.ticker.comment_marker)
2022-09-08 23:44:03 +00:00
)
async def get_ticker_content(
ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines),
) -> str:
2022-09-19 11:55:22 +00:00
ticker_content_padded = ["", *ticker_content_lines, ""]
if len(ticker_content_padded) == 2:
return ""
cfg = await get_config()
ticker_content = cfg.ticker.separator.join(
2022-09-19 11:55:22 +00:00
ticker_content_padded,
2022-09-08 23:44:03 +00:00
)
return ticker_content.strip()
2022-09-15 22:24:32 +00:00
@router.get("/html")
2022-09-08 23:44:03 +00:00
async def get_ticker(
ticker_content: str = Depends(get_ticker_content),
) -> str:
2023-10-26 16:21:07 +00:00
return markdown.markdown(ticker_content)
2022-09-08 23:44:03 +00:00
2022-09-15 22:24:32 +00:00
@router.get("/raw")
2022-09-08 23:44:03 +00:00
async def get_raw_ticker(
ticker_content: str = Depends(get_ticker_content),
) -> str:
return ticker_content
2022-09-09 00:04:13 +00:00
2023-10-24 17:39:43 +00:00
@router.get("/config")
2022-09-09 00:04:13 +00:00
async def get_ui_config(
cfg: Config = Depends(get_config),
2022-09-09 00:04:13 +00:00
) -> TickerUIConfig:
return cfg.ticker