File size: 2,628 Bytes
638d33e
c51f20e
638d33e
f9f9f4e
638d33e
 
f9f9f4e
c51f20e
f9f9f4e
 
638d33e
c51f20e
e7a5713
 
 
 
03594f9
e7a5713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03594f9
e7a5713
 
 
 
 
 
 
 
 
638d33e
e7a5713
 
 
 
04cb2a0
03594f9
 
638d33e
 
 
e7a5713
 
 
029c64a
e7a5713
 
 
 
 
 
029c64a
e7a5713
 
 
 
 
a29e162
e7a5713
 
638d33e
e7a5713
 
 
 
 
f9f9f4e
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
# --- SYSTEM PATCH ---
import socket
# Bypass DNS standar
def custom_dns_lookup(host, port=0, family=0, type=0, proto=0, flags=0):
    # Biarkan sistem memilih jalur terbaik (IPv4/IPv6) secara alami
    # Karena kita akan pakai Cookies, biasanya IP tidak dipermasalahkan lagi
    return socket.getaddrinfo_original(host, port, family, type, proto, flags)

socket.getaddrinfo_original = socket.getaddrinfo
socket.getaddrinfo = custom_dns_lookup
# --------------------

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

app = Flask(__name__)

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 DENGAN COOKIES (SOLUSI FINAL "SIGN IN")
    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,
        # INI KUNCINYA: Membaca file cookies.txt yang Anda upload
        "cookiefile": "cookies.txt", 
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
    }

    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: Video 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"Error: {str(e)}"

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