File size: 39,858 Bytes
337aa19 4a456cc 337aa19 37c550f 4a456cc 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 4a456cc 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 4a456cc 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 4a456cc 337aa19 4a456cc 337aa19 37c550f 4a456cc 37c550f 337aa19 4a456cc 337aa19 4a456cc 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 337aa19 37c550f 4a456cc 337aa19 4a456cc 337aa19 4a456cc 337aa19 37c550f 337aa19 4a456cc 337aa19 37c550f 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 | 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)
|