mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-12-26 14:13:02 +00:00
114 lines
2.7 KiB
Python
114 lines
2.7 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 ..dav_common import dav_get_file, dav_list_files
|
|
from ._image import AdventImage
|
|
from ._misc import get_loesungswort, get_rnd, set_length, shuffle
|
|
|
|
router = APIRouter(prefix="/days", tags=["days"])
|
|
|
|
|
|
@router.on_event("startup")
|
|
async def startup() -> None:
|
|
loesungswort = await get_loesungswort()
|
|
print(loesungswort)
|
|
print("".join(await shuffle(loesungswort)))
|
|
|
|
|
|
@router.get("/letter/{index}")
|
|
async def get_letter(
|
|
index: int,
|
|
loesungswort: str = Depends(get_loesungswort),
|
|
) -> str:
|
|
return (await shuffle(loesungswort))[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 get_images() -> list[str]:
|
|
ls = await dav_list_files(_RE_IMAGE_FILE, "/images")
|
|
ls = await set_length(ls, 24)
|
|
|
|
return await shuffle(ls)
|
|
|
|
|
|
async def load_image(
|
|
index: int,
|
|
images: list[str] = Depends(get_images),
|
|
) -> AdventImage:
|
|
"""
|
|
Bild laden und einen quadratischen Ausschnitt
|
|
aus der Mitte nehmen
|
|
"""
|
|
|
|
# Bild laden
|
|
img_buffer = await dav_get_file(images[index])
|
|
img_buffer.seek(0)
|
|
return await AdventImage.load_standard(img_buffer)
|
|
|
|
|
|
@router.get(
|
|
"/picture/{index}",
|
|
response_class=StreamingResponse,
|
|
)
|
|
async def get_picture_for_day(
|
|
index: int,
|
|
letter: str = Depends(get_letter),
|
|
adv_img: AdventImage = Depends(load_image),
|
|
) -> StreamingResponse:
|
|
"""
|
|
Bild für einen Tag erstellen
|
|
"""
|
|
|
|
rnd = await get_rnd(index)
|
|
|
|
# Buchstabe verstecken
|
|
await adv_img.hide_text(
|
|
xy=tuple(rnd.choices(range(30, 470), k=2)),
|
|
text=letter,
|
|
font=ImageFont.truetype("Lena.ttf", 50),
|
|
)
|
|
|
|
# Bilddaten in Puffer laden
|
|
img_buffer = BytesIO()
|
|
adv_img.img.save(img_buffer, format="JPEG", quality=85)
|
|
img_buffer.seek(0)
|
|
|
|
return StreamingResponse(
|
|
content=img_buffer,
|
|
media_type="image/jpeg",
|
|
)
|