"""Database module for MongoDB operations.""" import logging from datetime import datetime from typing import Optional, Dict, Any, List from pymongo import MongoClient, ASCENDING, DESCENDING from pymongo.collection import Collection from pymongo.errors import ConfigurationError from config import Config logger = logging.getLogger(__name__) class Database: """MongoDB database handler.""" def __init__(self): self.client = MongoClient(Config.MONGO_URI) # Try to get default database, or use explicit name try: self.db = self.client.get_default_database() logger.info("Using default database from URI") except ConfigurationError: # Fallback to explicit database name logger.info("No default database in URI, using 'anime_encoder_bot'") self.db = self.client["anime_encoder_bot"] except Exception as e: logger.warning(f"Database init error: {e}, using fallback") self.db = self.client["anime_encoder_bot"] # Collections self.anime_cache: Collection = self.db.anime_cache self.users: Collection = self.db.users self.downloads: Collection = self.db.downloads self.encodes: Collection = self.db.encodes # Create indexes self._create_indexes() def _create_indexes(self): """Create necessary indexes.""" self.anime_cache.create_index("anime_id", unique=True) self.anime_cache.create_index("title") self.users.create_index("user_id", unique=True) self.downloads.create_index([("created_at", DESCENDING)]) self.downloads.create_index("status") self.encodes.create_index([("created_at", DESCENDING)]) self.encodes.create_index("status") # Anime Cache def get_cached_anime(self, anime_id: str) -> Optional[Dict]: """Get cached anime by ID.""" return self.anime_cache.find_one({"anime_id": anime_id}) def cache_anime(self, anime_id: str, title: str, episodes: List[Dict], file_id: Optional[str] = None, metadata: Optional[Dict] = None): """Cache anime information.""" self.anime_cache.update_one( {"anime_id": anime_id}, { "$set": { "title": title, "episodes": episodes, "file_id": file_id, "metadata": metadata or {}, "cached_at": datetime.utcnow() } }, upsert=True ) def get_cached_episode(self, anime_id: str, episode_num: int) -> Optional[Dict]: """Get cached episode file_id.""" anime = self.anime_cache.find_one({"anime_id": anime_id}) if anime and "episodes" in anime: for ep in anime["episodes"]: if ep.get("number") == episode_num: return ep return None def update_episode_file_id(self, anime_id: str, episode_num: int, file_id: str): """Update file_id for cached episode.""" self.anime_cache.update_one( {"anime_id": anime_id, "episodes.number": episode_num}, {"$set": {"episodes.$.file_id": file_id, "episodes.$.cached_at": datetime.utcnow()}} ) # User Settings def get_user_settings(self, user_id: int) -> Dict: """Get user encoding settings.""" user = self.users.find_one({"user_id": user_id}) if not user: # Return defaults return { "codec": Config.DEFAULT_CODEC, "crf": Config.DEFAULT_CRF, "preset": Config.DEFAULT_PRESET, "resolution": "original", "audio_codec": Config.DEFAULT_AUDIO_CODEC, "audio_bitrate": Config.DEFAULT_AUDIO_BITRATE, "hardsub": False, "watermark": False, "rename_pattern": Config.DEFAULT_RENAME_PATTERN, "auto_rename": True, "include_metadata": True, "thumb_resolution": "720p", "thumb_format": "jpg", "thumb_caption_template": "šŸ–¼ļø Thumbnail of {filename}", "thumb_position": "25%", "thumb_auto_send": True, "video_caption_template": "šŸŽ¬ {filename}\nšŸ“Š {resolution} | ā±ļø {duration}" } # Ensure rename settings exist even for existing users settings = user.get("settings", {}) if "rename_pattern" not in settings: settings["rename_pattern"] = Config.DEFAULT_RENAME_PATTERN if "auto_rename" not in settings: settings["auto_rename"] = True if "include_metadata" not in settings: settings["include_metadata"] = True # Ensure thumbnail settings exist if "thumb_resolution" not in settings: settings["thumb_resolution"] = "720p" if "thumb_format" not in settings: settings["thumb_format"] = "jpg" if "thumb_caption_template" not in settings: settings["thumb_caption_template"] = "šŸ–¼ļø Thumbnail of {filename}" if "thumb_position" not in settings: settings["thumb_position"] = "25%" if "thumb_auto_send" not in settings: settings["thumb_auto_send"] = True if "video_caption_template" not in settings: settings["video_caption_template"] = "šŸŽ¬ {filename}\nšŸ“Š {resolution} | ā±ļø {duration}" return settings def update_user_settings(self, user_id: int, settings: Dict): """Update user encoding settings.""" self.users.update_one( {"user_id": user_id}, { "$set": { "settings": settings, "updated_at": datetime.utcnow() }, "$setOnInsert": {"user_id": user_id, "created_at": datetime.utcnow()} }, upsert=True ) def reset_user_settings(self, user_id: int): """Reset user settings to defaults.""" self.users.update_one( {"user_id": user_id}, {"$unset": {"settings": ""}} ) # Download Queue def add_download_task(self, task_id: str, user_id: int, anime_id: str, episode_num: int, url: str, status: str = "pending"): """Add download task to queue.""" self.downloads.insert_one({ "task_id": task_id, "user_id": user_id, "anime_id": anime_id, "episode_num": episode_num, "url": url, "status": status, "created_at": datetime.utcnow(), "updated_at": datetime.utcnow() }) def update_download_status(self, task_id: str, status: str, file_path: Optional[str] = None, error: Optional[str] = None): """Update download task status.""" update = {"status": status, "updated_at": datetime.utcnow()} if file_path: update["file_path"] = file_path if error: update["error"] = error self.downloads.update_one({"task_id": task_id}, {"$set": update}) def get_pending_downloads(self) -> List[Dict]: """Get pending download tasks.""" return list(self.downloads.find({"status": "pending"}).sort("created_at", ASCENDING)) def get_download_task(self, task_id: str) -> Optional[Dict]: """Get download task by ID.""" return self.downloads.find_one({"task_id": task_id}) # Encode Queue def add_encode_task(self, task_id: str, user_id: int, input_path: str, output_path: str, settings: Dict, status: str = "pending"): """Add encode task to queue.""" self.encodes.insert_one({ "task_id": task_id, "user_id": user_id, "input_path": input_path, "output_path": output_path, "settings": settings, "status": status, "progress": 0, "created_at": datetime.utcnow(), "updated_at": datetime.utcnow() }) def update_encode_status(self, task_id: str, status: str, progress: Optional[int] = None, file_id: Optional[str] = None, error: Optional[str] = None): """Update encode task status.""" update = {"status": status, "updated_at": datetime.utcnow()} if progress is not None: update["progress"] = progress if file_id: update["file_id"] = file_id if error: update["error"] = error self.encodes.update_one({"task_id": task_id}, {"$set": update}) def get_pending_encodes(self) -> List[Dict]: """Get pending encode tasks.""" return list(self.encodes.find({"status": "pending"}).sort("created_at", ASCENDING)) def get_encode_task(self, task_id: str) -> Optional[Dict]: """Get encode task by ID.""" return self.encodes.find_one({"task_id": task_id}) def get_active_tasks(self) -> Dict[str, int]: """Get count of active downloads and encodes.""" active_downloads = self.downloads.count_documents({"status": {"$in": ["downloading", "processing"]}}) active_encodes = self.encodes.count_documents({"status": {"$in": ["encoding", "processing"]}}) return { "downloads": active_downloads, "encodes": active_encodes } def get_user_tasks(self, user_id: int, limit: int = 10) -> Dict[str, List[Dict]]: """Get recent tasks for a user.""" downloads = list(self.downloads.find( {"user_id": user_id} ).sort("created_at", DESCENDING).limit(limit)) encodes = list(self.encodes.find( {"user_id": user_id} ).sort("created_at", DESCENDING).limit(limit)) return {"downloads": downloads, "encodes": encodes} def cleanup_old_tasks(self, days: int = 7): """Clean up completed/failed tasks older than specified days.""" from datetime import timedelta cutoff = datetime.utcnow() - timedelta(days=days) self.downloads.delete_many({ "status": {"$in": ["completed", "failed", "cancelled"]}, "updated_at": {"$lt": cutoff} }) self.encodes.delete_many({ "status": {"$in": ["completed", "failed", "cancelled"]}, "updated_at": {"$lt": cutoff} }) # Global database instance db = Database()