better ticker processing

This commit is contained in:
Jörn-Michael Miehe 2022-08-31 01:55:17 +00:00
parent bdb4933887
commit b040ede864

View file

@ -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(