Download bot/database.py from saske560/KudoBot: direct link, hf CLI and curl.
- Browser
- Download file 39.9 kB
-
https://huggingface.co/spaces/saske560/KudoBot/resolve/main/bot/database.py
- Command line
-
hf download hf://spaces/saske560/KudoBot/bot/database.py
-
curl -L -o database.py https://huggingface.co/spaces/saske560/KudoBot/resolve/main/bot/database.py
39.9 kB
| from __future__ import annotations | |
| import asyncio | |
| import hashlib | |
| import json | |
| from copy import deepcopy | |
| from datetime import datetime, timedelta, timezone | |
| from pathlib import Path | |
| from typing import Any | |
| from .mongo_store import MongoSnapshotStore | |
| from .permissions import AccessProfile, synthetic_plan | |
| from .utils import iso_now, normalize_text, score_match, today_key, to_item_id | |
| class JsonDatabase: | |
| def __init__( | |
| self, | |
| path: Path, | |
| owner_ids: list[int] | None = None, | |
| storage_chat_ids: list[int] | None = None, | |
| *, | |
| mongodb_uri: str = "", | |
| mongodb_db_name: str = "media_archive_bot", | |
| mongodb_collection: str = "snapshots", | |
| mongodb_server_selection_timeout_ms: int = 5000, | |
| ) -> None: | |
| self.path = path | |
| self.owner_ids = sorted({int(x) for x in (owner_ids or [])}) | |
| self.default_storage_chat_ids = sorted({int(x) for x in (storage_chat_ids or [])}) | |
| self._lock = asyncio.Lock() | |
| self.mongo = MongoSnapshotStore( | |
| mongodb_uri, | |
| mongodb_db_name, | |
| mongodb_collection, | |
| server_selection_timeout_ms=mongodb_server_selection_timeout_ms, | |
| ) | |
| self.path.parent.mkdir(parents=True, exist_ok=True) | |
| if not self.path.exists(): | |
| self._bootstrap() | |
| local_data = self._read_sync() if self.path.exists() else None | |
| merged = self.mongo.merge_prefer_latest(local_data) if self.mongo.enabled else local_data | |
| if merged: | |
| self._write_sync(merged, sync_mongo=False) | |
| if self.mongo.enabled: | |
| self.mongo.save_snapshot(merged) | |
| def _bootstrap(self) -> None: | |
| now = iso_now() | |
| base = { | |
| "meta": {"schema_version": 3, "created_at": now, "updated_at": now}, | |
| "config": { | |
| "maintenance": False, | |
| "storage_chat_ids": self.default_storage_chat_ids, | |
| "not_found_chat_id": None, | |
| "legal_notice": "Usa este bot solo con archivos que te pertenezcan o que tengas derecho a distribuir.", | |
| "ai_enabled": False, | |
| "db_backend": "json+mongo" if self.mongo.enabled else "json", | |
| }, | |
| "plans_catalog": { | |
| "free": { | |
| "name": "Free", | |
| "emoji": "🆓", | |
| "daily_search_limit": 30, | |
| "daily_download_limit": 8, | |
| "favorites_limit": 0, | |
| "priority": 0, | |
| }, | |
| "vip": { | |
| "name": "VIP", | |
| "emoji": "💎", | |
| "daily_search_limit": 200, | |
| "daily_download_limit": 60, | |
| "favorites_limit": 200, | |
| "priority": 1, | |
| }, | |
| "premium": { | |
| "name": "Premium", | |
| "emoji": "👑", | |
| "daily_search_limit": 999999, | |
| "daily_download_limit": 999999, | |
| "favorites_limit": 1000, | |
| "priority": 2, | |
| }, | |
| }, | |
| "admins": self.owner_ids, | |
| "users": {}, | |
| "library": {}, | |
| "requests": [], | |
| "audit": [], | |
| } | |
| self._write_sync(base) | |
| def _normalize_sync(self, data: dict[str, Any]) -> dict[str, Any]: | |
| now_iso = iso_now() | |
| data.setdefault("meta", {}) | |
| data["meta"].setdefault("schema_version", 3) | |
| data["meta"].setdefault("created_at", now_iso) | |
| data["meta"].setdefault("updated_at", now_iso) | |
| config = data.setdefault("config", {}) | |
| config.setdefault("maintenance", False) | |
| config.setdefault("storage_chat_ids", []) | |
| config.setdefault("not_found_chat_id", None) | |
| config.setdefault("ai_enabled", False) | |
| config.setdefault("db_backend", "json+mongo" if self.mongo.enabled else "json") | |
| storage_ids = {int(x) for x in config.get("storage_chat_ids", [])} | set(self.default_storage_chat_ids) | |
| config["storage_chat_ids"] = sorted(storage_ids) | |
| if config.get("not_found_chat_id") not in (None, ""): | |
| config["not_found_chat_id"] = int(config["not_found_chat_id"]) | |
| plans = data.setdefault("plans_catalog", {}) | |
| free = plans.setdefault("free", {}) | |
| free.setdefault("name", "Free") | |
| free.setdefault("emoji", "🆓") | |
| free.setdefault("daily_search_limit", 30) | |
| free.setdefault("daily_download_limit", 8) | |
| free.setdefault("favorites_limit", 0) | |
| free.setdefault("priority", 0) | |
| vip = plans.setdefault("vip", {}) | |
| vip.setdefault("name", "VIP") | |
| vip.setdefault("emoji", "💎") | |
| vip.setdefault("daily_search_limit", 200) | |
| vip.setdefault("daily_download_limit", 60) | |
| vip.setdefault("favorites_limit", 200) | |
| vip.setdefault("priority", 1) | |
| premium = plans.setdefault("premium", {}) | |
| premium.setdefault("name", "Premium") | |
| premium.setdefault("emoji", "👑") | |
| premium.setdefault("daily_search_limit", 999999) | |
| premium.setdefault("daily_download_limit", 999999) | |
| premium.setdefault("favorites_limit", 1000) | |
| premium.setdefault("priority", 2) | |
| data.setdefault("users", {}) | |
| data.setdefault("library", {}) | |
| data.setdefault("requests", []) | |
| requests = [] | |
| for req in data.get("requests", []): | |
| if not isinstance(req, dict): | |
| continue | |
| req.setdefault("id", hashlib.sha1(f"{req.get('query', '')}:{req.get('created_at', now_iso)}".encode("utf-8")).hexdigest()[:10]) | |
| req.setdefault("query", "") | |
| req.setdefault("normalized_query", normalize_text(req.get("query"))) | |
| req.setdefault("status", "open") | |
| req.setdefault("count", 1) | |
| req.setdefault("source", "manual") | |
| req.setdefault("created_at", now_iso) | |
| req.setdefault("last_requested_at", req.get("created_at", now_iso)) | |
| requesters = [] | |
| seen_requesters = set() | |
| for requester in req.get("requesters", []): | |
| if not isinstance(requester, dict): | |
| continue | |
| rid = int(requester.get("id") or 0) | |
| if rid and rid in seen_requesters: | |
| continue | |
| if rid: | |
| seen_requesters.add(rid) | |
| requester["id"] = rid | |
| requester.setdefault("first_name", "") | |
| requester.setdefault("username", "") | |
| requester.setdefault("requested_at", req.get("created_at", now_iso)) | |
| requesters.append(requester) | |
| req["requesters"] = requesters | |
| requests.append(req) | |
| data["requests"] = requests[-5000:] | |
| admins = {int(x) for x in data.get("admins", [])} | set(self.owner_ids) | |
| data["admins"] = sorted(admins) | |
| now = datetime.now(timezone.utc) | |
| for record in data["users"].values(): | |
| record.setdefault("favorites", []) | |
| record.setdefault("stats", {}) | |
| stats = record["stats"] | |
| stats.setdefault("searches_total", 0) | |
| stats.setdefault("downloads_total", 0) | |
| stats.setdefault("searches_today", 0) | |
| stats.setdefault("downloads_today", 0) | |
| stats.setdefault("last_search_date", today_key()) | |
| stats.setdefault("last_download_date", today_key()) | |
| record.setdefault("downloads", []) | |
| record.setdefault("plan", "free") | |
| record.setdefault("plan_expires_at", None) | |
| record.setdefault("notes", "") | |
| expires = record.get("plan_expires_at") | |
| if expires: | |
| try: | |
| expires_dt = datetime.fromisoformat(expires) | |
| if expires_dt.tzinfo is None: | |
| expires_dt = expires_dt.replace(tzinfo=timezone.utc) | |
| if expires_dt <= now: | |
| record["plan"] = "free" | |
| record["plan_expires_at"] = None | |
| except ValueError: | |
| record["plan_expires_at"] = None | |
| return data | |
| def _read_sync(self) -> dict[str, Any]: | |
| if not self.path.exists(): | |
| self._bootstrap() | |
| with self.path.open("r", encoding="utf-8") as handle: | |
| data = json.load(handle) | |
| return self._normalize_sync(data) | |
| def _write_sync(self, data: dict[str, Any], *, sync_mongo: bool = True) -> None: | |
| data["meta"]["updated_at"] = iso_now() | |
| temp = self.path.with_suffix(".tmp") | |
| with temp.open("w", encoding="utf-8") as handle: | |
| json.dump(data, handle, ensure_ascii=False, indent=2) | |
| temp.replace(self.path) | |
| if sync_mongo and self.mongo.enabled: | |
| self.mongo.save_snapshot(data) | |
| async def read(self) -> dict[str, Any]: | |
| async with self._lock: | |
| return deepcopy(self._read_sync()) | |
| async def write(self, data: dict[str, Any]) -> None: | |
| async with self._lock: | |
| self._write_sync(data) | |
| async def mutate(self, callback): | |
| async with self._lock: | |
| data = self._normalize_sync(self._read_sync()) | |
| result = callback(data) | |
| self._write_sync(data) | |
| return result | |
| async def get_backend_info(self) -> dict[str, Any]: | |
| return { | |
| "json_path": str(self.path), | |
| "mongo": self.mongo.info(), | |
| } | |
| async def ensure_user(self, user) -> dict[str, Any]: | |
| user_id = str(user.id) | |
| def _mut(data: dict[str, Any]): | |
| users = data.setdefault("users", {}) | |
| if user_id not in users: | |
| users[user_id] = { | |
| "id": user.id, | |
| "first_name": user.first_name or "", | |
| "last_name": user.last_name or "", | |
| "username": user.username or "", | |
| "joined_at": iso_now(), | |
| "last_seen_at": iso_now(), | |
| "plan": "free", | |
| "plan_expires_at": None, | |
| "banned": False, | |
| "notes": "", | |
| "favorites": [], | |
| "downloads": [], | |
| "stats": { | |
| "searches_total": 0, | |
| "downloads_total": 0, | |
| "searches_today": 0, | |
| "downloads_today": 0, | |
| "last_search_date": today_key(), | |
| "last_download_date": today_key(), | |
| }, | |
| } | |
| record = users[user_id] | |
| record["first_name"] = user.first_name or record.get("first_name", "") | |
| record["last_name"] = user.last_name or record.get("last_name", "") | |
| record["username"] = user.username or record.get("username", "") | |
| record["last_seen_at"] = iso_now() | |
| return deepcopy(record) | |
| return await self.mutate(_mut) | |
| async def get_user(self, user_id: int) -> dict[str, Any] | None: | |
| data = await self.read() | |
| user = data.get("users", {}).get(str(user_id)) | |
| return deepcopy(user) if user else None | |
| async def is_owner(self, user_id: int) -> bool: | |
| return int(user_id) in set(self.owner_ids) | |
| async def is_admin(self, user_id: int) -> bool: | |
| data = await self.read() | |
| return int(user_id) in {int(x) for x in data.get("admins", [])} or int(user_id) in self.owner_ids | |
| async def is_banned(self, user_id: int) -> bool: | |
| record = await self.get_user(user_id) | |
| return bool(record and record.get("banned")) | |
| async def get_maintenance(self) -> bool: | |
| data = await self.read() | |
| return bool(data.get("config", {}).get("maintenance", False)) | |
| async def set_maintenance(self, actor_id: int, value: bool) -> None: | |
| def _mut(data: dict[str, Any]): | |
| data.setdefault("config", {})["maintenance"] = value | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "set_maintenance", {"value": value}) | |
| async def list_admin_ids(self) -> list[int]: | |
| data = await self.read() | |
| return sorted({int(x) for x in data.get("admins", [])} | set(self.owner_ids)) | |
| async def add_admin(self, actor_id: int, target_id: int) -> None: | |
| if actor_id not in self.owner_ids: | |
| raise PermissionError("Solo el owner puede agregar admins.") | |
| def _mut(data: dict[str, Any]): | |
| admins = {int(x) for x in data.get("admins", [])} | |
| admins.add(int(target_id)) | |
| data["admins"] = sorted(admins | set(self.owner_ids)) | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "add_admin", {"target_id": target_id}) | |
| async def remove_admin(self, actor_id: int, target_id: int) -> None: | |
| if actor_id not in self.owner_ids: | |
| raise PermissionError("Solo el owner puede quitar admins.") | |
| if int(target_id) in self.owner_ids: | |
| raise PermissionError("No se puede quitar un owner.") | |
| def _mut(data: dict[str, Any]): | |
| admins = {int(x) for x in data.get("admins", [])} | |
| admins.discard(int(target_id)) | |
| data["admins"] = sorted(admins | set(self.owner_ids)) | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "remove_admin", {"target_id": target_id}) | |
| async def set_ban(self, actor_id: int, target_id: int, value: bool) -> None: | |
| def _mut(data: dict[str, Any]): | |
| user = data.get("users", {}).get(str(target_id)) | |
| if not user: | |
| raise KeyError("Usuario no encontrado en la base") | |
| user["banned"] = value | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "set_ban", {"target_id": target_id, "value": value}) | |
| async def get_plan_catalog(self) -> dict[str, Any]: | |
| data = await self.read() | |
| return deepcopy(data.get("plans_catalog", {})) | |
| async def create_plan(self, actor_id: int, key: str, name: str, daily_search_limit: int, daily_download_limit: int, favorites_limit: int, emoji: str = "⭐") -> None: | |
| key = normalize_text(key).replace(" ", "_") | |
| if not key: | |
| raise ValueError("Clave de plan inválida.") | |
| def _mut(data: dict[str, Any]): | |
| plans = data.setdefault("plans_catalog", {}) | |
| priority = max((int(v.get("priority", 0)) for v in plans.values()), default=0) + 1 | |
| plans[key] = { | |
| "name": name, | |
| "emoji": emoji, | |
| "daily_search_limit": int(daily_search_limit), | |
| "daily_download_limit": int(daily_download_limit), | |
| "favorites_limit": int(favorites_limit), | |
| "priority": priority, | |
| } | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "create_plan", {"plan": key}) | |
| async def delete_plan(self, actor_id: int, key: str) -> None: | |
| key = normalize_text(key).replace(" ", "_") | |
| if key in {"free", "vip", "premium"}: | |
| raise PermissionError("No puedes borrar planes base.") | |
| def _mut(data: dict[str, Any]): | |
| plans = data.setdefault("plans_catalog", {}) | |
| if key not in plans: | |
| raise KeyError("Plan no existe") | |
| del plans[key] | |
| for user in data.get("users", {}).values(): | |
| if user.get("plan") == key: | |
| user["plan"] = "free" | |
| user["plan_expires_at"] = None | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "delete_plan", {"plan": key}) | |
| async def get_access_profile(self, user_id: int) -> AccessProfile: | |
| data = await self.read() | |
| user = data.get("users", {}).get(str(user_id)) | |
| plans = data.get("plans_catalog", {}) | |
| admins = {int(x) for x in data.get("admins", [])} | set(self.owner_ids) | |
| if int(user_id) in self.owner_ids: | |
| plan = synthetic_plan("owner") | |
| return AccessProfile( | |
| role="owner", | |
| plan_key="owner", | |
| plan=plan, | |
| unlimited=True, | |
| protect_content=False, | |
| can_favorites=True, | |
| can_admin=True, | |
| ) | |
| if int(user_id) in admins: | |
| plan = synthetic_plan("admin") | |
| return AccessProfile( | |
| role="admin", | |
| plan_key="admin", | |
| plan=plan, | |
| unlimited=True, | |
| protect_content=False, | |
| can_favorites=True, | |
| can_admin=True, | |
| ) | |
| plan_key = (user or {}).get("plan", "free") | |
| plan = deepcopy(plans.get(plan_key, plans.get("free", {}))) | |
| return AccessProfile( | |
| role="user", | |
| plan_key=plan_key, | |
| plan=plan, | |
| unlimited=False, | |
| protect_content=(plan_key == "free"), | |
| can_favorites=(plan_key != "free"), | |
| can_admin=False, | |
| ) | |
| async def get_user_plan_data(self, user_id: int) -> tuple[str, dict[str, Any], dict[str, Any] | None]: | |
| data = await self.read() | |
| user = data.get("users", {}).get(str(user_id)) | |
| access = await self.get_access_profile(user_id) | |
| return access.plan_key, deepcopy(access.plan), deepcopy(user) if user else None | |
| async def can_use_favorites(self, user_id: int) -> bool: | |
| access = await self.get_access_profile(user_id) | |
| return access.can_favorites | |
| async def set_user_plan(self, actor_id: int, target_id: int, plan_key: str, days: int | None = None) -> None: | |
| plan_key = normalize_text(plan_key).replace(" ", "_") | |
| def _mut(data: dict[str, Any]): | |
| plans = data.setdefault("plans_catalog", {}) | |
| if plan_key not in plans: | |
| raise KeyError("Plan no existe") | |
| users = data.setdefault("users", {}) | |
| user = users.get(str(target_id)) | |
| if not user: | |
| raise KeyError("Usuario no encontrado en la base") | |
| user["plan"] = plan_key | |
| if days and days > 0: | |
| user["plan_expires_at"] = (datetime.now(timezone.utc) + timedelta(days=days)).isoformat() | |
| else: | |
| user["plan_expires_at"] = None | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "set_plan", {"target_id": target_id, "plan": plan_key, "days": days}) | |
| async def _daily_allowance(self, user_id: int, kind: str) -> tuple[bool, int, int]: | |
| data = await self.read() | |
| user = data.get("users", {}).get(str(user_id), {}) | |
| access = await self.get_access_profile(user_id) | |
| plan = access.plan | |
| stats = user.get("stats", {}) | |
| today = today_key() | |
| if kind == "search": | |
| last_key = "last_search_date" | |
| today_key_name = "searches_today" | |
| limit_key = "daily_search_limit" | |
| else: | |
| last_key = "last_download_date" | |
| today_key_name = "downloads_today" | |
| limit_key = "daily_download_limit" | |
| count = int(stats.get(today_key_name, 0)) if stats.get(last_key) == today else 0 | |
| limit = int(plan.get(limit_key, 0)) | |
| if access.unlimited: | |
| return True, count, limit | |
| return count < limit, count, limit | |
| async def can_search(self, user_id: int) -> tuple[bool, int, int]: | |
| return await self._daily_allowance(user_id, "search") | |
| async def can_download(self, user_id: int) -> tuple[bool, int, int]: | |
| return await self._daily_allowance(user_id, "download") | |
| async def _increment_counter(self, user_id: int, kind: str) -> None: | |
| user_id_str = str(user_id) | |
| def _mut(data: dict[str, Any]): | |
| user = data["users"][user_id_str] | |
| stats = user.setdefault("stats", {}) | |
| today = today_key() | |
| last_key = f"last_{kind}_date" | |
| if kind == "search": | |
| today_key_name = "searches_today" | |
| total_key = "searches_total" | |
| else: | |
| today_key_name = "downloads_today" | |
| total_key = "downloads_total" | |
| if stats.get(last_key) != today: | |
| stats[today_key_name] = 0 | |
| stats[last_key] = today | |
| stats[total_key] = int(stats.get(total_key, 0)) + 1 | |
| stats[today_key_name] = int(stats.get(today_key_name, 0)) + 1 | |
| await self.mutate(_mut) | |
| async def increment_search(self, user_id: int) -> None: | |
| await self._increment_counter(user_id, "search") | |
| async def increment_download(self, user_id: int) -> None: | |
| await self._increment_counter(user_id, "download") | |
| async def toggle_favorite(self, user_id: int, item_id: str) -> tuple[str, int]: | |
| def _mut(data: dict[str, Any]): | |
| user = data.get("users", {}).get(str(user_id)) | |
| if not user: | |
| raise KeyError("Usuario no encontrado") | |
| favorites = user.setdefault("favorites", []) | |
| if item_id in favorites: | |
| favorites.remove(item_id) | |
| return "removed", len(favorites) | |
| plan_key = user.get("plan", "free") | |
| if plan_key == "free": | |
| return "upgrade", len(favorites) | |
| plans = data.get("plans_catalog", {}) | |
| plan = plans.get(plan_key, plans.get("free", {})) | |
| limit = int(plan.get("favorites_limit", 25)) | |
| if len(favorites) >= limit: | |
| return "limit", len(favorites) | |
| favorites.append(item_id) | |
| return "added", len(favorites) | |
| return await self.mutate(_mut) | |
| async def get_favorites(self, user_id: int) -> list[dict[str, Any]]: | |
| data = await self.read() | |
| user = data.get("users", {}).get(str(user_id), {}) | |
| library = data.get("library", {}) | |
| results = [] | |
| for item_id in user.get("favorites", []): | |
| item = library.get(item_id) | |
| if item: | |
| results.append(deepcopy(item)) | |
| return results | |
| async def add_download_log(self, user_id: int, item_id: str) -> None: | |
| def _mut(data: dict[str, Any]): | |
| user = data.get("users", {}).get(str(user_id)) | |
| if not user: | |
| return | |
| downloads = user.setdefault("downloads", []) | |
| downloads.append({"item_id": item_id, "at": iso_now()}) | |
| if len(downloads) > 50: | |
| del downloads[:-50] | |
| await self.mutate(_mut) | |
| async def get_recent_downloads(self, user_id: int) -> list[dict[str, Any]]: | |
| data = await self.read() | |
| user = data.get("users", {}).get(str(user_id), {}) | |
| library = data.get("library", {}) | |
| results = [] | |
| for event in reversed(user.get("downloads", [])[-20:]): | |
| item = library.get(event.get("item_id")) | |
| if item: | |
| enriched = deepcopy(item) | |
| enriched["downloaded_at"] = event.get("at") | |
| results.append(enriched) | |
| return results | |
| async def export_copy(self, export_dir: Path) -> Path: | |
| export_dir.mkdir(parents=True, exist_ok=True) | |
| target = export_dir / f"database_export_{today_key()}.json" | |
| data = await self.read() | |
| with target.open("w", encoding="utf-8") as handle: | |
| json.dump(data, handle, ensure_ascii=False, indent=2) | |
| return target | |
| async def import_from_path(self, path: Path, actor_id: int) -> None: | |
| with path.open("r", encoding="utf-8") as handle: | |
| incoming = json.load(handle) | |
| incoming = self._normalize_sync(incoming) | |
| incoming["admins"] = sorted({int(x) for x in incoming.get("admins", [])} | set(self.owner_ids)) | |
| incoming.setdefault("config", {}).setdefault("storage_chat_ids", []) | |
| incoming["config"]["storage_chat_ids"] = sorted( | |
| {int(x) for x in incoming["config"].get("storage_chat_ids", [])} | set(self.default_storage_chat_ids) | |
| ) | |
| await self.write(incoming) | |
| await self.add_audit(actor_id, "import_db", {"path": str(path)}) | |
| async def get_storage_chat_ids(self) -> list[int]: | |
| data = await self.read() | |
| return [int(x) for x in data.get("config", {}).get("storage_chat_ids", [])] | |
| async def add_storage_chat(self, actor_id: int, chat_id: int) -> None: | |
| def _mut(data: dict[str, Any]): | |
| config = data.setdefault("config", {}) | |
| values = {int(x) for x in config.get("storage_chat_ids", [])} | |
| values.add(int(chat_id)) | |
| config["storage_chat_ids"] = sorted(values) | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "add_storage_chat", {"chat_id": chat_id}) | |
| async def remove_storage_chat(self, actor_id: int, chat_id: int) -> None: | |
| if int(chat_id) in self.default_storage_chat_ids: | |
| raise PermissionError("Ese chat viene fijado por entorno y no se puede quitar desde el bot.") | |
| def _mut(data: dict[str, Any]): | |
| config = data.setdefault("config", {}) | |
| values = {int(x) for x in config.get("storage_chat_ids", [])} | |
| values.discard(int(chat_id)) | |
| config["storage_chat_ids"] = sorted(values) | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "remove_storage_chat", {"chat_id": chat_id}) | |
| async def get_not_found_chat_id(self) -> int | None: | |
| data = await self.read() | |
| value = data.get("config", {}).get("not_found_chat_id") | |
| return int(value) if value not in (None, "") else None | |
| async def set_not_found_chat(self, actor_id: int, chat_id: int | None) -> None: | |
| def _mut(data: dict[str, Any]): | |
| config = data.setdefault("config", {}) | |
| config["not_found_chat_id"] = int(chat_id) if chat_id not in (None, "") else None | |
| await self.mutate(_mut) | |
| await self.add_audit(actor_id, "set_not_found_chat", {"chat_id": chat_id}) | |
| async def upsert_library_item(self, actor_id: int | None, item: dict[str, Any]) -> dict[str, Any]: | |
| item = deepcopy(item) | |
| item_id = to_item_id(int(item["chat_id"]), int(item["message_id"])) | |
| item["item_id"] = item_id | |
| item["indexed_at"] = iso_now() | |
| item["search_blob"] = item.get("search_blob") or "" | |
| item["normalized_title"] = item.get("normalized_title") or normalize_text(item.get("title")) | |
| def _mut(data: dict[str, Any]): | |
| library = data.setdefault("library", {}) | |
| item["created_at"] = library.get(item_id, {}).get("created_at", iso_now()) | |
| library[item_id] = item | |
| return deepcopy(item) | |
| stored = await self.mutate(_mut) | |
| if actor_id is not None: | |
| await self.add_audit(actor_id, "upsert_library_item", {"item_id": item_id, "title": item.get("title")}) | |
| return stored | |
| async def remove_library_item(self, actor_id: int | None, chat_id: int, message_id: int) -> None: | |
| item_id = to_item_id(chat_id, message_id) | |
| def _mut(data: dict[str, Any]): | |
| data.setdefault("library", {}).pop(item_id, None) | |
| for user in data.get("users", {}).values(): | |
| if item_id in user.get("favorites", []): | |
| user["favorites"] = [x for x in user.get("favorites", []) if x != item_id] | |
| await self.mutate(_mut) | |
| if actor_id is not None: | |
| await self.add_audit(actor_id, "remove_library_item", {"item_id": item_id}) | |
| async def get_library_item(self, item_id: str) -> dict[str, Any] | None: | |
| data = await self.read() | |
| item = data.get("library", {}).get(item_id) | |
| return deepcopy(item) if item else None | |
| async def list_library_items(self) -> list[dict[str, Any]]: | |
| data = await self.read() | |
| items = list(data.get("library", {}).values()) | |
| return sorted((deepcopy(x) for x in items), key=lambda item: (item.get("normalized_title", ""), item.get("season") or 0, item.get("episode") or 0, item.get("part") or 0)) | |
| async def search_library(self, query: str, limit: int = 80) -> list[dict[str, Any]]: | |
| data = await self.read() | |
| items = list(data.get("library", {}).values()) | |
| ranked = [] | |
| for item in items: | |
| score = score_match(query, item.get("search_blob", ""), item.get("title", "")) | |
| if score > 20: | |
| ranked.append((score, item)) | |
| ranked.sort(key=lambda pair: (pair[0], pair[1].get("indexed_at", "")), reverse=True) | |
| return [deepcopy(item) for _, item in ranked[:limit]] | |
| def _group_key_for_item(self, item: dict[str, Any]) -> str: | |
| return "|".join([ | |
| item.get("content_type") or "movie", | |
| item.get("category") or item.get("content_type") or "movie", | |
| item.get("normalized_title") or normalize_text(item.get("title")), | |
| str(item.get("year") or ""), | |
| ]) | |
| def _group_items(self, items: list[dict[str, Any]], *, score_query: str | None = None) -> list[dict[str, Any]]: | |
| grouped: dict[str, dict[str, Any]] = {} | |
| for item in items: | |
| group_key = self._group_key_for_item(item) | |
| bucket = grouped.setdefault(group_key, { | |
| "group_key": group_key, | |
| "title": item.get("title") or "Sin título", | |
| "normalized_title": item.get("normalized_title") or normalize_text(item.get("title")), | |
| "content_type": item.get("content_type") or "movie", | |
| "category": item.get("category") or item.get("content_type") or "movie", | |
| "year": item.get("year"), | |
| "quality": item.get("quality"), | |
| "language": item.get("language"), | |
| "language_flag": item.get("language_flag"), | |
| "items": [], | |
| "best_score": 0.0, | |
| "total_items": 0, | |
| "seasons": set(), | |
| "episodes_count": 0, | |
| "parts": set(), | |
| "total_size": 0, | |
| "downloads_total": 0, | |
| }) | |
| score = score_match(score_query or item.get("title", ""), item.get("search_blob", ""), item.get("title", "")) if score_query else 0.0 | |
| bucket["best_score"] = max(bucket["best_score"], score) | |
| bucket["items"].append(item) | |
| bucket["total_items"] += 1 | |
| bucket["total_size"] += int(item.get("file_size") or 0) | |
| bucket["downloads_total"] += int(item.get("downloads_count") or 0) | |
| if item.get("season") is not None: | |
| bucket["seasons"].add(int(item.get("season") or 1)) | |
| if item.get("episode") is not None: | |
| bucket["episodes_count"] += 1 | |
| if item.get("part") is not None: | |
| bucket["parts"].add(int(item["part"])) | |
| groups = [] | |
| for bucket in grouped.values(): | |
| bucket["items"].sort(key=lambda it: ( | |
| int(it.get("season") or 0), | |
| int(it.get("episode") or 0), | |
| int(it.get("part") or 0), | |
| it.get("indexed_at", ""), | |
| )) | |
| bucket["seasons"] = sorted(bucket["seasons"]) | |
| bucket["parts"] = sorted(bucket["parts"]) | |
| groups.append(bucket) | |
| return groups | |
| async def search_library_groups(self, query: str, limit_items: int = 160, limit_groups: int = 40) -> list[dict[str, Any]]: | |
| results = await self.search_library(query, limit=limit_items) | |
| groups = self._group_items(results, score_query=query) | |
| groups.sort(key=lambda group: (group["best_score"], group["total_items"]), reverse=True) | |
| return [deepcopy(group) for group in groups[:limit_groups]] | |
| async def catalog_groups(self, kind: str = "all", limit_groups: int = 500) -> list[dict[str, Any]]: | |
| data = await self.read() | |
| items = list(data.get("library", {}).values()) | |
| kind = normalize_text(kind or "all") | |
| filtered: list[dict[str, Any]] = [] | |
| for item in items: | |
| category = item.get("category") or item.get("content_type") or "movie" | |
| content_type = item.get("content_type") or "movie" | |
| if kind in {"all", "catalogo"}: | |
| pass | |
| elif kind in {"movies", "peliculas", "pelicula", "movie"} and content_type != "movie": | |
| continue | |
| elif kind in {"series", "serie"} and content_type != "series": | |
| continue | |
| elif kind == "anime" and category != "anime": | |
| continue | |
| filtered.append(item) | |
| groups = self._group_items(filtered) | |
| groups.sort(key=lambda group: (group.get("normalized_title", ""), group.get("year") or 0)) | |
| return [deepcopy(group) for group in groups[:limit_groups]] | |
| async def get_group_items(self, group_key: str) -> list[dict[str, Any]]: | |
| data = await self.read() | |
| library = data.get("library", {}) | |
| parts = group_key.split("|", 3) | |
| while len(parts) < 4: | |
| parts.append("") | |
| content_type, category, normalized_title, year_text = parts | |
| year_value = int(year_text) if year_text.isdigit() else None | |
| results = [] | |
| for item in library.values(): | |
| if (item.get("content_type") or "movie") != content_type: | |
| continue | |
| if (item.get("category") or item.get("content_type") or "movie") != category: | |
| continue | |
| if (item.get("normalized_title") or normalize_text(item.get("title"))) != normalized_title: | |
| continue | |
| if (item.get("year") or None) != year_value: | |
| continue | |
| results.append(deepcopy(item)) | |
| results.sort(key=lambda it: ( | |
| int(it.get("season") or 0), | |
| int(it.get("episode") or 0), | |
| int(it.get("part") or 0), | |
| it.get("indexed_at", ""), | |
| )) | |
| return results | |
| async def bump_item_downloads(self, item_id: str, amount: int = 1) -> None: | |
| def _mut(data: dict[str, Any]): | |
| item = data.get("library", {}).get(item_id) | |
| if not item: | |
| return | |
| item["downloads_count"] = int(item.get("downloads_count") or 0) + int(amount) | |
| await self.mutate(_mut) | |
| async def add_missing_request(self, user, query: str, *, source: str = "manual") -> dict[str, Any]: | |
| query = " ".join((query or "").split()).strip() | |
| if not query: | |
| raise ValueError("Petición vacía") | |
| normalized = normalize_text(query) | |
| if not normalized: | |
| raise ValueError("Petición inválida") | |
| def _mut(data: dict[str, Any]): | |
| requests = data.setdefault("requests", []) | |
| now_iso = iso_now() | |
| requester = { | |
| "id": int(user.id), | |
| "first_name": user.first_name or "", | |
| "username": user.username or "", | |
| "requested_at": now_iso, | |
| } | |
| existing = None | |
| for req in requests: | |
| if req.get("normalized_query") == normalized and req.get("status", "open") == "open": | |
| existing = req | |
| break | |
| if existing is None: | |
| request_id = hashlib.sha1(f"{normalized}:{now_iso}".encode("utf-8")).hexdigest()[:10] | |
| existing = { | |
| "id": request_id, | |
| "query": query, | |
| "normalized_query": normalized, | |
| "status": "open", | |
| "count": 0, | |
| "source": source, | |
| "created_at": now_iso, | |
| "last_requested_at": now_iso, | |
| "requesters": [], | |
| } | |
| requests.append(existing) | |
| existing["last_requested_at"] = now_iso | |
| existing["count"] = int(existing.get("count") or 0) + 1 | |
| requesters = existing.setdefault("requesters", []) | |
| requester_ids = {int(item.get("id") or 0) for item in requesters if isinstance(item, dict)} | |
| if int(user.id) not in requester_ids: | |
| requesters.append(requester) | |
| else: | |
| for item in requesters: | |
| if int(item.get("id") or 0) == int(user.id): | |
| item["requested_at"] = now_iso | |
| item["first_name"] = user.first_name or item.get("first_name", "") | |
| item["username"] = user.username or item.get("username", "") | |
| break | |
| if len(requests) > 5000: | |
| del requests[:-5000] | |
| return deepcopy(existing) | |
| request = await self.mutate(_mut) | |
| await self.add_audit(int(user.id), "add_missing_request", {"request_id": request.get("id"), "query": query, "source": source}) | |
| return request | |
| async def list_recent_requests(self, limit: int = 20) -> list[dict[str, Any]]: | |
| data = await self.read() | |
| requests = list(data.get("requests", [])) | |
| requests.sort(key=lambda item: item.get("last_requested_at", item.get("created_at", "")), reverse=True) | |
| return [deepcopy(item) for item in requests[:limit]] | |
| async def get_stats(self) -> dict[str, Any]: | |
| data = await self.read() | |
| users = data.get("users", {}) | |
| library = data.get("library", {}) | |
| requests = data.get("requests", []) | |
| requests_open = sum(1 for req in requests if req.get("status", "open") == "open") | |
| plans_count: dict[str, int] = {} | |
| searches = 0 | |
| downloads = 0 | |
| banned = 0 | |
| admins_set = {int(x) for x in data.get("admins", [])} | set(self.owner_ids) | |
| for user_id_str, user in users.items(): | |
| user_id = int(user_id_str) | |
| if user_id in self.owner_ids: | |
| plan_key = "owner" | |
| elif user_id in admins_set: | |
| plan_key = "admin" | |
| else: | |
| plan_key = user.get("plan", "free") | |
| plans_count[plan_key] = plans_count.get(plan_key, 0) + 1 | |
| stats = user.get("stats", {}) | |
| searches += int(stats.get("searches_total", 0)) | |
| downloads += int(stats.get("downloads_total", 0)) | |
| banned += 1 if user.get("banned") else 0 | |
| return { | |
| "users": len(users), | |
| "admins": len(set(data.get("admins", [])) | set(self.owner_ids)), | |
| "banned": banned, | |
| "library_items": len(library), | |
| "storage_chats": len(data.get("config", {}).get("storage_chat_ids", [])), | |
| "not_found_chat_id": data.get("config", {}).get("not_found_chat_id"), | |
| "searches": searches, | |
| "downloads": downloads, | |
| "requests_total": len(requests), | |
| "requests_open": requests_open, | |
| "maintenance": bool(data.get("config", {}).get("maintenance", False)), | |
| "plans": plans_count, | |
| "ai_enabled": bool(data.get("config", {}).get("ai_enabled", False)), | |
| "db_backend": data.get("config", {}).get("db_backend", "json+mongo" if self.mongo.enabled else "json"), | |
| "mongo_enabled": self.mongo.enabled, | |
| "mongo_ready": self.mongo.ready, | |
| "mongo_last_error": self.mongo.last_error, | |
| } | |
| async def add_audit(self, actor_id: int, action: str, payload: dict[str, Any] | None = None) -> None: | |
| payload = payload or {} | |
| def _mut(data: dict[str, Any]): | |
| audit = data.setdefault("audit", []) | |
| audit.append({"at": iso_now(), "actor_id": actor_id, "action": action, "payload": payload}) | |
| if len(audit) > 1000: | |
| del audit[:-1000] | |
| await self.mutate(_mut) | |