Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,43 @@ import os
|
|
| 2 |
import time
|
| 3 |
import uuid
|
| 4 |
import threading
|
|
|
|
|
|
|
|
|
|
| 5 |
from flask import Flask, request, jsonify, send_file, render_template_string
|
| 6 |
from flask_cors import CORS
|
| 7 |
import yt_dlp
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
app = Flask(__name__)
|
| 10 |
CORS(app)
|
| 11 |
|
|
@@ -126,15 +159,14 @@ def download_video():
|
|
| 126 |
file_id = str(uuid.uuid4())
|
| 127 |
output_template = os.path.join(DOWNLOAD_FOLDER, f"{file_id}.%(ext)s")
|
| 128 |
|
| 129 |
-
# تنظیمات پیشرفته دانلودر
|
| 130 |
ydl_opts = {
|
| 131 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 132 |
'outtmpl': output_template,
|
| 133 |
'quiet': True,
|
| 134 |
'no_warnings': True,
|
| 135 |
'merge_output_format': 'mp4',
|
| 136 |
-
'force_ipv4': True,
|
| 137 |
-
'source_address': '0.0.0.0', # اتصال مستقیم به سوکت شبکه برای حل خطای Errno -5
|
| 138 |
'nocheckcertificate': True, # نادیده گرفتن خطاهای گواهی
|
| 139 |
}
|
| 140 |
|
|
@@ -158,7 +190,7 @@ def download_video():
|
|
| 158 |
except Exception as e:
|
| 159 |
error_msg = str(e)
|
| 160 |
if "Sign in to confirm" in error_msg or "Private video" in error_msg:
|
| 161 |
-
return jsonify({'error': '
|
| 162 |
return jsonify({'error': error_msg}), 500
|
| 163 |
|
| 164 |
if __name__ == '__main__':
|
|
|
|
| 2 |
import time
|
| 3 |
import uuid
|
| 4 |
import threading
|
| 5 |
+
import socket
|
| 6 |
+
import json
|
| 7 |
+
import urllib.request
|
| 8 |
from flask import Flask, request, jsonify, send_file, render_template_string
|
| 9 |
from flask_cors import CORS
|
| 10 |
import yt_dlp
|
| 11 |
|
| 12 |
+
# ==========================================
|
| 13 |
+
# دور زدن محدودیت و قطعی DNS در هاگینگفیس (Hugging Face)
|
| 14 |
+
# این بخش ترافیک دامنههای مسدود شده را از طریق سیستم DNS گوگل هدایت میکند
|
| 15 |
+
# ==========================================
|
| 16 |
+
_orig_getaddrinfo = socket.getaddrinfo
|
| 17 |
+
|
| 18 |
+
def patched_getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
|
| 19 |
+
blocked_domains =['youtube', 'youtu.be', 'googlevideo', 'instagram', 'cdninstagram']
|
| 20 |
+
if host and any(d in host.lower() for d in blocked_domains):
|
| 21 |
+
try:
|
| 22 |
+
# دریافت IP واقعی سرور از طریق Google DNS-over-HTTPS
|
| 23 |
+
url = f"https://dns.google/resolve?name={host}&type=A"
|
| 24 |
+
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
| 25 |
+
with urllib.request.urlopen(req, timeout=5) as response:
|
| 26 |
+
data = json.loads(response.read().decode('utf-8'))
|
| 27 |
+
if 'Answer' in data:
|
| 28 |
+
for ans in data['Answer']:
|
| 29 |
+
if ans['type'] == 1: # رکورد IPv4
|
| 30 |
+
ip = ans['data']
|
| 31 |
+
return[(socket.AF_INET, type or socket.SOCK_STREAM, proto or 6, '', (ip, port))]
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"DoH fallback failed for {host}: {e}")
|
| 34 |
+
|
| 35 |
+
# برای سایر سایتها از همان سیستم عادی استفاده کن
|
| 36 |
+
return _orig_getaddrinfo(host, port, family, type, proto, flags)
|
| 37 |
+
|
| 38 |
+
# جایگزینی سیستم DNS اصلی پایتون با سیستم دور زدن محدودیت ما
|
| 39 |
+
socket.getaddrinfo = patched_getaddrinfo
|
| 40 |
+
# ==========================================
|
| 41 |
+
|
| 42 |
app = Flask(__name__)
|
| 43 |
CORS(app)
|
| 44 |
|
|
|
|
| 159 |
file_id = str(uuid.uuid4())
|
| 160 |
output_template = os.path.join(DOWNLOAD_FOLDER, f"{file_id}.%(ext)s")
|
| 161 |
|
| 162 |
+
# تنظیمات پیشرفته دانلودر
|
| 163 |
ydl_opts = {
|
| 164 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 165 |
'outtmpl': output_template,
|
| 166 |
'quiet': True,
|
| 167 |
'no_warnings': True,
|
| 168 |
'merge_output_format': 'mp4',
|
| 169 |
+
'force_ipv4': True, # اجبار به استفاده از IPv4 (مهم برای سرورهای ابری)
|
|
|
|
| 170 |
'nocheckcertificate': True, # نادیده گرفتن خطاهای گواهی
|
| 171 |
}
|
| 172 |
|
|
|
|
| 190 |
except Exception as e:
|
| 191 |
error_msg = str(e)
|
| 192 |
if "Sign in to confirm" in error_msg or "Private video" in error_msg:
|
| 193 |
+
return jsonify({'error': 'این ویدیو خصوصی است یا نیاز به اکانت دارد'}), 403
|
| 194 |
return jsonify({'error': error_msg}), 500
|
| 195 |
|
| 196 |
if __name__ == '__main__':
|