2022-08-29 11:27:18 +00:00
|
|
|
import logging
|
2022-08-31 01:55:17 +00:00
|
|
|
import re
|
|
|
|
from typing import Iterator
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
from fastapi import APIRouter, Depends
|
2022-08-29 19:20:03 +00:00
|
|
|
from markdown import Markdown
|
2022-08-31 01:55:17 +00:00
|
|
|
from pydantic import BaseModel
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-08-31 01:29:44 +00:00
|
|
|
from .. import CLIENT
|
|
|
|
from ..config import SETTINGS
|
2022-08-29 11:27:18 +00:00
|
|
|
from ..dav_file import DavFile
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/text", tags=["text"])
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
2022-08-29 19:20:03 +00:00
|
|
|
_md = Markdown()
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-08-31 01:29:44 +00:00
|
|
|
_message = ""
|
|
|
|
_ticker = ""
|
|
|
|
_title = ""
|
2022-08-29 11:27:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.on_event("startup")
|
|
|
|
async def on_startup():
|
2022-08-31 01:29:44 +00:00
|
|
|
global _message, _ticker, _title
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-08-31 01:29:44 +00:00
|
|
|
_message = DavFile(client=CLIENT, path="message.txt")
|
|
|
|
_ticker = DavFile(client=CLIENT, path="ticker.txt")
|
|
|
|
_title = DavFile(client=CLIENT, path="title.txt")
|
|
|
|
DavFile.refresh(60)
|
|
|
|
|
|
|
|
_logger.debug("text router started")
|
2022-08-29 11:27:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/message")
|
|
|
|
async def get_message():
|
2022-08-29 19:20:03 +00:00
|
|
|
return _md.convert(
|
|
|
|
str(_message)
|
|
|
|
)
|
2022-08-29 11:27:18 +00:00
|
|
|
|
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
async def get_ticker_lines() -> Iterator[str]:
|
|
|
|
return (
|
2022-08-29 19:20:03 +00:00
|
|
|
line.strip()
|
|
|
|
for line in str(_ticker).split("\n")
|
2022-08-31 01:55:17 +00:00
|
|
|
if line.strip()
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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(".")
|
2022-08-29 19:20:03 +00:00
|
|
|
)
|
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
|
|
|
|
@router.get("/ticker/content")
|
|
|
|
async def get_ticker_content(
|
|
|
|
ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines),
|
|
|
|
) -> str:
|
2022-08-29 19:20:03 +00:00
|
|
|
return _md.convert(
|
2022-08-31 01:55:17 +00:00
|
|
|
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))
|
2022-08-29 19:20:03 +00:00
|
|
|
)
|
2022-08-29 11:27:18 +00:00
|
|
|
|
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
@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)
|
|
|
|
|
|
|
|
|
2022-08-29 11:27:18 +00:00
|
|
|
@router.get("/title")
|
|
|
|
async def get_title():
|
2022-08-29 19:20:03 +00:00
|
|
|
return _md.convert(
|
|
|
|
str(_title)
|
|
|
|
)
|