forked from Sankar8098/Advanced-File-Store-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
172 lines (141 loc) · 4.88 KB
/
Copy pathdatabase.py
File metadata and controls
172 lines (141 loc) · 4.88 KB
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
from typing import Optional, Dict, Any, List
import logging
from datetime import datetime
from models import db, movies, users, statistics
from bson.objectid import ObjectId
logger = logging.getLogger(__name__)
class URLVerificationError(Exception):
"""Exception for URL verification failures."""
pass
class DatabaseError(Exception):
"""Base exception for database operations."""
pass
def get_db():
"""Get database connection."""
return db
def verify_url_token(url: str) -> Optional[Dict[str, Any]]:
"""
Verify shortened URL and return stream_id if valid.
Args:
url: The URL to verify
Returns:
Dict containing stream_id and verified status, or None if invalid
Raises:
URLVerificationError: If URL verification fails
"""
try:
if not (url.startswith('https://get2short.com/') or
url.startswith('https://modijiurl.com/')):
return None
stream_id = url.split('/')[-1]
movie = movies.find_one({"stream_id": stream_id})
if not movie:
return None
return {
'stream_id': stream_id,
'verified': True
}
except Exception as e:
logger.error(f"Error in verify_url_token with URL {url}: {e}")
raise URLVerificationError("Error verifying URL token") from e
def get_movie_by_stream_id(stream_id: str) -> Optional[Dict]:
"""
Get movie by stream ID.
Args:
stream_id: The stream ID to look up
Returns:
Movie document or None if not found
"""
try:
return movies.find_one({"stream_id": stream_id})
except Exception as e:
logger.error(f"Error getting movie with stream_id {stream_id}: {e}")
raise DatabaseError("Error retrieving movie") from e
def search_movies(query: str, limit: int = 10) -> List[Dict]:
"""
Search movies by title.
Args:
query: Search query string
limit: Maximum number of results to return
Returns:
List of matching movie documents
"""
try:
return list(movies.find(
{"title": {"$regex": query, "$options": "i"}},
limit=limit
).sort("created_at", -1))
except Exception as e:
logger.error(f"Error searching movies with query '{query}': {e}")
raise DatabaseError("Error searching movies") from e
def increment_movie_views(stream_id: str) -> bool:
"""
Increment movie view count.
Args:
stream_id: The stream ID of the movie
Returns:
True if successful, False otherwise
"""
try:
result = movies.update_one(
{"stream_id": stream_id},
{"$inc": {"views": 1}}
)
return result.modified_count > 0
except Exception as e:
logger.error(f"Error incrementing views for stream_id {stream_id}: {e}")
raise DatabaseError("Error updating view count") from e
def create_movie(title: str, stream_id: str, file_url: str,
description: Optional[str] = None, year: Optional[int] = None,
genre: Optional[str] = None, uploader_id: Optional[str] = None) -> Dict:
"""
Create a new movie entry.
Args:
title: Movie title
stream_id: Unique stream identifier
file_url: URL to the movie file
description: Optional movie description
year: Optional release year
genre: Optional movie genre
uploader_id: Optional uploader's ID
Returns:
Created movie document
Raises:
DatabaseError: If creation fails
"""
try:
movie = {
"title": title,
"stream_id": stream_id,
"file_url": file_url,
"description": description,
"year": year,
"genre": genre,
"uploader_id": uploader_id,
"views": 0,
"created_at": datetime.utcnow()
}
result = movies.insert_one(movie)
if not result.inserted_id:
raise DatabaseError("Failed to insert movie")
return movie
except Exception as e:
logger.error(f"Error creating movie {title}: {e}")
raise DatabaseError("Error creating movie") from e
def get_movie_stats() -> Dict[str, Any]:
"""
Get movie statistics.
Returns:
Dictionary containing total movies, views, and other stats
"""
try:
total_movies = movies.count_documents({})
total_views = sum(movie.get('views', 0) for movie in movies.find({}, {'views': 1}))
return {
'total_movies': total_movies,
'total_views': total_views,
'last_updated': datetime.utcnow()
}
except Exception as e:
logger.error(f"Error getting movie stats: {e}")
raise DatabaseError("Error retrieving movie statistics") from e