Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Flask, render_template, request, send_file
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
import glob
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
# Folder untuk menyimpan file sementara
|
| 9 |
DOWNLOAD_FOLDER = "downloads"
|
| 10 |
if not os.path.exists(DOWNLOAD_FOLDER):
|
| 11 |
os.makedirs(DOWNLOAD_FOLDER)
|
|
@@ -20,7 +31,7 @@ def download():
|
|
| 20 |
quality = request.form.get("quality")
|
| 21 |
format_type = request.form.get("format")
|
| 22 |
|
| 23 |
-
# Bersihkan
|
| 24 |
files = glob.glob(os.path.join(DOWNLOAD_FOLDER, "*"))
|
| 25 |
for f in files:
|
| 26 |
try:
|
|
@@ -28,19 +39,21 @@ def download():
|
|
| 28 |
except:
|
| 29 |
pass
|
| 30 |
|
| 31 |
-
# Template nama file output
|
| 32 |
output_template = os.path.join(DOWNLOAD_FOLDER, "%(title)s.%(ext)s")
|
| 33 |
|
| 34 |
-
# Konfigurasi
|
| 35 |
ydl_opts = {
|
| 36 |
"outtmpl": output_template,
|
| 37 |
"format": f"bestvideo[height<={quality}]+bestaudio/best" if format_type == "mp4" else "bestaudio/best",
|
| 38 |
"merge_output_format": "mp4",
|
| 39 |
-
"
|
| 40 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
-
# Jika user minta MP3
|
| 44 |
if format_type == "mp3":
|
| 45 |
ydl_opts.update({
|
| 46 |
"format": "bestaudio/best",
|
|
@@ -55,16 +68,17 @@ def download():
|
|
| 55 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 56 |
ydl.download([url])
|
| 57 |
|
| 58 |
-
#
|
|
|
|
| 59 |
list_of_files = glob.glob(os.path.join(DOWNLOAD_FOLDER, "*"))
|
| 60 |
if not list_of_files:
|
| 61 |
-
return "Gagal:
|
| 62 |
|
| 63 |
latest_file = max(list_of_files, key=os.path.getctime)
|
| 64 |
return send_file(latest_file, as_attachment=True)
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
-
return f"
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
# --- BAGIAN INI WAJIB ADA DI PALING ATAS ---
|
| 2 |
+
import socket
|
| 3 |
+
# Trik memaksa sistem menggunakan IPv4 agar tidak error DNS
|
| 4 |
+
def new_getaddrinfo(*args, **kwargs):
|
| 5 |
+
responses = socket.getaddrinfo.original(*args, **kwargs)
|
| 6 |
+
return [res for res in responses if res[0] == socket.AF_INET]
|
| 7 |
+
|
| 8 |
+
socket.getaddrinfo.original = socket.getaddrinfo
|
| 9 |
+
socket.getaddrinfo = new_getaddrinfo
|
| 10 |
+
# -------------------------------------------
|
| 11 |
+
|
| 12 |
from flask import Flask, render_template, request, send_file
|
| 13 |
import yt_dlp
|
| 14 |
import os
|
| 15 |
import glob
|
| 16 |
+
import time
|
| 17 |
|
| 18 |
app = Flask(__name__)
|
| 19 |
|
|
|
|
| 20 |
DOWNLOAD_FOLDER = "downloads"
|
| 21 |
if not os.path.exists(DOWNLOAD_FOLDER):
|
| 22 |
os.makedirs(DOWNLOAD_FOLDER)
|
|
|
|
| 31 |
quality = request.form.get("quality")
|
| 32 |
format_type = request.form.get("format")
|
| 33 |
|
| 34 |
+
# Bersihkan file lama
|
| 35 |
files = glob.glob(os.path.join(DOWNLOAD_FOLDER, "*"))
|
| 36 |
for f in files:
|
| 37 |
try:
|
|
|
|
| 39 |
except:
|
| 40 |
pass
|
| 41 |
|
|
|
|
| 42 |
output_template = os.path.join(DOWNLOAD_FOLDER, "%(title)s.%(ext)s")
|
| 43 |
|
| 44 |
+
# Konfigurasi Anti-Gagal
|
| 45 |
ydl_opts = {
|
| 46 |
"outtmpl": output_template,
|
| 47 |
"format": f"bestvideo[height<={quality}]+bestaudio/best" if format_type == "mp4" else "bestaudio/best",
|
| 48 |
"merge_output_format": "mp4",
|
| 49 |
+
"nocheckcertificate": True,
|
| 50 |
+
"ignoreerrors": True,
|
| 51 |
+
"quiet": True,
|
| 52 |
+
"no_warnings": True,
|
| 53 |
+
# Menggunakan User Agent Android agar dianggap HP (lebih stabil)
|
| 54 |
+
"user_agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36",
|
| 55 |
}
|
| 56 |
|
|
|
|
| 57 |
if format_type == "mp3":
|
| 58 |
ydl_opts.update({
|
| 59 |
"format": "bestaudio/best",
|
|
|
|
| 68 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 69 |
ydl.download([url])
|
| 70 |
|
| 71 |
+
# Cek hasil download
|
| 72 |
+
time.sleep(1) # Beri waktu sebentar
|
| 73 |
list_of_files = glob.glob(os.path.join(DOWNLOAD_FOLDER, "*"))
|
| 74 |
if not list_of_files:
|
| 75 |
+
return "Gagal: Video tidak bisa diunduh. Pastikan link bukan Playlist/Mix."
|
| 76 |
|
| 77 |
latest_file = max(list_of_files, key=os.path.getctime)
|
| 78 |
return send_file(latest_file, as_attachment=True)
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"Error Sistem: {str(e)}"
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
app.run(host="0.0.0.0", port=7860)
|