FarelDeveloper commited on
Commit
4bc4c10
·
verified ·
1 Parent(s): 3af03c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -62
app.py CHANGED
@@ -1,79 +1,44 @@
1
- import os
2
  import random
3
- import re
4
- import string
5
- import gradio as gr
6
  import yt_dlp
7
 
8
- def generate_random_ipv6():
9
- """
10
- Menghasilkan IP acak dari range IPv6 jika HF Space mendukungnya.
11
- Ini trik jitu buat bypass rate-limit YouTube.
12
- """
13
- # Menggunakan prefix IPv6 acak (contoh range umum)
14
- prefix = "2001:4860:4860:"
15
- suffix = ":".join(f"{random.randint(0, 65535):x}" for _ in range(5))
16
- return prefix + suffix
17
-
18
- def get_random_user_agent():
19
- """Menghasilkan User-Agent acak agar tidak terdeteksi sebagai bot standar."""
20
- chrome_version = random.randint(110, 122)
21
- webkit_version = random.randint(500, 600)
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
- # Konfigurasi yt-dlp untuk bypass blokir
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
- # TRIK BYPASS: Coba pakai source address acak (IPv6)
49
- # Jika server HF kamu kebetulan tidak mendukung IPv6, baris ini otomatis dilewati yt-dlp
50
- try:
51
- ydl_opts['source_address'] = generate_random_ipv6()
52
- except Exception:
53
- pass
 
 
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
- # Dapatkan path file hasil download yang asli
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
- error_msg = str(e)
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: