Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,3 @@
|
|
| 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
|
|
@@ -17,6 +6,7 @@ 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)
|
|
@@ -41,17 +31,15 @@ def download():
|
|
| 41 |
|
| 42 |
output_template = os.path.join(DOWNLOAD_FOLDER, "%(title)s.%(ext)s")
|
| 43 |
|
| 44 |
-
# Konfigurasi
|
| 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 |
-
"
|
| 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":
|
|
@@ -68,17 +56,16 @@ def download():
|
|
| 68 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 69 |
ydl.download([url])
|
| 70 |
|
| 71 |
-
|
| 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:
|
| 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"
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Flask, render_template, request, send_file
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# Folder download
|
| 10 |
DOWNLOAD_FOLDER = "downloads"
|
| 11 |
if not os.path.exists(DOWNLOAD_FOLDER):
|
| 12 |
os.makedirs(DOWNLOAD_FOLDER)
|
|
|
|
| 31 |
|
| 32 |
output_template = os.path.join(DOWNLOAD_FOLDER, "%(title)s.%(ext)s")
|
| 33 |
|
| 34 |
+
# Konfigurasi yt-dlp
|
| 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 |
"nocheckcertificate": True,
|
| 40 |
+
"force_ipv4": True, # Kita pakai fitur bawaan ini saja, LEBIH AMAN
|
| 41 |
"quiet": True,
|
| 42 |
"no_warnings": True,
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
if format_type == "mp3":
|
|
|
|
| 56 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 57 |
ydl.download([url])
|
| 58 |
|
| 59 |
+
time.sleep(1)
|
|
|
|
| 60 |
list_of_files = glob.glob(os.path.join(DOWNLOAD_FOLDER, "*"))
|
| 61 |
if not list_of_files:
|
| 62 |
+
return "Gagal: File tidak ditemukan."
|
| 63 |
|
| 64 |
latest_file = max(list_of_files, key=os.path.getctime)
|
| 65 |
return send_file(latest_file, as_attachment=True)
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
+
return f"Terjadi kesalahan: {str(e)}"
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
app.run(host="0.0.0.0", port=7860)
|