From b040ede864137e8e9375e25849a2857cfb8c36bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Wed, 31 Aug 2022 01:55:17 +0000 Subject: [PATCH] better ticker processing --- api/ovkiosk/routers/text.py | 61 ++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/api/ovkiosk/routers/text.py b/api/ovkiosk/routers/text.py index 416656f..1f308f6 100644 --- a/api/ovkiosk/routers/text.py +++ b/api/ovkiosk/routers/text.py @@ -1,7 +1,10 @@ import logging +import re +from typing import Iterator -from fastapi import APIRouter +from fastapi import APIRouter, Depends from markdown import Markdown +from pydantic import BaseModel from .. import CLIENT from ..config import SETTINGS @@ -36,19 +39,63 @@ async def get_message(): ) -@router.get("/ticker/content") -async def get_ticker_content(): - ticker_clean = ( +async def get_ticker_lines() -> Iterator[str]: + return ( line.strip() for line in str(_ticker).split("\n") - if line and not line.startswith(".") + if line.strip() ) - return _md.convert( - SETTINGS.ticker_separator.join(ticker_clean) + +async def get_ticker_content_lines( + ticker_lines: Iterator[str] = Depends(get_ticker_lines), +) -> Iterator[str]: + return ( + line + for line in ticker_lines + if not line.startswith(".") ) +@router.get("/ticker/content") +async def get_ticker_content( + ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines), +) -> str: + return _md.convert( + SETTINGS.ticker_separator.join(ticker_content_lines) + ) + +_re_ticker_command_line = re.compile( + r"^\.([a-z]+)\s+(.*)$", + flags=re.IGNORECASE, +) + + +class TickerCommand(BaseModel): + command: str + argument: str + + +async def get_ticker_commands( + ticker_lines: Iterator[str] = Depends(get_ticker_lines), +) -> Iterator[TickerCommand]: + return ( + TickerCommand( + command=match.group(1), + argument=match.group(2), + ) + for line in ticker_lines + if (match := _re_ticker_command_line.match(line)) + ) + + +@router.get("/ticker/commands", response_model=list[TickerCommand]) +async def get_ticker_commands( + ticker_commands: Iterator[str] = Depends(get_ticker_commands) +): + return list(ticker_commands) + + @router.get("/title") async def get_title(): return _md.convert(