mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-12-25 22:02:59 +00:00
136 lines
3.2 KiB
Python
136 lines
3.2 KiB
Python
import re
|
|
# from datetime import date
|
|
from io import BytesIO
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from fastapi.responses import StreamingResponse
|
|
from PIL import ImageFont
|
|
|
|
from ..config import Config, get_config
|
|
from ..dav_common import dav_file_exists, dav_get_file, dav_list_files
|
|
from ._image import AdventImage
|
|
from ._misc import get_rnd, set_length, shuffle
|
|
|
|
router = APIRouter(prefix="/days", tags=["days"])
|
|
|
|
|
|
@router.on_event("startup")
|
|
async def startup() -> None:
|
|
cfg = await get_config()
|
|
print(cfg.solution)
|
|
print("".join(await shuffle(cfg.solution)))
|
|
|
|
|
|
@router.get("/letter/{index}")
|
|
async def get_letter(
|
|
index: int,
|
|
cfg: Config = Depends(get_config),
|
|
) -> str:
|
|
return (await shuffle(cfg.solution))[index]
|
|
|
|
|
|
# @router.get("/date")
|
|
# def get_date() -> int:
|
|
# return date.today().day
|
|
|
|
|
|
# @router.get(
|
|
# "/picture",
|
|
# response_class=StreamingResponse,
|
|
# )
|
|
# async def get_picture():
|
|
# img = Image.open("hand.png").convert("RGBA")
|
|
# d1 = ImageDraw.Draw(img)
|
|
# font = ImageFont.truetype("Lena.ttf", 50)
|
|
# d1.text((260, 155), "W", font=font, fill=(0, 0, 255))
|
|
# # d1.text(xy=(400, 210), text="Deine Hände auch?",
|
|
# # font=Font, fill=(255, 0, 0))
|
|
# img_buffer = BytesIO()
|
|
# img.save(img_buffer, format="PNG", quality=85)
|
|
# img_buffer.seek(0)
|
|
|
|
# return StreamingResponse(
|
|
# content=img_buffer,
|
|
# media_type="image/png",
|
|
# )
|
|
|
|
_RE_IMAGE_FILE = re.compile(
|
|
r"\.(gif|jpe?g|tiff?|png|bmp)$",
|
|
flags=re.IGNORECASE,
|
|
)
|
|
|
|
|
|
async def list_images_auto() -> list[str]:
|
|
ls = await dav_list_files(_RE_IMAGE_FILE, "/images_auto")
|
|
ls = await set_length(ls, 24)
|
|
|
|
return await shuffle(ls)
|
|
|
|
|
|
async def load_image(
|
|
file_name: str,
|
|
) -> AdventImage | None:
|
|
"""
|
|
Versuche, Bild aus Datei zu laden
|
|
"""
|
|
|
|
if not await dav_file_exists(file_name):
|
|
return None
|
|
|
|
img_buffer = await dav_get_file(file_name)
|
|
img_buffer.seek(0)
|
|
return await AdventImage.load_standard(img_buffer)
|
|
|
|
|
|
async def get_auto_image(
|
|
index: int,
|
|
letter: str = Depends(get_letter),
|
|
images: list[str] = Depends(list_images_auto),
|
|
) -> AdventImage:
|
|
|
|
image = await load_image(images[index])
|
|
assert image is not None
|
|
|
|
rnd = await get_rnd(index)
|
|
|
|
# Buchstabe verstecken
|
|
await image.hide_text(
|
|
xy=tuple(rnd.choices(range(30, 470), k=2)),
|
|
text=letter,
|
|
font=ImageFont.truetype("Lena.ttf", 50),
|
|
)
|
|
|
|
return image
|
|
|
|
|
|
@router.get(
|
|
"/picture/{index}",
|
|
response_class=StreamingResponse,
|
|
)
|
|
async def get_picture_for_day(
|
|
index: int,
|
|
) -> StreamingResponse:
|
|
"""
|
|
Bild für einen Tag erstellen
|
|
"""
|
|
|
|
# Versuche, aus "manual"-Ordner zu laden
|
|
image = await load_image(f"images_manual/{index}.jpg")
|
|
|
|
if image is None:
|
|
# Erstelle automatisch generiertes Bild
|
|
image = await get_auto_image(
|
|
index=index,
|
|
letter=await get_letter(index, await get_config()),
|
|
images=await list_images_auto()
|
|
)
|
|
|
|
# Bilddaten in Puffer laden
|
|
img_buffer = BytesIO()
|
|
image.img.save(img_buffer, format="JPEG", quality=85)
|
|
img_buffer.seek(0)
|
|
|
|
return StreamingResponse(
|
|
content=img_buffer,
|
|
media_type="image/jpeg",
|
|
)
|