From cc8e0333f9da8cfd1b67295cef8c51c3027d2041 Mon Sep 17 00:00:00 2001 From: Redume Date: Fri, 27 Sep 2024 17:34:59 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=80=D0=BE=D1=83?= =?UTF-8?q?=D1=82,=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B6=D0=BD=D0=BE=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D1=81=D0=B5=D0=B3=D0=BE=D0=B4=D0=BD=D1=8F=D1=88=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D0=B1=D0=BE=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/wallpaper.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/routes/wallpaper.py b/src/routes/wallpaper.py index 24adbd4..f9bbdf2 100644 --- a/src/routes/wallpaper.py +++ b/src/routes/wallpaper.py @@ -1,20 +1,34 @@ from fastapi import APIRouter, Request -from fastapi.responses import HTMLResponse, FileResponse +from fastapi.responses import HTMLResponse, FileResponse, RedirectResponse from fastapi.templating import Jinja2Templates import requests +from datetime import datetime router = APIRouter() template = Jinja2Templates(directory='./src/web/html') +def get_wallpaper(date: str): + return requests.get('https://api.nasa.gov/planetary/apod', params={ + 'api_key': '1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et', + 'date': date, + }) + +@router.get('/wallpaper/today', response_class=HTMLResponse) +async def today_wallpaper(requests: Request): + date = datetime.today().strftime('%Y-%m-%d') + res = get_wallpaper(date) + + if res.status_code != 200: + return FileResponse('./src/web/html/error/404.html') + + return RedirectResponse(f'/wallpaper/{date}') + @router.get('/wallpaper/{day}', response_class=HTMLResponse) async def wallpaper(request: Request, day): - res = requests.get('https://api.nasa.gov/planetary/apod', params={ - 'api_key': '1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et', - 'date': day, - }) + res = get_wallpaper(day) if res.status_code != 200: return FileResponse('./src/web/html/error/404.html') @@ -23,4 +37,4 @@ async def wallpaper(request: Request, day): request, '/wallpaper.html', {'info': res.json()} - ) + ) \ No newline at end of file