pmrony commited on
Commit
371c6fd
·
verified ·
1 Parent(s): 9f19490

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -21
app.py CHANGED
@@ -293,20 +293,16 @@ async def send_to_specific_group(client, message):
293
  # ================= DATABASE PROGRESS-BASED CLONING =================
294
  async def save_progress(source_id, dest_id, msg_id):
295
  try:
296
- # ডাটাবেজে আগে থেকে প্রোগ্রেস আছে কিনা চেক করা হচ্ছে
297
  res = await db_query(lambda: supabase.table('clone_progress').select('id').eq('source_id', source_id).eq('dest_id', dest_id).execute())
298
  if res.data:
299
- # থাকলে সেটা আপডেট হবে
300
  await db_query(lambda: supabase.table('clone_progress').update({'last_copied_id': msg_id}).eq('id', res.data[0]['id']).execute())
301
  else:
302
- # না থাকলে নতুন রো ইনসার্ট হবে
303
  await db_query(lambda: supabase.table('clone_progress').insert({'source_id': source_id, 'dest_id': dest_id, 'last_copied_id': msg_id}).execute())
304
  except Exception as e:
305
  print(f"Error saving progress: {e}")
306
 
307
  async def clone_videos_background(client, source_id, dest_id, status_msg):
308
  try:
309
- # ডাটাবেজে আগের কোনো সেভড প্রোগ্রেস আছে কিনা দেখা হচ্ছে
310
  progress_res = await db_query(lambda: supabase.table('clone_progress').select('last_copied_id').eq('source_id', source_id).eq('dest_id', dest_id).execute())
311
 
312
  last_copied_id = None
@@ -316,14 +312,34 @@ async def clone_videos_background(client, source_id, dest_id, status_msg):
316
  else:
317
  await status_msg.edit_text(f"⏳ <b>Cloning started!</b>\nFetching video list from <code>{source_id}</code>...\n<i>This might take a few minutes if the group has many videos.</i>", parse_mode=enums.ParseMode.HTML)
318
 
 
319
  video_ids = []
320
- async for msg in client.search_messages(source_id, filter=enums.MessagesFilter.VIDEO):
321
- # যদি আগের প্রোগ্রেস থাকে, তবে আগের কপি করা ভিডিওর আইডি থেকে ছোট সব ভিডিও স্কিপ করা হবে
322
- if last_copied_id and msg.id <= last_copied_id:
323
- continue
324
- video_ids.append(msg.id)
325
- if len(video_ids) % 200 == 0:
326
- await asyncio.sleep(0.1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
  if not video_ids:
329
  if last_copied_id:
@@ -343,21 +359,35 @@ async def clone_videos_background(client, source_id, dest_id, status_msg):
343
  failed = 0
344
 
345
  for index, msg_id in enumerate(video_ids, 1):
346
- try:
347
- await client.copy_message(chat_id=dest_id, from_chat_id=source_id, message_id=msg_id)
348
- success += 1
349
- # সফলভাবে কপি হওয়ার সাথে সাথেই ডাটাবেজে প্রোগ্রেস সেভ
350
- await save_progress(source_id, dest_id, msg_id)
351
- except FloodWait as e:
352
- await asyncio.sleep(e.value + 2)
353
  try:
 
 
 
 
354
  await client.copy_message(chat_id=dest_id, from_chat_id=source_id, message_id=msg_id)
355
  success += 1
356
  await save_progress(source_id, dest_id, msg_id)
357
- except Exception:
358
- failed += 1
359
- except Exception:
 
 
 
 
 
 
 
 
 
 
 
360
  failed += 1
 
361
 
362
  if index % 20 == 0 or index == total:
363
  try:
@@ -498,6 +528,7 @@ async def handle_media_upload(client, message):
498
  original_file, watermarked_file, blurred_file = None, None, None
499
  clean_upload_file, telegram_file, embed_link = None, None, None
500
 
 
501
  last_update_time = time.time()
502
  async def download_progress(current, total):
503
  nonlocal last_update_time
@@ -509,11 +540,13 @@ async def handle_media_upload(client, message):
509
  last_update_time = now
510
  except Exception:
511
  pass
 
512
 
513
  try:
514
  original_file = await message.download(progress=download_progress)
515
  clean_upload_file = original_file
516
 
 
517
  if media_type == "video" and not is_large_video:
518
  await status_msg.edit_text("⏳ Watermarking video... (HD + Superfast Processing)")
519
  watermarked_file = f"{original_file}_wm.mp4"
@@ -528,6 +561,7 @@ async def handle_media_upload(client, message):
528
  if process.returncode == 0 and os.path.exists(watermarked_file):
529
  clean_upload_file = watermarked_file
530
 
 
531
  if media_type == "video":
532
  await status_msg.edit_text("⏳ Uploading Clean HD video to byse.sx server...")
533
  api_endpoint = "https://api.byse.sx/upload/server"
@@ -555,6 +589,7 @@ async def handle_media_upload(client, message):
555
  await status_msg.delete()
556
  return
557
 
 
558
  telegram_file = clean_upload_file
559
  if is_blur and not is_large_video:
560
  await status_msg.edit_text(f"⏳ Applying {blur_percent}% blur for Telegram broadcast...")
@@ -623,6 +658,7 @@ async def handle_media_upload(client, message):
623
 
624
  await status_msg.delete()
625
 
 
626
  groups_res = await db_query(lambda: supabase.table('groups').select('group_id').execute())
627
  group_ids = [g['group_id'] for g in groups_res.data]
628
  success_count, fail_count = 0, 0
 
293
  # ================= DATABASE PROGRESS-BASED CLONING =================
294
  async def save_progress(source_id, dest_id, msg_id):
295
  try:
 
296
  res = await db_query(lambda: supabase.table('clone_progress').select('id').eq('source_id', source_id).eq('dest_id', dest_id).execute())
297
  if res.data:
 
298
  await db_query(lambda: supabase.table('clone_progress').update({'last_copied_id': msg_id}).eq('id', res.data[0]['id']).execute())
299
  else:
 
300
  await db_query(lambda: supabase.table('clone_progress').insert({'source_id': source_id, 'dest_id': dest_id, 'last_copied_id': msg_id}).execute())
301
  except Exception as e:
302
  print(f"Error saving progress: {e}")
303
 
304
  async def clone_videos_background(client, source_id, dest_id, status_msg):
305
  try:
 
306
  progress_res = await db_query(lambda: supabase.table('clone_progress').select('last_copied_id').eq('source_id', source_id).eq('dest_id', dest_id).execute())
307
 
308
  last_copied_id = None
 
312
  else:
313
  await status_msg.edit_text(f"⏳ <b>Cloning started!</b>\nFetching video list from <code>{source_id}</code>...\n<i>This might take a few minutes if the group has many videos.</i>", parse_mode=enums.ParseMode.HTML)
314
 
315
+ # === Auto Reconnect & Retry loop for Video Scanning ===
316
  video_ids = []
317
+ retries = 5
318
+ while retries > 0:
319
+ try:
320
+ # যদি বট ক্লায়েন্ট কোনো কারণে ডিসকানেক্ট হয়, রিকানেক্ট করবে
321
+ if not client.is_connected:
322
+ try: await client.connect()
323
+ except: pass
324
+
325
+ async for msg in client.search_messages(source_id, filter=enums.MessagesFilter.VIDEO):
326
+ if last_copied_id and msg.id <= last_copied_id:
327
+ continue
328
+ video_ids.append(msg.id)
329
+ if len(video_ids) % 200 == 0:
330
+ await asyncio.sleep(0.1)
331
+ break # সফল হলে লুপ থেকে বের হবে
332
+ except Exception as e:
333
+ err_msg = str(e).lower()
334
+ # যদি ডিসকানেক্ট বা কানেকশন জনিত সমস্যা হয়
335
+ if "disconnect" in err_msg or "connection" in err_msg or "timeout" in err_msg or "reset" in err_msg:
336
+ retries -= 1
337
+ video_ids = [] # লিস্ট রিসেট করা হলো যাতে কোনো ভিডিও মিস না যায়
338
+ await status_msg.edit_text(f"⚠️ <b>Network issue detected!</b>\nRetrying in 10 seconds... (Attempts left: {retries})\nError: <code>{e}</code>", parse_mode=enums.ParseMode.HTML)
339
+ await asyncio.sleep(10)
340
+ else:
341
+ raise e # অন্য কোনো পারমিশন বা বড় এরর হলে কাজ থামিয়ে দেবে
342
+ # =======================================================
343
 
344
  if not video_ids:
345
  if last_copied_id:
 
359
  failed = 0
360
 
361
  for index, msg_id in enumerate(video_ids, 1):
362
+ copy_success = False
363
+ copy_retries = 3
364
+
365
+ # === Video Sending Auto Reconnect & Retry ===
366
+ while copy_retries > 0:
 
 
367
  try:
368
+ if not client.is_connected:
369
+ try: await client.connect()
370
+ except: pass
371
+
372
  await client.copy_message(chat_id=dest_id, from_chat_id=source_id, message_id=msg_id)
373
  success += 1
374
  await save_progress(source_id, dest_id, msg_id)
375
+ copy_success = True
376
+ break # কপি সফল হলে রিট্রাই লুপ থেকে বের হবে
377
+ except FloodWait as e:
378
+ # লিমিট এররে রিট্রাই কমানো হবে না, শুধু ওয়েট করবে
379
+ await asyncio.sleep(e.value + 2)
380
+ except Exception as e:
381
+ err_msg = str(e).lower()
382
+ if "disconnect" in err_msg or "connection" in err_msg or "timeout" in err_msg or "reset" in err_msg:
383
+ copy_retries -= 1
384
+ await asyncio.sleep(5) # ৫ সেকেন্ড পর আবার চেষ্টা করবে
385
+ else:
386
+ break # অন্য কোনো এরর (যেমন: ভিডিও ডিলিট করা) হলে লুপ স্কিপ করবে
387
+
388
+ if not copy_success:
389
  failed += 1
390
+ # ============================================
391
 
392
  if index % 20 == 0 or index == total:
393
  try:
 
528
  original_file, watermarked_file, blurred_file = None, None, None
529
  clean_upload_file, telegram_file, embed_link = None, None, None
530
 
531
+ # === Progress Bar Function ===
532
  last_update_time = time.time()
533
  async def download_progress(current, total):
534
  nonlocal last_update_time
 
540
  last_update_time = now
541
  except Exception:
542
  pass
543
+ # =============================
544
 
545
  try:
546
  original_file = await message.download(progress=download_progress)
547
  clean_upload_file = original_file
548
 
549
+ # ১. ওয়াটারমার্ক (Watermark) করা - Balanced (Superfast + CRF 23)
550
  if media_type == "video" and not is_large_video:
551
  await status_msg.edit_text("⏳ Watermarking video... (HD + Superfast Processing)")
552
  watermarked_file = f"{original_file}_wm.mp4"
 
561
  if process.returncode == 0 and os.path.exists(watermarked_file):
562
  clean_upload_file = watermarked_file
563
 
564
+ # ২. byse.sx সার্ভারে HD / Clean ভিডিও আপলোড করা
565
  if media_type == "video":
566
  await status_msg.edit_text("⏳ Uploading Clean HD video to byse.sx server...")
567
  api_endpoint = "https://api.byse.sx/upload/server"
 
589
  await status_msg.delete()
590
  return
591
 
592
+ # ৩. টেলিগ্রামে পাঠানোর জন্য ভিডিও ব্লার করা
593
  telegram_file = clean_upload_file
594
  if is_blur and not is_large_video:
595
  await status_msg.edit_text(f"⏳ Applying {blur_percent}% blur for Telegram broadcast...")
 
658
 
659
  await status_msg.delete()
660
 
661
+ # ================= FloodWait Handler for Broadcast =================
662
  groups_res = await db_query(lambda: supabase.table('groups').select('group_id').execute())
663
  group_ids = [g['group_id'] for g in groups_res.data]
664
  success_count, fail_count = 0, 0