Karan Adhikari commited on
Update utilities.py
Browse files
Backend/pyrofork/plugins/utilities.py
CHANGED
|
@@ -27,7 +27,8 @@ from Backend.config import Telegram
|
|
| 27 |
from Backend.helper.encrypt import decode_string
|
| 28 |
|
| 29 |
|
| 30 |
-
CONCURRENT_TASKS =
|
|
|
|
| 31 |
PROGRESS_UPDATE_EVERY = 50
|
| 32 |
|
| 33 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -228,12 +229,9 @@ async def search_command(client: Client, message: Message):
|
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 235 |
-
# Helper: check single message
|
| 236 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 237 |
async def check_message(client, stream_hash):
|
| 238 |
try:
|
| 239 |
decoded = await decode_string(stream_hash)
|
|
@@ -254,17 +252,17 @@ async def check_message(client, stream_hash):
|
|
| 254 |
return None
|
| 255 |
|
| 256 |
|
| 257 |
-
# βββββββββββββββββββββββββββββββββββ
|
| 258 |
# Batch processor
|
| 259 |
-
# βββββββββββββββββββββββββββββββββββ
|
| 260 |
async def process_batch(client, batch):
|
| 261 |
tasks = [check_message(client, s) for s in batch]
|
| 262 |
return await asyncio.gather(*tasks, return_exceptions=True)
|
| 263 |
|
| 264 |
|
| 265 |
-
# βββββββββββββββββββββββββββββββββββ
|
| 266 |
# /dbcheck β Integrity checker (find orphaned streams)
|
| 267 |
-
# βββββββββββββββββββββββββββββββββββ
|
| 268 |
@Client.on_message(filters.command('dbcheck') & filters.private & CustomFilters.owner, group=10)
|
| 269 |
async def dbcheck_command(client: Client, message: Message):
|
| 270 |
|
|
@@ -272,7 +270,7 @@ async def dbcheck_command(client: Client, message: Message):
|
|
| 272 |
purge_mode = len(args) > 1 and args[1].lower() == "purge"
|
| 273 |
|
| 274 |
status_msg = await message.reply_text(
|
| 275 |
-
"π
|
| 276 |
quote=True
|
| 277 |
)
|
| 278 |
|
|
@@ -289,14 +287,27 @@ async def dbcheck_command(client: Client, message: Message):
|
|
| 289 |
try:
|
| 290 |
for i in range(1, db.current_db_index + 1):
|
| 291 |
storage = db.dbs.get(f"storage_{i}")
|
|
|
|
| 292 |
if storage is None:
|
| 293 |
continue
|
| 294 |
|
| 295 |
-
# βββββββββ MOVIES βββββββββ
|
| 296 |
-
|
| 297 |
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
title = movie.get("title", "Unknown")
|
| 301 |
|
| 302 |
stream_ids = [
|
|
@@ -332,16 +343,24 @@ async def dbcheck_command(client: Client, message: Message):
|
|
| 332 |
f"β‘ Speed: {speed}/sec"
|
| 333 |
)
|
| 334 |
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
finally:
|
| 338 |
-
await cursor.close()
|
| 339 |
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
-
try:
|
| 344 |
-
async for show in cursor:
|
| 345 |
stream_ids = []
|
| 346 |
|
| 347 |
for season in show.get("seasons", []):
|
|
@@ -378,14 +397,10 @@ async def dbcheck_command(client: Client, message: Message):
|
|
| 378 |
f"β‘ Speed: {speed}/sec"
|
| 379 |
)
|
| 380 |
|
| 381 |
-
except CursorNotFound:
|
| 382 |
-
await status_msg.edit_text("β οΈ Cursor expired (tv), retrying...")
|
| 383 |
-
finally:
|
| 384 |
-
await cursor.close()
|
| 385 |
-
|
| 386 |
# βββββββββ PURGE βββββββββ
|
| 387 |
if purge_mode and dead_entries:
|
| 388 |
purge_tasks = []
|
|
|
|
| 389 |
for stream_hash in dead_entries:
|
| 390 |
purge_tasks.append(db.delete_media_by_stream_id(stream_hash))
|
| 391 |
|
|
@@ -413,8 +428,10 @@ async def dbcheck_command(client: Client, message: Message):
|
|
| 413 |
)
|
| 414 |
|
| 415 |
except Exception as e:
|
|
|
|
| 416 |
await status_msg.edit_text(f"β DB check failed: {e}")
|
| 417 |
|
|
|
|
| 418 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 419 |
# /exportchannels & /importchannels β Backup/restore channel config
|
| 420 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 27 |
from Backend.helper.encrypt import decode_string
|
| 28 |
|
| 29 |
|
| 30 |
+
CONCURRENT_TASKS = 10
|
| 31 |
+
BATCH_SIZE = 100
|
| 32 |
PROGRESS_UPDATE_EVERY = 50
|
| 33 |
|
| 34 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
+
# βββββββββββββββββββββββββββββββββββ
|
| 233 |
+
# Check single Telegram message
|
| 234 |
+
# βββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
| 235 |
async def check_message(client, stream_hash):
|
| 236 |
try:
|
| 237 |
decoded = await decode_string(stream_hash)
|
|
|
|
| 252 |
return None
|
| 253 |
|
| 254 |
|
| 255 |
+
# βββββββββββββββββββββββββββββββββββ
|
| 256 |
# Batch processor
|
| 257 |
+
# βββββββββββββββββββββββββββββββββββ
|
| 258 |
async def process_batch(client, batch):
|
| 259 |
tasks = [check_message(client, s) for s in batch]
|
| 260 |
return await asyncio.gather(*tasks, return_exceptions=True)
|
| 261 |
|
| 262 |
|
| 263 |
+
# βββββββββββββββββββββββββββββββββββ
|
| 264 |
# /dbcheck β Integrity checker (find orphaned streams)
|
| 265 |
+
# βββββββββββββββββββββββββββββββββββ
|
| 266 |
@Client.on_message(filters.command('dbcheck') & filters.private & CustomFilters.owner, group=10)
|
| 267 |
async def dbcheck_command(client: Client, message: Message):
|
| 268 |
|
|
|
|
| 270 |
purge_mode = len(args) > 1 and args[1].lower() == "purge"
|
| 271 |
|
| 272 |
status_msg = await message.reply_text(
|
| 273 |
+
"π DB Check started (Atlas-safe mode)...",
|
| 274 |
quote=True
|
| 275 |
)
|
| 276 |
|
|
|
|
| 287 |
try:
|
| 288 |
for i in range(1, db.current_db_index + 1):
|
| 289 |
storage = db.dbs.get(f"storage_{i}")
|
| 290 |
+
|
| 291 |
if storage is None:
|
| 292 |
continue
|
| 293 |
|
| 294 |
+
# βββββββββ MOVIES (PAGINATION) βββββββββ
|
| 295 |
+
last_id = None
|
| 296 |
|
| 297 |
+
while True:
|
| 298 |
+
query = {"_id": {"$gt": last_id}} if last_id else {}
|
| 299 |
+
|
| 300 |
+
docs = await storage["movie"] \
|
| 301 |
+
.find(query) \
|
| 302 |
+
.sort("_id", 1) \
|
| 303 |
+
.limit(BATCH_SIZE) \
|
| 304 |
+
.to_list(length=BATCH_SIZE)
|
| 305 |
+
|
| 306 |
+
if not docs:
|
| 307 |
+
break
|
| 308 |
+
|
| 309 |
+
for movie in docs:
|
| 310 |
+
last_id = movie["_id"]
|
| 311 |
title = movie.get("title", "Unknown")
|
| 312 |
|
| 313 |
stream_ids = [
|
|
|
|
| 343 |
f"β‘ Speed: {speed}/sec"
|
| 344 |
)
|
| 345 |
|
| 346 |
+
# βββββββββ TV (PAGINATION) βββββββββ
|
| 347 |
+
last_id = None
|
|
|
|
|
|
|
| 348 |
|
| 349 |
+
while True:
|
| 350 |
+
query = {"_id": {"$gt": last_id}} if last_id else {}
|
| 351 |
+
|
| 352 |
+
docs = await storage["tv"] \
|
| 353 |
+
.find(query) \
|
| 354 |
+
.sort("_id", 1) \
|
| 355 |
+
.limit(BATCH_SIZE) \
|
| 356 |
+
.to_list(length=BATCH_SIZE)
|
| 357 |
+
|
| 358 |
+
if not docs:
|
| 359 |
+
break
|
| 360 |
+
|
| 361 |
+
for show in docs:
|
| 362 |
+
last_id = show["_id"]
|
| 363 |
|
|
|
|
|
|
|
| 364 |
stream_ids = []
|
| 365 |
|
| 366 |
for season in show.get("seasons", []):
|
|
|
|
| 397 |
f"β‘ Speed: {speed}/sec"
|
| 398 |
)
|
| 399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
# βββββββββ PURGE βββββββββ
|
| 401 |
if purge_mode and dead_entries:
|
| 402 |
purge_tasks = []
|
| 403 |
+
|
| 404 |
for stream_hash in dead_entries:
|
| 405 |
purge_tasks.append(db.delete_media_by_stream_id(stream_hash))
|
| 406 |
|
|
|
|
| 428 |
)
|
| 429 |
|
| 430 |
except Exception as e:
|
| 431 |
+
LOGGER.error(f"[DBCheck] Error: {e}")
|
| 432 |
await status_msg.edit_text(f"β DB check failed: {e}")
|
| 433 |
|
| 434 |
+
|
| 435 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 436 |
# /exportchannels & /importchannels β Backup/restore channel config
|
| 437 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|