Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,12 +23,14 @@ def cleanup_old_files():
|
|
| 23 |
for filename in os.listdir(DOWNLOAD_FOLDER):
|
| 24 |
file_path = os.path.join(DOWNLOAD_FOLDER, filename)
|
| 25 |
if os.path.isfile(file_path):
|
|
|
|
| 26 |
if current_time - os.path.getctime(file_path) > 3600:
|
| 27 |
os.remove(file_path)
|
| 28 |
except Exception as e:
|
| 29 |
print(f"Cleanup error: {e}")
|
| 30 |
time.sleep(3600)
|
| 31 |
|
|
|
|
| 32 |
cleanup_thread = threading.Thread(target=cleanup_old_files, daemon=True)
|
| 33 |
cleanup_thread.start()
|
| 34 |
|
|
@@ -98,6 +100,7 @@ HTML_TEMPLATE = """
|
|
| 98 |
throw new Error(data.error || 'خطا در دانلود ویدیو');
|
| 99 |
}
|
| 100 |
|
|
|
|
| 101 |
const blob = await response.blob();
|
| 102 |
const videoUrl = URL.createObjectURL(blob);
|
| 103 |
|
|
@@ -133,29 +136,29 @@ def download_video():
|
|
| 133 |
return jsonify({'error': 'URL is required'}), 400
|
| 134 |
|
| 135 |
url = data['url']
|
|
|
|
|
|
|
| 136 |
file_id = str(uuid.uuid4())
|
| 137 |
output_template = os.path.join(DOWNLOAD_FOLDER, f"{file_id}.%(ext)s")
|
| 138 |
|
| 139 |
-
#
|
| 140 |
ydl_opts = {
|
| 141 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 142 |
-
'outtmpl': output_template,
|
| 143 |
-
'quiet': True,
|
| 144 |
-
'no_warnings': True,
|
| 145 |
'merge_output_format': 'mp4',
|
| 146 |
|
| 147 |
-
#
|
| 148 |
-
|
| 149 |
-
'extractor_args': {'youtube': {'player_client': ['android', 'web']}},
|
| 150 |
|
| 151 |
-
#
|
| 152 |
-
|
| 153 |
-
# و یک پروکسی استاندارد (مثلاً از Webshare یا دیتاسنترهای دیگر) تهیه کردی، هشتگ خط زیر را بردار و آدرس را وارد کن:
|
| 154 |
-
# 'proxy': 'http://IP_ADDRESS:PORT',
|
| 155 |
}
|
| 156 |
|
| 157 |
try:
|
| 158 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
|
|
| 159 |
info_dict = ydl.extract_info(url, download=True)
|
| 160 |
downloaded_ext = info_dict.get('ext', 'mp4')
|
| 161 |
final_filename = f"{file_id}.{downloaded_ext}"
|
|
@@ -173,7 +176,7 @@ def download_video():
|
|
| 173 |
except Exception as e:
|
| 174 |
error_msg = str(e)
|
| 175 |
if "Sign in to confirm" in error_msg or "Private video" in error_msg:
|
| 176 |
-
return jsonify({'error': '
|
| 177 |
return jsonify({'error': f'خطا در دانلود: {error_msg}'}), 500
|
| 178 |
|
| 179 |
if __name__ == '__main__':
|
|
|
|
| 23 |
for filename in os.listdir(DOWNLOAD_FOLDER):
|
| 24 |
file_path = os.path.join(DOWNLOAD_FOLDER, filename)
|
| 25 |
if os.path.isfile(file_path):
|
| 26 |
+
# اگر فایل قدیمیتر از 3600 ثانیه (1 ساعت) بود، حذف شود
|
| 27 |
if current_time - os.path.getctime(file_path) > 3600:
|
| 28 |
os.remove(file_path)
|
| 29 |
except Exception as e:
|
| 30 |
print(f"Cleanup error: {e}")
|
| 31 |
time.sleep(3600)
|
| 32 |
|
| 33 |
+
# راهاندازی نخ پاکسازی در پسزمینه
|
| 34 |
cleanup_thread = threading.Thread(target=cleanup_old_files, daemon=True)
|
| 35 |
cleanup_thread.start()
|
| 36 |
|
|
|
|
| 100 |
throw new Error(data.error || 'خطا در دانلود ویدیو');
|
| 101 |
}
|
| 102 |
|
| 103 |
+
// دریافت فایل به صورت Blob
|
| 104 |
const blob = await response.blob();
|
| 105 |
const videoUrl = URL.createObjectURL(blob);
|
| 106 |
|
|
|
|
| 136 |
return jsonify({'error': 'URL is required'}), 400
|
| 137 |
|
| 138 |
url = data['url']
|
| 139 |
+
|
| 140 |
+
# شناسه یکتا برای فایل خروجی
|
| 141 |
file_id = str(uuid.uuid4())
|
| 142 |
output_template = os.path.join(DOWNLOAD_FOLDER, f"{file_id}.%(ext)s")
|
| 143 |
|
| 144 |
+
# تنظیمات yt-dlp همراه با سیستم بایپس یوتیوب
|
| 145 |
ydl_opts = {
|
| 146 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 147 |
+
'outtmpl': output_template,
|
| 148 |
+
'quiet': True,
|
| 149 |
+
'no_warnings': True,
|
| 150 |
'merge_output_format': 'mp4',
|
| 151 |
|
| 152 |
+
# 1. آدرس فایل کوکی که باید در کنار app.py آپلود شود
|
| 153 |
+
'cookiefile': 'cookies.txt',
|
|
|
|
| 154 |
|
| 155 |
+
# 2. تغییر کلاینت به موبایل/وب برای کاهش حساسیت رباتهای یوتیوب
|
| 156 |
+
'extractor_args': {'youtube': {'player_client': ['android', 'web']}},
|
|
|
|
|
|
|
| 157 |
}
|
| 158 |
|
| 159 |
try:
|
| 160 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 161 |
+
# استخراج اطلاعات و دانلود
|
| 162 |
info_dict = ydl.extract_info(url, download=True)
|
| 163 |
downloaded_ext = info_dict.get('ext', 'mp4')
|
| 164 |
final_filename = f"{file_id}.{downloaded_ext}"
|
|
|
|
| 176 |
except Exception as e:
|
| 177 |
error_msg = str(e)
|
| 178 |
if "Sign in to confirm" in error_msg or "Private video" in error_msg:
|
| 179 |
+
return jsonify({'error': 'آیپی سرور مسدود شده است. لطفا مطمئن شوید فایل cookies.txt را در روت پروژه آپلود کردهاید.'}), 403
|
| 180 |
return jsonify({'error': f'خطا در دانلود: {error_msg}'}), 500
|
| 181 |
|
| 182 |
if __name__ == '__main__':
|