Добавлен новый роут, теперь можно получить сегодняшние обои

This commit is contained in:
Данил 2024-09-27 17:34:59 +03:00
parent 752914349a
commit cc8e0333f9

View file

@ -1,20 +1,34 @@
from fastapi import APIRouter, Request from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse, FileResponse from fastapi.responses import HTMLResponse, FileResponse, RedirectResponse
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
import requests import requests
from datetime import datetime
router = APIRouter() router = APIRouter()
template = Jinja2Templates(directory='./src/web/html') 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) @router.get('/wallpaper/{day}', response_class=HTMLResponse)
async def wallpaper(request: Request, day): async def wallpaper(request: Request, day):
res = requests.get('https://api.nasa.gov/planetary/apod', params={ res = get_wallpaper(day)
'api_key': '1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et',
'date': day,
})
if res.status_code != 200: if res.status_code != 200:
return FileResponse('./src/web/html/error/404.html') return FileResponse('./src/web/html/error/404.html')
@ -23,4 +37,4 @@ async def wallpaper(request: Request, day):
request, request,
'/wallpaper.html', '/wallpaper.html',
{'info': res.json()} {'info': res.json()}
) )