Download bot/utils.py from saske560/KudoBot: direct link, hf CLI and curl.
- Browser
- Download file 3.97 kB
-
https://huggingface.co/spaces/saske560/KudoBot/resolve/main/bot/utils.py
- Command line
-
hf download hf://spaces/saske560/KudoBot/bot/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/saske560/KudoBot/resolve/main/bot/utils.py
3.97 kB
| from __future__ import annotations | |
| import hashlib | |
| import html | |
| import re | |
| import unicodedata | |
| from datetime import datetime, timezone | |
| from difflib import SequenceMatcher | |
| from typing import Iterable, List | |
| COMMON_STOPWORDS = { | |
| "the", "a", "an", "de", "del", "la", "el", "los", "las", "y", "en", "por", "para", "con", | |
| "temporada", "season", "capitulo", "capítulo", "episodio", "episode", "parte", "part", "sub", | |
| "espanol", "español", "latino", "castellano", "dual", "audio", "pelicula", "movie", "serie", "series", | |
| } | |
| def utc_now() -> datetime: | |
| return datetime.now(timezone.utc) | |
| def iso_now() -> str: | |
| return utc_now().isoformat() | |
| def today_key() -> str: | |
| return utc_now().date().isoformat() | |
| def safe_html(text: str | None) -> str: | |
| return html.escape(text or "") | |
| def normalize_text(text: str | None) -> str: | |
| text = text or "" | |
| text = unicodedata.normalize("NFKD", text) | |
| text = "".join(ch for ch in text if not unicodedata.combining(ch)) | |
| text = text.lower() | |
| text = re.sub(r"\.(mkv|mp4|avi|mov|m4v|ts|webm|srt|ass|mka|zip|rar)$", "", text) | |
| text = re.sub(r"[^a-z0-9]+", " ", text) | |
| text = re.sub(r"\s+", " ", text).strip() | |
| return text | |
| def tokenize(text: str | None) -> List[str]: | |
| norm = normalize_text(text) | |
| tokens = [t for t in norm.split() if t and t not in COMMON_STOPWORDS] | |
| return tokens | |
| def compact_title(text: str | None) -> str: | |
| if not text: | |
| return "Sin título" | |
| clean = re.sub(r"\s+", " ", text).strip(" -_\n\t") | |
| return clean[:1].upper() + clean[1:] if clean else "Sin título" | |
| def short_text(text: str | None, limit: int = 280) -> str: | |
| if not text: | |
| return "" | |
| clean = " ".join((text or "").split()) | |
| return clean if len(clean) <= limit else clean[: limit - 1].rstrip() + "…" | |
| def to_item_id(chat_id: int, message_id: int) -> str: | |
| return hashlib.sha1(f"{chat_id}:{message_id}".encode("utf-8")).hexdigest()[:18] | |
| def chunked(items: Iterable, size: int): | |
| bucket = [] | |
| for item in items: | |
| bucket.append(item) | |
| if len(bucket) >= size: | |
| yield bucket | |
| bucket = [] | |
| if bucket: | |
| yield bucket | |
| def parse_bool(value: str | None) -> bool: | |
| return (value or "").strip().lower() in {"1", "true", "yes", "on", "si", "sí"} | |
| def human_size(value: int | None) -> str: | |
| if not value: | |
| return "0 B" | |
| suffixes = ["B", "KB", "MB", "GB", "TB"] | |
| num = float(value) | |
| idx = 0 | |
| while num >= 1024 and idx < len(suffixes) - 1: | |
| num /= 1024 | |
| idx += 1 | |
| if idx == 0: | |
| return f"{int(num)} {suffixes[idx]}" | |
| return f"{num:.1f} {suffixes[idx]}" | |
| def human_duration(seconds: int | None) -> str: | |
| if not seconds: | |
| return "—" | |
| h, r = divmod(int(seconds), 3600) | |
| m, s = divmod(r, 60) | |
| if h: | |
| return f"{h}:{m:02d}:{s:02d}" | |
| return f"{m}:{s:02d}" | |
| def similarity(a: str, b: str) -> float: | |
| if not a or not b: | |
| return 0.0 | |
| return SequenceMatcher(None, normalize_text(a), normalize_text(b)).ratio() | |
| def score_match(query: str, text: str, title: str) -> float: | |
| q_norm = normalize_text(query) | |
| t_norm = normalize_text(text) | |
| title_norm = normalize_text(title) | |
| q_tokens = set(tokenize(query)) | |
| t_tokens = set(tokenize(text)) | |
| score = 0.0 | |
| if not q_norm: | |
| return 0.0 | |
| if q_norm == title_norm: | |
| score += 200 | |
| if q_norm in title_norm: | |
| score += 120 | |
| if q_norm in t_norm: | |
| score += 80 | |
| common = q_tokens & t_tokens | |
| score += len(common) * 18 | |
| score += similarity(query, title) * 70 | |
| score += similarity(query, text) * 25 | |
| return score | |
| def format_expiry(value: str | None) -> str: | |
| if not value: | |
| return "—" | |
| try: | |
| dt = datetime.fromisoformat(value) | |
| if dt.tzinfo is None: | |
| dt = dt.replace(tzinfo=timezone.utc) | |
| return dt.astimezone().strftime("%d/%m/%Y %H:%M") | |
| except ValueError: | |
| return value | |