refactor: The long lines were broken into smaller ones

This commit is contained in:
Данил 2025-02-20 21:48:44 +03:00
parent 6f304d650e
commit 4f70c6c0e3
3 changed files with 34 additions and 14 deletions

11
main.py
View file

@ -5,8 +5,10 @@ middleware, static file serving, and error handling.
Routes:
- `/app-ads.txt`: Serves the `app-ads.txt` file.
- `/robots.txt`: Serves the `robots.txt` file.
- `/wallpaper/today`: Served by the wallpaper router, fetches today's wallpaper.
- `/wallpaper/{day}`: Served by the wallpaper router, fetches the wallpaper for a specific day.
- `/wallpaper/today`: Served by the wallpaper router,
fetches today's wallpaper.
- `/wallpaper/{day}`: Served by the wallpaper router,
fetches the wallpaper for a specific day.
"""
import http
@ -46,7 +48,10 @@ async def robots_txt():
@app.exception_handler(404)
async def not_found(req, __):
return FileResponse('./src/web/html/error/404.html', status_code=http.HTTPStatus.NOT_FOUND)
return FileResponse(
'./src/web/html/error/404.html',
status_code=http.HTTPStatus.NOT_FOUND
)
if __name__ == '__main__':
uvicorn.run(app,

View file

@ -1,8 +1,10 @@
"""
This module contains the PlausibleAnalytics middleware for sending analytics events
This module contains the PlausibleAnalytics middleware
for sending analytics events
to Plausible Analytics after processing each request.
The middleware collects information about the request, such as the HTTP method, status code,
The middleware collects information about the request,
such as the HTTP method, status code,
user agent, and source, and sends this data to the Plausible Analytics API.
In case of any client-side or server-side errors, no event is sent,
@ -51,8 +53,10 @@ class PlausibleAnalytics:
"props": {
"method": request.method,
"statusCode": response.status_code,
"browser": f"{user_agent_parsed.browser.family} {user_agent_parsed.browser.version_string}",
"os": f"{user_agent_parsed.os.family} {user_agent_parsed.os.version_string}",
"browser": f"{user_agent_parsed.browser.family}" \
f"{user_agent_parsed.browser.version_string}",
"os": f"{user_agent_parsed.os.family}" \
f"{user_agent_parsed.os.version_string}",
"source": request.headers.get('referer', 'direct'),
},
}

View file

@ -2,10 +2,13 @@
This module contains routes for serving wallpapers using FastAPI.
Routes:
- `/wallpaper/today`: Fetches the wallpaper of the day and renders it using a template.
- `/wallpaper/{day}`: Fetches a specific wallpaper by day and renders it using a template.
- `/wallpaper/today`: Fetches the wallpaper of the day
and renders it using a template.
- `/wallpaper/{day}`: Fetches a specific wallpaper by day
and renders it using a template.
In case of any client or server errors or empty data, a 404 error page is returned.
In case of any client or server errors or empty data,
a 404 error page is returned.
"""
from http import HTTPStatus
@ -34,8 +37,12 @@ async def today_wallpaper(request: Request):
"""
res = requests.get('https://api.starlio.space/last', timeout=3)
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)
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,
@ -48,7 +55,8 @@ async def today_wallpaper(request: Request):
async def wallpaper(request: Request, day):
"""
Fetches a specific wallpaper by day and renders it using a template.
Returns a 404 page in case of client or server errors or if the wallpaper data is empty.
Returns a 404 page in case of client or server errors \
or if the wallpaper data is empty.
Args:
request: The FastAPI request object.
@ -63,7 +71,10 @@ async def wallpaper(request: Request, day):
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 FileResponse(
'./src/web/html/error/404.html',
status_code=HTTPStatus.NOT_FOUND
)
return template.TemplateResponse(
request,