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
|
|
|
"""
|
|
|
|
|
|
|
|
from logging import getLogger
|
|
|
|
from typing import Iterator
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from markdown import markdown
|
|
|
|
|
2023-10-20 11:40:15 +00:00
|
|
|
from ...core.config import Config, TickerUIConfig, get_config
|
|
|
|
from ...core.dav_common import webdav_ensure_files, webdav_ensure_path
|
|
|
|
from ...core.webdav import WebDAV
|
2023-10-23 21:32:25 +00:00
|
|
|
from .text import _fpu, _rp
|
2022-09-08 23:44:03 +00:00
|
|
|
|
|
|
|
_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.")
|
|
|
|
|
2023-10-25 18:50:03 +00:00
|
|
|
remote_path = await _rp.func()
|
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]:
|
2023-10-20 11:40:15 +00:00
|
|
|
cfg = await get_config()
|
2023-10-25 18:50:03 +00:00
|
|
|
file_name = await _fpu.func(cfg.ticker.file_name)
|
|
|
|
remote_path = await _rp.func()
|
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]:
|
2023-10-20 11:40:15 +00:00
|
|
|
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 ""
|
|
|
|
|
2023-10-20 11:40:15 +00:00
|
|
|
cfg = await get_config()
|
2022-09-15 22:14:02 +00:00
|
|
|
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:
|
|
|
|
return markdown(ticker_content)
|
|
|
|
|
|
|
|
|
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(
|
2023-10-20 11:40:15 +00:00
|
|
|
cfg: Config = Depends(get_config),
|
2022-09-09 00:04:13 +00:00
|
|
|
) -> TickerUIConfig:
|
2022-09-15 22:14:02 +00:00
|
|
|
return cfg.ticker
|