Karan Adhikari commited on
Commit
e7f9e2f
Β·
unverified Β·
1 Parent(s): 4c27f7b

Update utilities.py

Browse files
Files changed (1) hide show
  1. Backend/pyrofork/plugins/utilities.py +46 -29
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 = 15
 
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
- "πŸš€ Advanced DB Check started...\nThis will run fast ⚑",
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
- cursor = storage["movie"].find({}, no_cursor_timeout=True)
297
 
298
- try:
299
- async for movie in cursor:
 
 
 
 
 
 
 
 
 
 
 
 
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
- except CursorNotFound:
336
- await status_msg.edit_text("⚠️ Cursor expired (movies), retrying...")
337
- finally:
338
- await cursor.close()
339
 
340
- # ───────── TV ─────────
341
- cursor = storage["tv"].find({}, no_cursor_timeout=True)
 
 
 
 
 
 
 
 
 
 
 
 
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
  # ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━