Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,44 @@
|
|
| 1 |
-
import
|
| 2 |
import random
|
| 3 |
-
import re
|
| 4 |
-
import string
|
| 5 |
-
import gradio as gr
|
| 6 |
import yt_dlp
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return f"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/{webkit_version}.36 (KHTML, like Gecko) Chrome/{chrome_version}.0.0.0 Safari/{webkit_version}.36"
|
| 23 |
-
|
| 24 |
-
def download_video(url):
|
| 25 |
-
if not url:
|
| 26 |
-
return "Silakan masukkan URL terlebih dahulu!", None
|
| 27 |
-
|
| 28 |
-
# Buat nama file acak unik biar tidak bentrok antar user
|
| 29 |
-
random_str = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
|
| 30 |
-
outtmpl = f"downloads/%(title)s_{random_str}.%(ext)s"
|
| 31 |
|
| 32 |
-
|
| 33 |
ydl_opts = {
|
| 34 |
-
'outtmpl': outtmpl,
|
| 35 |
-
# Menggabungkan video terbaik dan audio terbaik (mp4)
|
| 36 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 37 |
'merge_output_format': 'mp4',
|
| 38 |
'quiet': True,
|
| 39 |
-
'no_warnings': True,
|
| 40 |
-
'http_headers': {
|
| 41 |
-
'User-Agent': get_random_user_agent(),
|
| 42 |
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
|
| 43 |
-
'Accept-Language': 'en-US,en;q=0.5',
|
| 44 |
-
'Sec-Fetch-Mode': 'navigate',
|
| 45 |
-
},
|
| 46 |
}
|
| 47 |
-
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
try:
|
| 56 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 57 |
-
# Ekstrak informasi info dulu
|
| 58 |
info = ydl.extract_info(url, download=True)
|
| 59 |
-
|
| 60 |
-
filename = ydl.prepare_filename(info)
|
| 61 |
-
|
| 62 |
-
# Kadang ekstensi berubah setelah di-merge (misal jadi .mp4)
|
| 63 |
-
if not os.path.exists(filename):
|
| 64 |
-
base, _ = os.path.splitext(filename)
|
| 65 |
-
filename = base + ".mp4"
|
| 66 |
-
|
| 67 |
-
if os.path.exists(filename):
|
| 68 |
-
return f"Sukses Mengunduh: {info.get('title', 'Video')}", filename
|
| 69 |
-
else:
|
| 70 |
-
return "File berhasil diproses, tapi lokasinya tidak ditemukan.", None
|
| 71 |
-
|
| 72 |
except Exception as e:
|
| 73 |
-
|
| 74 |
-
# Bersihkan pesan error dari info sensitif jika ada
|
| 75 |
-
clean_error = re.sub(r'http.*?\s', ' ', error_msg)
|
| 76 |
-
return f"Gagal mengunduh. Error: {clean_error}\n\nTips: Jika masih terblokir, coba klik download lagi untuk memicu IP acak baru.", None
|
| 77 |
|
| 78 |
# Membuat UI dengan Gradio
|
| 79 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 1 |
+
import requests
|
| 2 |
import random
|
|
|
|
|
|
|
|
|
|
| 3 |
import yt_dlp
|
| 4 |
|
| 5 |
+
def get_fresh_proxies():
|
| 6 |
+
"""Mengambil daftar proxy HTTP gratis yang masih aktif secara otomatis"""
|
| 7 |
+
try:
|
| 8 |
+
# Mengambil list proxy gratis dari API Geonode / ProxyScrape
|
| 9 |
+
url = "https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all"
|
| 10 |
+
response = requests.get(url, timeout=5)
|
| 11 |
+
if response.status_code == 200:
|
| 12 |
+
# Memisahkan string berdasarkan baris baru menjadi sebuah list
|
| 13 |
+
proxies = response.text.strip().split("\r\n")
|
| 14 |
+
# Format menjadi http://ip:port
|
| 15 |
+
return [f"http://{proxy}" for proxy in proxies if proxy]
|
| 16 |
+
except Exception:
|
| 17 |
+
pass
|
| 18 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def download_video_with_proxy(url):
|
| 21 |
ydl_opts = {
|
|
|
|
|
|
|
| 22 |
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 23 |
'merge_output_format': 'mp4',
|
| 24 |
'quiet': True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
+
|
| 27 |
+
# Ambil list proxy segar
|
| 28 |
+
active_proxies = get_fresh_proxies()
|
| 29 |
+
|
| 30 |
+
if active_proxies:
|
| 31 |
+
# Pilih 1 proxy acak dari ratusan proxy gratis yang didapat
|
| 32 |
+
chosen_proxy = random.choice(active_proxies)
|
| 33 |
+
ydl_opts['proxy'] = chosen_proxy
|
| 34 |
+
print(f"[INFO] Menggunakan Proxy: {chosen_proxy}")
|
| 35 |
|
| 36 |
try:
|
| 37 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
|
|
| 38 |
info = ydl.extract_info(url, download=True)
|
| 39 |
+
return "Sukses!", ydl.prepare_filename(info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
+
return f"Gagal menggunakan proxy ini. Error: {str(e)}", None
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Membuat UI dengan Gradio
|
| 44 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|