Update bot.py
Browse files
bot.py
CHANGED
|
@@ -19,7 +19,7 @@ os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
|
| 19 |
|
| 20 |
HEALTHCHECK_URL = os.environ.get("HEALTHCHECK_URL", "")
|
| 21 |
MAX_FILE_SIZE = 2000 * 1024 * 1024 # 2GB
|
| 22 |
-
DYNAMIC_UPLOAD_DELAY = 3
|
| 23 |
download_queue = asyncio.Queue()
|
| 24 |
|
| 25 |
# --- 2. سيرفر الحفاظ على التشغيل ---
|
|
@@ -32,7 +32,7 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|
| 32 |
|
| 33 |
Thread(target=lambda: HTTPServer(('0.0.0.0', 7860), RequestHandler).serve_forever(), daemon=True).start()
|
| 34 |
|
| 35 |
-
# --- 3. إعدادات الحساب
|
| 36 |
API_ID = int(os.environ.get("API_ID", 0))
|
| 37 |
API_HASH = os.environ.get("API_HASH", "")
|
| 38 |
SESSION_STRING = os.environ.get("SESSION_STRING", "")
|
|
@@ -44,7 +44,6 @@ app = Client(
|
|
| 44 |
api_hash=API_HASH,
|
| 45 |
session_string=SESSION_STRING,
|
| 46 |
in_memory=True,
|
| 47 |
-
# التخفي كأنه هاتف حقيقي لتجنب الحظر
|
| 48 |
device_model="Samsung Galaxy S23 Ultra",
|
| 49 |
system_version="Android 14.0",
|
| 50 |
app_version="10.14.1"
|
|
@@ -91,7 +90,7 @@ def get_media_details(message):
|
|
| 91 |
file_size = getattr(media, "file_size", 0)
|
| 92 |
return unique_id, m_type, file_size
|
| 93 |
|
| 94 |
-
# --- 5. محرك التنزيل المتوازي ---
|
| 95 |
async def download_worker_task(worker_id):
|
| 96 |
while True:
|
| 97 |
message = await download_queue.get()
|
|
@@ -107,12 +106,13 @@ async def download_worker_task(worker_id):
|
|
| 107 |
async with db.execute("SELECT id FROM queue WHERE file_unique_id=?", (unique_id,)) as cursor:
|
| 108 |
if await cursor.fetchone():
|
| 109 |
continue
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
for attempt in range(3):
|
| 114 |
try:
|
| 115 |
-
file_path = await asyncio.wait_for(message.download(file_name=
|
| 116 |
if file_path and os.path.exists(file_path) and os.path.getsize(file_path) > 0:
|
| 117 |
group_id = str(message.media_group_id) if message.media_group_id else None
|
| 118 |
src_title = message.chat.title or "المصدر"
|
|
@@ -136,7 +136,7 @@ async def download_worker_task(worker_id):
|
|
| 136 |
finally:
|
| 137 |
download_queue.task_done()
|
| 138 |
|
| 139 |
-
# --- 6. صائد الرسائل الحية
|
| 140 |
@app.on_message(filters.incoming & ~filters.me & (filters.channel | filters.group) & filters.media)
|
| 141 |
async def catch_incoming(client, message):
|
| 142 |
if message.chat.id != TARGET_CHAT:
|
|
@@ -163,7 +163,7 @@ async def status_command(client, message):
|
|
| 163 |
)
|
| 164 |
await message.reply_text(report)
|
| 165 |
|
| 166 |
-
# --- 8. محرك الرفع
|
| 167 |
async def upload_worker():
|
| 168 |
global DYNAMIC_UPLOAD_DELAY
|
| 169 |
while True:
|
|
@@ -248,8 +248,13 @@ async def upload_worker():
|
|
| 248 |
DYNAMIC_UPLOAD_DELAY += 2
|
| 249 |
await asyncio.sleep(e.value + 5)
|
| 250 |
except Exception as e:
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
async def healthcheck_ping():
|
| 255 |
while True:
|
|
@@ -258,7 +263,6 @@ async def healthcheck_ping():
|
|
| 258 |
try: urllib.request.urlopen(HEALTHCHECK_URL, timeout=10)
|
| 259 |
except: pass
|
| 260 |
|
| 261 |
-
# --- التحديث هنا: دالة main لتعريف القنوات وتجنب الأخطاء ---
|
| 262 |
async def main():
|
| 263 |
await init_db()
|
| 264 |
await app.start()
|
|
|
|
| 19 |
|
| 20 |
HEALTHCHECK_URL = os.environ.get("HEALTHCHECK_URL", "")
|
| 21 |
MAX_FILE_SIZE = 2000 * 1024 * 1024 # 2GB
|
| 22 |
+
DYNAMIC_UPLOAD_DELAY = 3
|
| 23 |
download_queue = asyncio.Queue()
|
| 24 |
|
| 25 |
# --- 2. سيرفر الحفاظ على التشغيل ---
|
|
|
|
| 32 |
|
| 33 |
Thread(target=lambda: HTTPServer(('0.0.0.0', 7860), RequestHandler).serve_forever(), daemon=True).start()
|
| 34 |
|
| 35 |
+
# --- 3. إعدادات الحساب ---
|
| 36 |
API_ID = int(os.environ.get("API_ID", 0))
|
| 37 |
API_HASH = os.environ.get("API_HASH", "")
|
| 38 |
SESSION_STRING = os.environ.get("SESSION_STRING", "")
|
|
|
|
| 44 |
api_hash=API_HASH,
|
| 45 |
session_string=SESSION_STRING,
|
| 46 |
in_memory=True,
|
|
|
|
| 47 |
device_model="Samsung Galaxy S23 Ultra",
|
| 48 |
system_version="Android 14.0",
|
| 49 |
app_version="10.14.1"
|
|
|
|
| 90 |
file_size = getattr(media, "file_size", 0)
|
| 91 |
return unique_id, m_type, file_size
|
| 92 |
|
| 93 |
+
# --- 5. محرك التنزيل المتوازي (تم تحديثه لحل مشكلة الامتداد) ---
|
| 94 |
async def download_worker_task(worker_id):
|
| 95 |
while True:
|
| 96 |
message = await download_queue.get()
|
|
|
|
| 106 |
async with db.execute("SELECT id FROM queue WHERE file_unique_id=?", (unique_id,)) as cursor:
|
| 107 |
if await cursor.fetchone():
|
| 108 |
continue
|
| 109 |
+
|
| 110 |
+
# التعديل هنا: إضافة ( _ ) في النهاية تجعل بايروجرام يضيف امتداد الملف الصحيح تلقائياً (.jpg, .mp4)
|
| 111 |
+
file_prefix = f"{DOWNLOAD_DIR}/{message.chat.id}_{message.id}_"
|
| 112 |
|
| 113 |
for attempt in range(3):
|
| 114 |
try:
|
| 115 |
+
file_path = await asyncio.wait_for(message.download(file_name=file_prefix), timeout=300)
|
| 116 |
if file_path and os.path.exists(file_path) and os.path.getsize(file_path) > 0:
|
| 117 |
group_id = str(message.media_group_id) if message.media_group_id else None
|
| 118 |
src_title = message.chat.title or "المصدر"
|
|
|
|
| 136 |
finally:
|
| 137 |
download_queue.task_done()
|
| 138 |
|
| 139 |
+
# --- 6. صائد الرسائل الحية ---
|
| 140 |
@app.on_message(filters.incoming & ~filters.me & (filters.channel | filters.group) & filters.media)
|
| 141 |
async def catch_incoming(client, message):
|
| 142 |
if message.chat.id != TARGET_CHAT:
|
|
|
|
| 163 |
)
|
| 164 |
await message.reply_text(report)
|
| 165 |
|
| 166 |
+
# --- 8. محرك الرفع (تم تحديثه لمنع الدوامة اللانهائية) ---
|
| 167 |
async def upload_worker():
|
| 168 |
global DYNAMIC_UPLOAD_DELAY
|
| 169 |
while True:
|
|
|
|
| 248 |
DYNAMIC_UPLOAD_DELAY += 2
|
| 249 |
await asyncio.sleep(e.value + 5)
|
| 250 |
except Exception as e:
|
| 251 |
+
# التعديل هنا: إذا حدث خطأ غير طبيعي، نتجاوز الملف حتى لا يعلق السكربت
|
| 252 |
+
print(f"⚠️ خطأ أثناء الرفع، سيتم تخطي الملف: {e}")
|
| 253 |
+
async with aiosqlite.connect(DB_PATH) as db:
|
| 254 |
+
await db.execute("UPDATE queue SET status='FAILED' WHERE id=?", (q_id,))
|
| 255 |
+
await db.commit()
|
| 256 |
+
if os.path.exists(file_path): os.remove(file_path)
|
| 257 |
+
await asyncio.sleep(2)
|
| 258 |
|
| 259 |
async def healthcheck_ping():
|
| 260 |
while True:
|
|
|
|
| 263 |
try: urllib.request.urlopen(HEALTHCHECK_URL, timeout=10)
|
| 264 |
except: pass
|
| 265 |
|
|
|
|
| 266 |
async def main():
|
| 267 |
await init_db()
|
| 268 |
await app.start()
|