2022-08-31 01:55:17 +00:00
|
|
|
import re
|
|
|
|
from typing import Iterator
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-09-02 14:51:11 +00:00
|
|
|
from fastapi import APIRouter, Depends
|
2022-09-02 12:20:29 +00:00
|
|
|
from markdown import markdown
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-09-04 21:46:08 +00:00
|
|
|
from ..settings import SETTINGS
|
2022-08-29 11:27:18 +00:00
|
|
|
from ..dav_file import DavFile
|
2022-09-04 14:14:22 +00:00
|
|
|
from ._common import FileNameLister, PrefixFinder, PrefixUnique
|
2022-08-29 11:27:18 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/text", tags=["text"])
|
|
|
|
|
2022-09-02 13:22:35 +00:00
|
|
|
_lister = FileNameLister(
|
|
|
|
remote_path="text",
|
|
|
|
re=re.compile(
|
|
|
|
r"\.(txt|md)$",
|
|
|
|
flags=re.IGNORECASE,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-09-04 14:14:22 +00:00
|
|
|
_finder = PrefixFinder(_lister)
|
2022-09-04 14:14:22 +00:00
|
|
|
_unique = PrefixUnique(_finder)
|
2022-09-02 13:22:35 +00:00
|
|
|
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
async def get_ticker_lines() -> Iterator[str]:
|
2022-09-02 12:50:43 +00:00
|
|
|
ticker = await DavFile("text/ticker.txt").string
|
2022-09-02 12:20:29 +00:00
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
return (
|
2022-08-29 19:20:03 +00:00
|
|
|
line.strip()
|
2022-09-02 12:20:29 +00:00
|
|
|
for line in 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
|
2022-09-02 12:50:43 +00:00
|
|
|
if not line.startswith("#")
|
2022-08-29 19:20:03 +00:00
|
|
|
)
|
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
|
2022-09-02 12:50:43 +00:00
|
|
|
@router.get("/get/ticker")
|
2022-08-31 01:55:17 +00:00
|
|
|
async def get_ticker_content(
|
|
|
|
ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines),
|
|
|
|
) -> str:
|
2022-09-02 12:20:29 +00:00
|
|
|
return markdown(
|
2022-08-31 01:55:17 +00:00
|
|
|
SETTINGS.ticker_separator.join(ticker_content_lines)
|
|
|
|
)
|
|
|
|
|
2022-09-02 12:50:43 +00:00
|
|
|
|
|
|
|
@router.get("/list", response_model=list[str])
|
|
|
|
async def list_texts(
|
2022-09-04 14:14:22 +00:00
|
|
|
names: Iterator[str] = Depends(_lister),
|
2022-09-02 12:50:43 +00:00
|
|
|
) -> list[str]:
|
2022-09-04 14:14:22 +00:00
|
|
|
return list(names)
|
2022-09-02 12:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/find/{prefix}", response_model=list[str])
|
|
|
|
async def find_texts(
|
2022-09-04 14:14:22 +00:00
|
|
|
names: Iterator[str] = Depends(_finder),
|
2022-09-02 12:50:43 +00:00
|
|
|
) -> list[str]:
|
2022-09-04 14:14:22 +00:00
|
|
|
return list(names)
|
2022-09-02 12:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
|
|
"/get/{prefix}",
|
2022-09-02 13:22:35 +00:00
|
|
|
response_model=str,
|
2022-09-04 14:14:22 +00:00
|
|
|
responses=_unique.responses,
|
2022-09-02 12:50:43 +00:00
|
|
|
)
|
|
|
|
async def get_text(
|
2022-09-04 14:14:22 +00:00
|
|
|
name: str = Depends(_unique),
|
2022-09-02 12:50:43 +00:00
|
|
|
) -> str:
|
2022-09-04 14:14:22 +00:00
|
|
|
text = await DavFile(f"{_lister.remote_path}/{name}").string
|
2022-09-02 12:20:29 +00:00
|
|
|
|
|
|
|
return markdown(
|
2022-09-02 12:50:43 +00:00
|
|
|
text
|
2022-08-29 19:20:03 +00:00
|
|
|
)
|