File size: 2,514 Bytes
c51f20e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7a5713
 
 
 
03594f9
e7a5713
 
 
a29e162
e7a5713
 
 
 
 
 
 
 
 
 
 
 
 
 
03594f9
e7a5713
 
 
 
 
 
 
 
 
a29e162
e7a5713
 
 
 
03594f9
 
 
c51f20e
e7a5713
 
 
029c64a
e7a5713
 
 
 
 
 
029c64a
e7a5713
 
 
 
 
a29e162
e7a5713
 
a29e162
e7a5713
 
 
 
 
a29e162
e7a5713
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# --- SYSTEM PATCH: FORCE IPv4 ONLY (SAFE MODE) ---
import socket

# Simpan fungsi asli
_original_getaddrinfo = socket.getaddrinfo

# Buat fungsi pengganti yang membuang hasil IPv6
def new_getaddrinfo(*args, **kwargs):
    responses = _original_getaddrinfo(*args, **kwargs)
    # Hanya ambil yang jenisnya AF_INET (IPv4)
    return [res for res in responses if res[0] == socket.AF_INET]

# Pasang fungsi pengganti ke sistem
socket.getaddrinfo = new_getaddrinfo
# -------------------------------------------------

from flask import Flask, render_template, request, send_file
import yt_dlp
import os
import glob
import time

app = Flask(__name__)

# Folder download
DOWNLOAD_FOLDER = "downloads"
if not os.path.exists(DOWNLOAD_FOLDER):
    os.makedirs(DOWNLOAD_FOLDER)

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/download", methods=["POST"])
def download():
    url = request.form.get("url")
    quality = request.form.get("quality")
    format_type = request.form.get("format")

    # Bersihkan file lama
    files = glob.glob(os.path.join(DOWNLOAD_FOLDER, "*"))
    for f in files:
        try:
            os.remove(f)
        except:
            pass

    output_template = os.path.join(DOWNLOAD_FOLDER, "%(title)s.%(ext)s")

    # Konfigurasi yt-dlp
    ydl_opts = {
        "outtmpl": output_template,
        "format": f"bestvideo[height<={quality}]+bestaudio/best" if format_type == "mp4" else "bestaudio/best",
        "merge_output_format": "mp4",
        "nocheckcertificate": True,
        "quiet": True,
        "no_warnings": True,
        # Kita tidak perlu lagi "force_ipv4" disini karena sudah di-patch di sistem atas
    }

    if format_type == "mp3":
        ydl_opts.update({
            "format": "bestaudio/best",
            "postprocessors": [{
                "key": "FFmpegExtractAudio",
                "preferredcodec": "mp3",
                "preferredquality": "192",
            }],
        })

    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])
        
        time.sleep(1)
        list_of_files = glob.glob(os.path.join(DOWNLOAD_FOLDER, "*"))
        if not list_of_files:
            return "Gagal: File tidak ditemukan."
            
        latest_file = max(list_of_files, key=os.path.getctime)
        return send_file(latest_file, as_attachment=True)
    
    except Exception as e:
        return f"Terjadi kesalahan: {str(e)}"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)