Opera8 commited on
Commit
06cd0eb
·
verified ·
1 Parent(s): b48c322

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +40 -69
main.py CHANGED
@@ -704,67 +704,9 @@ HF_TOKENS =[k.strip() for k in HF_TOKENS_STR.split(",") if k.strip()]
704
  # ==============================================================================
705
  BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN", "").strip()
706
 
707
- async def background_rubika_upload(client, chat_id, original_path, file_type, caption, is_background=True):
708
- """کارگر جداگانه برای آپلود فایل در روبیکا بدون ایجاد تاخیر در ربات اصلی"""
709
- target_path = original_path
710
-
711
- if is_background:
712
- # کپی کردن فایل برای کارگر بک‌گراند (چون فایل اصلی توسط تابع والد بلافاصله حذف می‌شود)
713
- target_path = f"{original_path}_bg_{uuid.uuid4().hex[:6]}.tmp"
714
- try:
715
- with open(original_path, 'rb') as f_src, open(target_path, 'wb') as f_dst:
716
- f_dst.write(f_src.read())
717
- except Exception:
718
- return False
719
-
720
- api_file_type = "Image" if file_type in ["photo", "Image", "image"] else "Voice" if file_type in ["voice", "Voice", "audio"] else file_type
721
- if api_file_type == "Voice" and not target_path.lower().endswith('.ogg'): api_file_type = "Music"
722
- if api_file_type == "File" and target_path.lower().endswith('.mp3'): api_file_type = "Music"
723
-
724
- # اگر آپلود در بک‌گراند (همزمان با لینک بله) است، کپشن ساده باشد وگرنه متن نجات
725
- final_caption = "📥 فایل ضمیمه:" if is_background else f"{caption}\n\n⚠️ (به دلیل اختلال سرورهای بله، فایل موقتاً به صورت مستقیم در روبیکا ارسال شد)"
726
-
727
- sent_success = False
728
- for attempt in range(5):
729
- try:
730
- if api_file_type == "Image" and hasattr(client, "send_photo"):
731
- try: await client.send_photo(chat_id, photo=target_path, caption=final_caption); sent_success = True
732
- except: await client.send_photo(chat_id, target_path, caption=final_caption); sent_success = True
733
- elif api_file_type == "Voice" and hasattr(client, "send_voice"):
734
- try: await client.send_voice(chat_id, target_path, caption=final_caption); sent_success = True
735
- except: await client.send_voice(chat_id, file=target_path, caption=final_caption); sent_success = True
736
- elif api_file_type == "Music" and hasattr(client, "send_music"):
737
- try: await client.send_music(chat_id, target_path, caption=final_caption); sent_success = True
738
- except: await client.send_music(chat_id, music=target_path, caption=final_caption); sent_success = True
739
-
740
- if not sent_success:
741
- if hasattr(client, "send_document"):
742
- try: await client.send_document(chat_id, document=target_path, caption=final_caption); sent_success = True
743
- except: await client.send_document(chat_id, target_path, caption=final_caption); sent_success = True
744
- elif hasattr(client, "send_file"):
745
- try: await client.send_file(chat_id, file=target_path)
746
- except: await client.send_file(chat_id, target_path)
747
- if final_caption: await send_with_keyboard(client, chat_id, final_caption, True)
748
- sent_success = True
749
-
750
- if sent_success:
751
- break
752
- except Exception:
753
- await asyncio.sleep(3)
754
-
755
- # پاک کردن فایل موقتِ کارگر
756
- if is_background:
757
- try:
758
- if os.path.exists(target_path):
759
- os.remove(target_path)
760
- except:
761
- pass
762
-
763
- return sent_success
764
-
765
  async def helper_upload_file(client, chat_id, file_name, file_type="Image", caption=""):
766
  abs_path = os.path.abspath(file_name)
767
- error_logs = []
768
 
769
  # ==========================================
770
  # فاز 1: تلاش برای آپلود در بله (ساخت لینک)
@@ -799,27 +741,56 @@ async def helper_upload_file(client, chat_id, file_name, file_type="Image", capt
799
  final_text = f"""{caption}\n\n━━━━━━━━━━━━━━━━━━━\n🌐 لینک دانلود فایل شما:\n\n{download_url}\n\n━━━━━━━━━━━━━━━━━━━\n⚠️ **راهنمای دانلود:**\nلطفاً لینک بالا را کپی کرده و در **مرورگر گوشی خود** باز کنید.\n*(مستقیماً کلیک نکنید)*"""
800
 
801
  send_res = await send_with_keyboard(client, chat_id, final_text, True)
802
-
803
- if send_res:
804
- # 🟢 ایجاد تسک کارگر برای آپلود مستقیم و بی‌صدا در روبیکا در پس‌زمینه (بدون اختلال)
805
- asyncio.create_task(background_rubika_upload(client, chat_id, abs_path, file_type, caption, True))
806
- return True
807
  else:
808
  error_logs.append(f"Bale API Err: {bale_data.get('description')}")
809
  else:
810
  error_logs.append(f"Bale HTTP Error: {resp.status}")
811
- if resp.status in [500, 502, 503]: await asyncio.sleep(2)
812
  except Exception as e:
813
  error_logs.append(f"Bale Err: {str(e)[:100]}")
814
  await asyncio.sleep(2)
815
 
816
  # ==========================================
817
  # فاز 2: سیستم نجات! (آپلود مستقیم در روبیکا)
818
- # (اگر بله قطع بود، تابع اصلی متوقف می‌شود تا فایل را مستقیماً ارسال کند)
819
  # ==========================================
820
- success = await background_rubika_upload(client, chat_id, abs_path, file_type, caption, False)
821
- if success:
822
- return True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
823
 
824
  return "\n".join(error_logs[-5:])
825
 
 
704
  # ==============================================================================
705
  BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN", "").strip()
706
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707
  async def helper_upload_file(client, chat_id, file_name, file_type="Image", caption=""):
708
  abs_path = os.path.abspath(file_name)
709
+ error_logs =[]
710
 
711
  # ==========================================
712
  # فاز 1: تلاش برای آپلود در بله (ساخت لینک)
 
741
  final_text = f"""{caption}\n\n━━━━━━━━━━━━━━━━━━━\n🌐 لینک دانلود فایل شما:\n\n{download_url}\n\n━━━━━━━━━━━━━━━━━━━\n⚠️ **راهنمای دانلود:**\nلطفاً لینک بالا را کپی کرده و در **مرورگر گوشی خود** باز کنید.\n*(مستقیماً کلیک نکنید)*"""
742
 
743
  send_res = await send_with_keyboard(client, chat_id, final_text, True)
744
+ if send_res: return True
 
 
 
 
745
  else:
746
  error_logs.append(f"Bale API Err: {bale_data.get('description')}")
747
  else:
748
  error_logs.append(f"Bale HTTP Error: {resp.status}")
749
+ if resp.status in[500, 502, 503]: await asyncio.sleep(2)
750
  except Exception as e:
751
  error_logs.append(f"Bale Err: {str(e)[:100]}")
752
  await asyncio.sleep(2)
753
 
754
  # ==========================================
755
  # فاز 2: سیستم نجات! (آپلود مستقیم در روبیکا)
756
+ # (اگر بله قطع بود، فایل مستقیما تو روبیکا ارسال میشه)
757
  # ==========================================
758
+ api_file_type = "Image" if file_type in["photo", "Image", "image"] else "Voice" if file_type in ["voice", "Voice", "audio"] else file_type
759
+ if api_file_type == "Voice" and not abs_path.lower().endswith('.ogg'): api_file_type = "Music"
760
+ if api_file_type == "File" and abs_path.lower().endswith('.mp3'): api_file_type = "Music"
761
+
762
+ fallback_caption = f"{caption}\n\n⚠️ (به دلیل اختلال سرورهای بله، فایل موقتاً به صورت مستقیم در روبیکا ارسال شد)"
763
+
764
+ for attempt in range(5):
765
+ try:
766
+ sent_success = False
767
+
768
+ if api_file_type == "Image" and hasattr(client, "send_photo"):
769
+ try: await client.send_photo(chat_id, photo=abs_path, caption=fallback_caption); sent_success = True
770
+ except: await client.send_photo(chat_id, abs_path, caption=fallback_caption); sent_success = True
771
+ elif api_file_type == "Voice" and hasattr(client, "send_voice"):
772
+ try: await client.send_voice(chat_id, abs_path, caption=fallback_caption); sent_success = True
773
+ except: await client.send_voice(chat_id, file=abs_path, caption=fallback_caption); sent_success = True
774
+ elif api_file_type == "Music" and hasattr(client, "send_music"):
775
+ try: await client.send_music(chat_id, abs_path, caption=fallback_caption); sent_success = True
776
+ except: await client.send_music(chat_id, music=abs_path, caption=fallback_caption); sent_success = True
777
+
778
+ if not sent_success:
779
+ if hasattr(client, "send_document"):
780
+ try: await client.send_document(chat_id, document=abs_path, caption=fallback_caption); sent_success = True
781
+ except: await client.send_document(chat_id, abs_path, caption=fallback_caption); sent_success = True
782
+ elif hasattr(client, "send_file"):
783
+ try: await client.send_file(chat_id, file=abs_path)
784
+ except: await client.send_file(chat_id, abs_path)
785
+ if fallback_caption: await send_with_keyboard(client, chat_id, fallback_caption, True)
786
+ sent_success = True
787
+
788
+ if sent_success:
789
+ return True
790
+
791
+ except Exception as e:
792
+ error_logs.append(f"Rubpy Send Err: {str(e)[:100]}")
793
+ await asyncio.sleep(3)
794
 
795
  return "\n".join(error_logs[-5:])
796