import re import requests from typing import Any from config import app def humanize_tags_from_json(value: str, default: str) -> str: if value is not "": return ", ".join([re.sub("_\\(.*", "", t) for t in value.split()]) return default def humanize_filesize(value: int) -> str: for unit in ["B", "KiB", "MiB", "GiB"]: if value < 1024 or unit is "GiB": break value /= 1024 return f"{value:.2f} {unit}" def format_rating(value: str) -> str | None: match value: case "g": # Negative Squared Latin Capital Letter G return "🅶" case "s": # Negative Squared Latin Capital Letter S return "🆂" case "q": # Negative Squared Latin Capital Letter Q return "🆀" case "e": # Negative Squared Latin Capital Letter E return "🅴" return None def format_status(data) -> str: is_active = True is_pending = data["is_pending"] is_flagged = data["is_flagged"] is_deleted = data["is_deleted"] is_banned = data["is_banned"] if is_pending | is_flagged | is_deleted: is_active = False status = [] if is_active: status.append("Active") if is_pending: status.append("Pending") if is_flagged: status.append("Flagged") if is_banned: status.append("Banned") return " ".join(status) def get_json(pathname: str) -> Any | None: r = requests.get(f"http://{app.host}/{pathname}.json") if r.status_code != 200: return None return r.json()