2022-10-10 23:46:04 +00:00
|
|
|
import re
|
2022-10-14 21:42:05 +00:00
|
|
|
# from datetime import date
|
2022-10-10 00:09:09 +00:00
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from fastapi.responses import StreamingResponse
|
2022-10-14 23:35:17 +00:00
|
|
|
from PIL import ImageFont
|
2022-10-10 23:46:04 +00:00
|
|
|
|
2022-10-14 22:15:20 +00:00
|
|
|
from ..dav_common import dav_get_file, dav_list_files
|
2022-10-14 22:09:23 +00:00
|
|
|
from ._image import AdventImage
|
2022-10-14 23:21:14 +00:00
|
|
|
from ._misc import get_loesungswort, get_rnd, set_length, shuffle
|
2022-10-10 00:09:09 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/days", tags=["days"])
|
|
|
|
|
|
|
|
|
|
|
|
@router.on_event("startup")
|
2022-10-14 21:42:05 +00:00
|
|
|
async def startup() -> None:
|
2022-10-14 23:21:14 +00:00
|
|
|
loesungswort = await get_loesungswort()
|
2022-10-10 00:09:09 +00:00
|
|
|
print(loesungswort)
|
2022-10-14 22:47:16 +00:00
|
|
|
print("".join(await shuffle(loesungswort)))
|
2022-10-10 00:09:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/letter/{index}")
|
|
|
|
async def get_letter(
|
|
|
|
index: int,
|
2022-10-14 23:21:14 +00:00
|
|
|
loesungswort: str = Depends(get_loesungswort),
|
2022-10-10 00:09:09 +00:00
|
|
|
) -> str:
|
|
|
|
return (await shuffle(loesungswort))[index]
|
|
|
|
|
|
|
|
|
2022-10-14 21:42:05 +00:00
|
|
|
# @router.get("/date")
|
|
|
|
# def get_date() -> int:
|
|
|
|
# return date.today().day
|
2022-10-10 00:09:09 +00:00
|
|
|
|
|
|
|
|
2022-10-14 21:42:05 +00:00
|
|
|
# @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",
|
|
|
|
# )
|
2022-10-10 00:09:09 +00:00
|
|
|
|
2022-10-10 23:46:04 +00:00
|
|
|
_RE_IMAGE_FILE = re.compile(
|
|
|
|
r"\.(gif|jpe?g|tiff?|png|bmp)$",
|
|
|
|
flags=re.IGNORECASE,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-14 23:03:36 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2022-10-14 22:09:23 +00:00
|
|
|
async def load_image(
|
2022-10-11 00:29:27 +00:00
|
|
|
index: int,
|
2022-10-14 23:03:36 +00:00
|
|
|
images: list[str] = Depends(get_images),
|
2022-10-14 22:09:23 +00:00
|
|
|
) -> AdventImage:
|
2022-10-10 20:22:56 +00:00
|
|
|
"""
|
|
|
|
Bild laden und einen quadratischen Ausschnitt
|
|
|
|
aus der Mitte nehmen
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Bild laden
|
2022-10-14 23:03:36 +00:00
|
|
|
img_buffer = await dav_get_file(images[index])
|
2022-10-14 22:09:23 +00:00
|
|
|
return await AdventImage.load_standard(img_buffer)
|
2022-10-10 18:44:01 +00:00
|
|
|
|
|
|
|
|
2022-10-10 00:09:09 +00:00
|
|
|
@router.get(
|
|
|
|
"/picture/{index}",
|
|
|
|
response_class=StreamingResponse,
|
|
|
|
)
|
|
|
|
async def get_picture_for_day(
|
2022-10-10 22:59:52 +00:00
|
|
|
index: int,
|
2022-10-10 00:09:09 +00:00
|
|
|
letter: str = Depends(get_letter),
|
2022-10-14 22:09:23 +00:00
|
|
|
adv_img: AdventImage = Depends(load_image),
|
2022-10-10 00:09:09 +00:00
|
|
|
) -> StreamingResponse:
|
2022-10-10 20:22:56 +00:00
|
|
|
"""
|
|
|
|
Bild für einen Tag erstellen
|
|
|
|
"""
|
|
|
|
|
2022-10-14 23:21:14 +00:00
|
|
|
rnd = await get_rnd(index)
|
2022-10-10 20:22:56 +00:00
|
|
|
|
2022-10-14 23:38:37 +00:00
|
|
|
# Buchstabe verstecken
|
2022-10-14 23:35:17 +00:00
|
|
|
await adv_img.hide_text(
|
2022-10-14 23:38:37 +00:00
|
|
|
xy=tuple(rnd.choices(range(30, 470), k=2)),
|
2022-10-10 02:12:24 +00:00
|
|
|
text=letter,
|
2022-10-14 23:38:37 +00:00
|
|
|
font=ImageFont.truetype("Lena.ttf", 50),
|
2022-10-10 02:12:24 +00:00
|
|
|
)
|
|
|
|
|
2022-10-10 20:22:56 +00:00
|
|
|
# Bilddaten in Puffer laden
|
2022-10-10 00:09:09 +00:00
|
|
|
img_buffer = BytesIO()
|
2022-10-14 22:09:23 +00:00
|
|
|
adv_img.img.save(img_buffer, format="JPEG", quality=85)
|
2022-10-10 00:09:09 +00:00
|
|
|
img_buffer.seek(0)
|
|
|
|
|
|
|
|
return StreamingResponse(
|
|
|
|
content=img_buffer,
|
2022-10-10 20:25:11 +00:00
|
|
|
media_type="image/jpeg",
|
2022-10-10 00:09:09 +00:00
|
|
|
)
|