mirror of
https://github.com/Starlio-app/Starlio-web.git
synced 2024-11-21 16:26:21 +03:00
Добавил условие, если массив от API пустой, то 404, вернул старую логику /wallpaper/today
This commit is contained in:
parent
035cf23197
commit
c9dd20dbb7
1 changed files with 23 additions and 20 deletions
|
@ -1,10 +1,9 @@
|
||||||
from fastapi import APIRouter, Request
|
from http import HTTPStatus
|
||||||
from fastapi.responses import HTMLResponse, FileResponse, RedirectResponse
|
|
||||||
from fastapi.templating import Jinja2Templates
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from datetime import datetime
|
from fastapi import APIRouter, Request
|
||||||
from http import HTTPStatus
|
from fastapi.responses import HTMLResponse, FileResponse
|
||||||
|
from fastapi.templating import Jinja2Templates
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
@ -13,23 +12,27 @@ template = Jinja2Templates(directory='./src/web/html')
|
||||||
|
|
||||||
@router.get('/wallpaper/today', response_class=HTMLResponse)
|
@router.get('/wallpaper/today', response_class=HTMLResponse)
|
||||||
async def today_wallpaper(request: Request):
|
async def today_wallpaper(request: Request):
|
||||||
date = datetime.today().strftime('%Y-%m-%d')
|
|
||||||
res = requests.get(f'https://api.starlio.space/last')
|
res = requests.get(f'https://api.starlio.space/last')
|
||||||
|
|
||||||
if HTTPStatus(res.status_code).is_server_error or \
|
if HTTPStatus(res.status_code).is_server_error or HTTPStatus(res.status_code).is_client_error:
|
||||||
HTTPStatus(res.status_code).is_client_error:
|
return FileResponse('../web/html/error/404.html', status_code=HTTPStatus.NOT_FOUND)
|
||||||
return FileResponse('./src/web/html/error/404.html')
|
|
||||||
|
return template.TemplateResponse(
|
||||||
return RedirectResponse(f'/wallpaper/{date}')
|
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}')
|
|
||||||
|
@router.get('/wallpaper/{day}', response_class=HTMLResponse)
|
||||||
if HTTPStatus(res.status_code).is_server_error or \
|
async def wallpaper(request: Request, day):
|
||||||
HTTPStatus(res.status_code).is_client_error:
|
res = requests.get(f'https://api.starlio.space/wallpaper/{day}')
|
||||||
return FileResponse('./src/web/html/error/404.html')
|
|
||||||
|
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(
|
return template.TemplateResponse(
|
||||||
request,
|
request,
|
||||||
|
|
Loading…
Add table
Reference in a new issue