advent22/api/advent22_api/routers/_misc.py

153 lines
3.3 KiB
Python
Raw Normal View History

2022-10-14 22:47:16 +00:00
import itertools
import random
2022-11-15 22:58:04 +00:00
import re
2023-09-04 21:23:27 +00:00
from io import BytesIO
2023-09-03 16:23:01 +00:00
from typing import Any, Self, Sequence
2022-10-14 22:47:16 +00:00
2022-11-15 22:58:04 +00:00
from fastapi import Depends
2023-09-04 21:23:27 +00:00
from fastapi.responses import StreamingResponse
from PIL import Image, ImageFont
2022-11-15 22:58:04 +00:00
from ..config import Config, get_config
from ..dav_common import dav_file_exists, dav_get_file, dav_list_files
from ._image import AdventImage
##########
# RANDOM #
##########
2022-10-14 22:47:16 +00:00
2022-11-23 03:36:40 +00:00
class Random(random.Random):
@classmethod
2023-09-03 16:23:01 +00:00
async def get(cls, bonus_salt: Any = "") -> Self:
2022-11-23 03:36:40 +00:00
cfg = await get_config()
return cls(f"{cfg.puzzle.solution}{bonus_salt}")
2022-10-14 23:21:14 +00:00
2022-10-14 22:47:16 +00:00
2022-10-14 23:03:36 +00:00
async def set_length(seq: Sequence, length: int) -> list:
2022-10-14 23:20:35 +00:00
# `seq` unendlich wiederholen
2022-10-14 23:03:36 +00:00
infinite = itertools.cycle(seq)
2022-10-14 23:20:35 +00:00
# Die ersten `length` einträge nehmen
2022-10-14 23:03:36 +00:00
return list(itertools.islice(infinite, length))
2022-10-14 22:47:16 +00:00
2022-10-14 23:21:14 +00:00
async def shuffle(seq: Sequence, rnd: random.Random | None = None) -> list:
2022-10-14 23:20:35 +00:00
# Zufallsgenerator
2022-11-23 03:36:40 +00:00
rnd = rnd or await Random.get()
2022-10-14 23:20:35 +00:00
# Elemente mischen
2022-10-14 22:47:16 +00:00
return rnd.sample(seq, len(seq))
2022-11-15 22:58:04 +00:00
2023-09-03 16:44:18 +00:00
2022-11-15 22:58:04 +00:00
#########
# IMAGE #
#########
async def get_letter(
index: int,
cfg: Config = Depends(get_config),
) -> str:
2022-11-18 01:39:05 +00:00
return (await shuffle(cfg.puzzle.solution))[index]
2022-11-15 22:58:04 +00:00
_RE_IMAGE_FILE = re.compile(
r"\.(gif|jpe?g|tiff?|png|bmp)$",
flags=re.IGNORECASE,
)
async def list_images_auto() -> list[str]:
"""
Finde alle Bilder im "automatisch"-Verzeichnis
"""
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:
"""
Versuche, Bild aus Datei zu laden
"""
if not await dav_file_exists(file_name):
raise RuntimeError(f"DAV-File {file_name} does not exist!")
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,
2023-09-04 21:42:58 +00:00
letter: str,
images: list[str],
cfg: Config,
2022-11-15 22:58:04 +00:00
) -> AdventImage:
"""
Erstelle automatisch generiertes Bild
"""
# hier niemals RuntimeError!
image = await load_image(images[index])
2022-11-23 03:36:40 +00:00
rnd = await Random.get(index)
2022-11-15 22:58:04 +00:00
2023-09-04 21:42:58 +00:00
font = await dav_get_file(f"files/{cfg.puzzle.font}")
font.seek(0)
2022-12-08 21:16:41 +00:00
2022-11-15 22:58:04 +00:00
# Buchstabe verstecken
await image.hide_text(
xy=tuple(rnd.choices(range(30, 470), k=2)),
text=letter,
2023-09-04 21:42:58 +00:00
font=ImageFont.truetype(font, 50),
2022-11-15 22:58:04 +00:00
)
return image
async def get_image(
index: int,
letter: str = Depends(get_letter),
images: list[str] = Depends(list_images_auto),
2023-09-04 21:42:58 +00:00
cfg: Config = Depends(get_config),
2022-11-15 22:58:04 +00:00
) -> AdventImage:
"""
Bild für einen Tag erstellen
"""
try:
# Versuche, aus "manual"-Ordner zu laden
return await load_image(f"images_manual/{index}.jpg")
except RuntimeError:
# Erstelle automatisch generiertes Bild
2023-09-04 21:42:58 +00:00
return await get_auto_image(
index=index,
letter=letter,
images=images,
cfg=cfg,
)
2023-09-04 21:23:27 +00:00
async def api_return_image(
img: Image.Image,
) -> StreamingResponse:
"""
Bild mit API zurückgeben
"""
# Bilddaten in Puffer laden
img_buffer = BytesIO()
img.save(img_buffer, format="JPEG", quality=85)
img_buffer.seek(0)
# zurückgeben
return StreamingResponse(
content=img_buffer,
media_type="image/jpeg",
)