From 83db799b9661fa42d60c3acb4e51a712185a7d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 8 Sep 2022 23:44:03 +0000 Subject: [PATCH] ticker router --- api/ovdashboard_api/routers/__init__.py | 3 +- api/ovdashboard_api/routers/text.py | 53 ---------------- api/ovdashboard_api/routers/ticker.py | 84 +++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 54 deletions(-) create mode 100644 api/ovdashboard_api/routers/ticker.py diff --git a/api/ovdashboard_api/routers/__init__.py b/api/ovdashboard_api/routers/__init__.py index 9fa4d4b..f09888c 100644 --- a/api/ovdashboard_api/routers/__init__.py +++ b/api/ovdashboard_api/routers/__init__.py @@ -7,10 +7,11 @@ This file: Main API router definition. from fastapi import APIRouter from ..settings import SETTINGS -from . import aggregate, calendar, image, text +from . import aggregate, calendar, image, text, ticker main_router = APIRouter(prefix=f"/{SETTINGS.api_v1_prefix}") main_router.include_router(text.router) +main_router.include_router(ticker.router) main_router.include_router(image.router) main_router.include_router(calendar.router) main_router.include_router(aggregate.router) diff --git a/api/ovdashboard_api/routers/text.py b/api/ovdashboard_api/routers/text.py index 4b3034b..37b129d 100644 --- a/api/ovdashboard_api/routers/text.py +++ b/api/ovdashboard_api/routers/text.py @@ -16,7 +16,6 @@ from typing import Iterator from fastapi import APIRouter, Depends from markdown import markdown -from ..config import Config from ..dav_common import webdav_ensure_path from ..dav_file import DavFile from ._common import FileNameLister, PrefixFinder, PrefixUnique @@ -44,58 +43,6 @@ async def start_router() -> None: webdav_ensure_path(await text_lister.remote_path) -async def get_ticker_lines() -> Iterator[str]: - cfg = await Config.get() - file_name = await text_unique(cfg.ticker.file_name) - - ticker = await DavFile( - f"{await text_lister.remote_path}/{file_name}", - ).as_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]: - cfg = await Config.get() - - 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: - cfg = await Config.get() - ticker_content = cfg.ticker.separator.join( - ["", *ticker_content_lines, ""], - ) - - return ticker_content.strip() - - -@ router.get("/get/html/ticker") -async def get_ticker( - ticker_content: str = Depends(get_ticker_content), -) -> str: - return markdown(ticker_content) - - -@ router.get("/get/raw/ticker") -async def get_raw_ticker( - ticker_content: str = Depends(get_ticker_content), -) -> str: - return ticker_content - - @router.get( "/list", response_model=list[str], diff --git a/api/ovdashboard_api/routers/ticker.py b/api/ovdashboard_api/routers/ticker.py new file mode 100644 index 0000000..1fabf55 --- /dev/null +++ b/api/ovdashboard_api/routers/ticker.py @@ -0,0 +1,84 @@ +""" +Router "text" provides: + +- listing text files +- finding text files by name prefix +- getting text file raw content by name prefix +- getting text file HTML content by name prefix (using Markdown) +- getting the "ticker" raw content +- getting the "ticker" HTML content (using Markdown) +""" + +from logging import getLogger +from typing import Iterator + +from fastapi import APIRouter, Depends +from markdown import markdown + +from ..config import Config +from ..dav_common import webdav_ensure_path +from ..dav_file import DavFile +from .text import text_lister, text_unique + +_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.") + + webdav_ensure_path(await text_lister.remote_path) + + +async def get_ticker_lines() -> Iterator[str]: + cfg = await Config.get() + file_name = await text_unique(cfg.ticker.file_name) + + ticker = await DavFile( + f"{await text_lister.remote_path}/{file_name}", + ).as_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]: + cfg = await Config.get() + + 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: + cfg = await Config.get() + ticker_content = cfg.ticker.separator.join( + ["", *ticker_content_lines, ""], + ) + + return ticker_content.strip() + + +@ router.get("/get/html") +async def get_ticker( + ticker_content: str = Depends(get_ticker_content), +) -> str: + return markdown(ticker_content) + + +@ router.get("/get/raw") +async def get_raw_ticker( + ticker_content: str = Depends(get_ticker_content), +) -> str: + return ticker_content