2022-09-08 23:44:03 +00:00
|
|
|
"""
|
|
|
|
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()
|
2022-09-08 23:59:17 +00:00
|
|
|
file_name = await text_unique(cfg.ticker.api.file_name)
|
2022-09-08 23:44:03 +00:00
|
|
|
|
|
|
|
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
|
2022-09-08 23:59:17 +00:00
|
|
|
if not line.startswith(cfg.ticker.api.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:
|
|
|
|
cfg = await Config.get()
|
2022-09-08 23:59:17 +00:00
|
|
|
ticker_content = cfg.ticker.api.separator.join(
|
2022-09-08 23:44:03 +00:00
|
|
|
["", *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
|