From c9dd20dbb7d9753249284f3d70452414ac0e6d31 Mon Sep 17 00:00:00 2001 From: Redume Date: Wed, 23 Oct 2024 17:01:10 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=83=D1=81=D0=BB=D0=BE=D0=B2=D0=B8=D0=B5,=20=D0=B5=D1=81?= =?UTF-8?q?=D0=BB=D0=B8=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=20=D0=BE?= =?UTF-8?q?=D1=82=20API=20=D0=BF=D1=83=D1=81=D1=82=D0=BE=D0=B9,=20=D1=82?= =?UTF-8?q?=D0=BE=20404,=20=D0=B2=D0=B5=D1=80=D0=BD=D1=83=D0=BB=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=80=D1=83=D1=8E=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D1=83=20`/wallpaper/today`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/wallpaper.py | 43 ++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/routes/wallpaper.py b/src/routes/wallpaper.py index 79273a3..ba0a422 100644 --- a/src/routes/wallpaper.py +++ b/src/routes/wallpaper.py @@ -1,10 +1,9 @@ -from fastapi import APIRouter, Request -from fastapi.responses import HTMLResponse, FileResponse, RedirectResponse -from fastapi.templating import Jinja2Templates +from http import HTTPStatus import requests -from datetime import datetime -from http import HTTPStatus +from fastapi import APIRouter, Request +from fastapi.responses import HTMLResponse, FileResponse +from fastapi.templating import Jinja2Templates router = APIRouter() @@ -13,23 +12,27 @@ template = Jinja2Templates(directory='./src/web/html') @router.get('/wallpaper/today', response_class=HTMLResponse) async def today_wallpaper(request: Request): - date = datetime.today().strftime('%Y-%m-%d') res = requests.get(f'https://api.starlio.space/last') - if HTTPStatus(res.status_code).is_server_error or \ - HTTPStatus(res.status_code).is_client_error: - 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(f'https://api.starlio.space/wallpaper/{day}') - - if HTTPStatus(res.status_code).is_server_error or \ - HTTPStatus(res.status_code).is_client_error: - return FileResponse('./src/web/html/error/404.html') + if HTTPStatus(res.status_code).is_server_error or HTTPStatus(res.status_code).is_client_error: + return FileResponse('../web/html/error/404.html', status_code=HTTPStatus.NOT_FOUND) + + return template.TemplateResponse( + request, + '/wallpaper.html', + {'info': res.json()} + ) + + +@router.get('/wallpaper/{day}', response_class=HTMLResponse) +async def wallpaper(request: Request, day): + res = requests.get(f'https://api.starlio.space/wallpaper/{day}') + + if (HTTPStatus(res.status_code).is_server_error + or HTTPStatus(res.status_code).is_client_error + or len(res.json()) <= 0): + + return FileResponse('./src/web/html/error/404.html', status_code=HTTPStatus.NOT_FOUND) return template.TemplateResponse( request,