refactor: add English docstrings to all functions

This commit is contained in:
Данил 2025-02-20 21:38:11 +03:00
parent 06ca56f019
commit cabcccc0fb
4 changed files with 82 additions and 9 deletions

11
main.py
View file

@ -1,3 +1,14 @@
"""
This module defines the main FastAPI application, including route handlers,
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.
"""
import http
import uvicorn

View file

@ -1,13 +1,40 @@
"""
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,
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,
and the response is returned as usual.
"""
import httpx
import yaml
from http import HTTPStatus
from user_agents import parse as ua_parse
config = yaml.safe_load(open('./config.yaml'))
class PlausibleAnalytics:
"""
Middleware for sending analytics data to Plausible Analytics
after processing each request.
"""
async def __call__(self, request, call_next):
"""
Called for each request, sends an event to Plausible with
information about the request and user.
Args:
request: FastAPI request object.
call_next: Function to call the next request handler.
Returns:
Response: FastAPI response object.
"""
response = await call_next(request)
user_agent = request.headers.get('user-agent', 'unknown')
@ -29,15 +56,15 @@ class PlausibleAnalytics:
},
}
async with httpx.AsyncClient() as client:
try:
async with httpx.AsyncClient() as client:
await client.post(
config['analytics']['endpoint'],
json=event,
headers={
"Authorization": f"Bearer {config['analytics']['token']}",
"Content-Type": "application/json",
"User-Agent": request.headers.get('user-agent', 'unknown'),
"User-Agent": user_agent,
},
)
except Exception as e:

View file

@ -1,9 +1,13 @@
from fastapi import APIRouter, Request
"""
This module defines API routes for serving static HTML files.
"""
from fastapi import APIRouter
from fastapi.responses import FileResponse
router = APIRouter()
@router.get('/')
async def main():
"""Returns the main HTML page."""
return FileResponse('./src/web/html/index.html')

View file

@ -1,3 +1,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.
In case of any client or server errors or empty data, a 404 error page is returned.
"""
from http import HTTPStatus
import requests
@ -12,6 +22,16 @@ template = Jinja2Templates(directory='./src/web/html')
@router.get('/wallpaper/today', response_class=HTMLResponse)
async def today_wallpaper(request: Request):
"""
Fetches the wallpaper of the day and renders it using a template.
Returns a 404 page in case of client or server errors.
Args:
request: The FastAPI request object.
Returns:
HTMLResponse: The wallpaper page or 404 error page.
"""
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:
@ -26,6 +46,17 @@ async def today_wallpaper(request: Request):
@router.get('/wallpaper/{day}', response_class=HTMLResponse)
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.
Args:
request: The FastAPI request object.
day: The day parameter used to fetch the wallpaper.
Returns:
HTMLResponse: The wallpaper page or 404 error page.
"""
res = requests.get(f'https://api.starlio.space/wallpaper/{day}')
if (HTTPStatus(res.status_code).is_server_error