Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -88,11 +88,10 @@ async def download_youtube_rapidapi(url, chat_id, message_id, client):
|
|
| 88 |
}
|
| 89 |
|
| 90 |
async with aiohttp.ClientSession(headers=headers) as session:
|
| 91 |
-
# مرحله 1: شروع دانلود
|
| 92 |
payload = {"url": url, "format": "mp4", "quality": 720}
|
| 93 |
-
async with session.post(f"{API_BASE}/
|
| 94 |
|
| 95 |
-
# 🟢 بخش نمایش دقیق لاگ سرور
|
| 96 |
if resp.status != 200:
|
| 97 |
server_log = await resp.text()
|
| 98 |
error_message = (
|
|
@@ -112,14 +111,21 @@ async def download_youtube_rapidapi(url, chat_id, message_id, client):
|
|
| 112 |
if not job_id:
|
| 113 |
raise Exception(f"سرور شناسه Job ID را برنگرداند. پاسخ سرور:\n{start_data}")
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
if status_msg_id:
|
| 116 |
try: await client.edit_message_text(chat_id, status_msg_id, "⏳ ویدیو در سرور رپید در حال پردازش است (در حال بررسی وضعیت...)")
|
| 117 |
except: pass
|
| 118 |
|
| 119 |
-
# مرحله 2: بررسی وضعیت پردازش
|
| 120 |
while True:
|
| 121 |
await asyncio.sleep(3)
|
| 122 |
-
async with session.get(
|
| 123 |
if status_resp.status != 200:
|
| 124 |
status_log = await status_resp.text()
|
| 125 |
raise Exception(f"خطا در بررسی وضعیت (کد {status_resp.status}).\nلاگ:\n{status_log}")
|
|
@@ -141,7 +147,7 @@ async def download_youtube_rapidapi(url, chat_id, message_id, client):
|
|
| 141 |
try: await client.edit_message_text(chat_id, status_msg_id, "📥 پردازش موفق! در حال دانلود ویدیو از سرور واسط...")
|
| 142 |
except: pass
|
| 143 |
|
| 144 |
-
# مرحله 3: دانلود فایل نهایی در
|
| 145 |
temp_dir = "/app/downloads"
|
| 146 |
os.makedirs(temp_dir, exist_ok=True)
|
| 147 |
filename = f"youtube_{uuid.uuid4().hex[:6]}.mp4"
|
|
@@ -158,10 +164,9 @@ async def download_youtube_rapidapi(url, chat_id, message_id, client):
|
|
| 158 |
return file_path, filename, status_msg_id
|
| 159 |
|
| 160 |
except Exception as e:
|
| 161 |
-
# نمایش خطای کامل در ربات
|
| 162 |
error_msg = f"❌ خطای یوتیوب:\n{str(e)}"
|
| 163 |
if status_msg_id:
|
| 164 |
-
try: await client.edit_message_text(chat_id, status_msg_id, error_msg[:1000])
|
| 165 |
except: pass
|
| 166 |
return None, None, status_msg_id
|
| 167 |
|
|
|
|
| 88 |
}
|
| 89 |
|
| 90 |
async with aiohttp.ClientSession(headers=headers) as session:
|
| 91 |
+
# مرحله 1: شروع دانلود (مسیر اصلاح شد: downloadVideo)
|
| 92 |
payload = {"url": url, "format": "mp4", "quality": 720}
|
| 93 |
+
async with session.post(f"{API_BASE}/downloadVideo", json=payload) as resp:
|
| 94 |
|
|
|
|
| 95 |
if resp.status != 200:
|
| 96 |
server_log = await resp.text()
|
| 97 |
error_message = (
|
|
|
|
| 111 |
if not job_id:
|
| 112 |
raise Exception(f"سرور شناسه Job ID را برنگرداند. پاسخ سرور:\n{start_data}")
|
| 113 |
|
| 114 |
+
# خواندن داینامیک مسیر وضعیت از پاسخی که خود سرور برمیگرداند
|
| 115 |
+
status_path = start_data.get("statusUrl", f"/status/{job_id}")
|
| 116 |
+
if status_path.startswith("/"):
|
| 117 |
+
status_url = API_BASE + status_path
|
| 118 |
+
else:
|
| 119 |
+
status_url = f"{API_BASE}/{status_path}"
|
| 120 |
+
|
| 121 |
if status_msg_id:
|
| 122 |
try: await client.edit_message_text(chat_id, status_msg_id, "⏳ ویدیو در سرور رپید در حال پردازش است (در حال بررسی وضعیت...)")
|
| 123 |
except: pass
|
| 124 |
|
| 125 |
+
# مرحله 2: بررسی وضعیت پردازش
|
| 126 |
while True:
|
| 127 |
await asyncio.sleep(3)
|
| 128 |
+
async with session.get(status_url) as status_resp:
|
| 129 |
if status_resp.status != 200:
|
| 130 |
status_log = await status_resp.text()
|
| 131 |
raise Exception(f"خطا در بررسی وضعیت (کد {status_resp.status}).\nلاگ:\n{status_log}")
|
|
|
|
| 147 |
try: await client.edit_message_text(chat_id, status_msg_id, "📥 پردازش موفق! در حال دانلود ویدیو از سرور واسط...")
|
| 148 |
except: pass
|
| 149 |
|
| 150 |
+
# مرحله 3: دانلود فایل نهایی (آدرس دانلود هم نیاز به توکن دارد که توسط سشن ارسال میشود)
|
| 151 |
temp_dir = "/app/downloads"
|
| 152 |
os.makedirs(temp_dir, exist_ok=True)
|
| 153 |
filename = f"youtube_{uuid.uuid4().hex[:6]}.mp4"
|
|
|
|
| 164 |
return file_path, filename, status_msg_id
|
| 165 |
|
| 166 |
except Exception as e:
|
|
|
|
| 167 |
error_msg = f"❌ خطای یوتیوب:\n{str(e)}"
|
| 168 |
if status_msg_id:
|
| 169 |
+
try: await client.edit_message_text(chat_id, status_msg_id, error_msg[:1000])
|
| 170 |
except: pass
|
| 171 |
return None, None, status_msg_id
|
| 172 |
|