From 4f70c6c0e3948181c984d6b0af458840e1364b30 Mon Sep 17 00:00:00 2001 From: Redume Date: Thu, 20 Feb 2025 21:48:44 +0300 Subject: [PATCH] refactor: The long lines were broken into smaller ones --- main.py | 11 ++++++++--- src/middleware/plausible_analytics.py | 12 ++++++++---- src/routes/wallpaper.py | 25 ++++++++++++++++++------- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index e4bcfbc..5f10fb9 100644 --- a/main.py +++ b/main.py @@ -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, diff --git a/src/middleware/plausible_analytics.py b/src/middleware/plausible_analytics.py index b1c384c..6b8ad79 100644 --- a/src/middleware/plausible_analytics.py +++ b/src/middleware/plausible_analytics.py @@ -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'), }, } diff --git a/src/routes/wallpaper.py b/src/routes/wallpaper.py index 3deea5d..5ac0b4d 100644 --- a/src/routes/wallpaper.py +++ b/src/routes/wallpaper.py @@ -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,