advent22/api/advent22_api/routers/days.py

75 lines
1.7 KiB
Python
Raw Normal View History

2022-10-14 21:42:05 +00:00
# from datetime import date
from io import BytesIO
from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
2022-10-10 23:46:04 +00:00
2022-11-04 18:49:31 +00:00
from ..config import Config, get_config
2022-10-14 22:09:23 +00:00
from ._image import AdventImage
2022-11-15 22:58:04 +00:00
from ._misc import get_image, shuffle
router = APIRouter(prefix="/days", tags=["days"])
@router.on_event("startup")
2022-10-14 21:42:05 +00:00
async def startup() -> None:
2022-11-04 18:49:31 +00:00
cfg = await get_config()
print(cfg.solution)
print("".join(await shuffle(cfg.solution)))
@router.get("/letter/{index}")
async def get_letter(
index: int,
2022-11-04 18:49:31 +00:00
cfg: Config = Depends(get_config),
) -> str:
2022-11-04 18:49:31 +00:00
return (await shuffle(cfg.solution))[index]
2022-10-14 21:42:05 +00:00
# @router.get("/date")
# def get_date() -> int:
# return date.today().day
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-11-04 20:00:43 +00:00
@router.get(
"/picture/{index}",
response_class=StreamingResponse,
)
2022-11-15 22:58:04 +00:00
async def get_image_for_day(
image: AdventImage = Depends(get_image),
) -> StreamingResponse:
2022-10-10 20:22:56 +00:00
"""
Bild für einen Tag erstellen
"""
# Bilddaten in Puffer laden
img_buffer = BytesIO()
2022-11-04 20:00:43 +00:00
image.img.save(img_buffer, format="JPEG", quality=85)
img_buffer.seek(0)
return StreamingResponse(
content=img_buffer,
2022-10-10 20:25:11 +00:00
media_type="image/jpeg",
)