File size: 3,969 Bytes
337aa19 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 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
|