File size: 10,764 Bytes
7cb306b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()