lexicalspace commited on
Commit
6ae83d0
Β·
verified Β·
1 Parent(s): 18b9f23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -31
app.py CHANGED
@@ -2,29 +2,20 @@ import yt_dlp
2
  import os
3
  import gradio as gr
4
  import re
5
- from typing import Optional, Dict, Any, List
6
- import socket
7
-
8
- # Force IPv4 (fixes many container DNS issues)
9
- def force_ipv4():
10
- orig_getaddrinfo = socket.getaddrinfo
11
-
12
- def new_getaddrinfo(*args, **kwargs):
13
- responses = orig_getaddrinfo(*args, **kwargs)
14
- return [r for r in responses if r[0] == socket.AF_INET]
15
-
16
- socket.getaddrinfo = new_getaddrinfo
17
-
18
- force_ipv4()
19
 
20
  OUTPUT_DIR = "downloads"
21
  os.makedirs(OUTPUT_DIR, exist_ok=True)
22
 
23
- # 1. Modified Engine to return the FILE PATH, not just a list of names
24
- def core_download(url, progress=gr.Progress()):
25
  filepaths = []
26
 
27
- # Custom hook to update Gradio progress bar
 
 
 
 
 
28
  def progress_hook(d):
29
  if d['status'] == 'downloading':
30
  try:
@@ -44,6 +35,9 @@ def core_download(url, progress=gr.Progress()):
44
  }],
45
  'progress_hooks': [progress_hook],
46
  'noplaylist': True,
 
 
 
47
  }
48
 
49
  try:
@@ -53,46 +47,46 @@ def core_download(url, progress=gr.Progress()):
53
  final_filename = os.path.splitext(filename)[0] + ".mp3"
54
 
55
  if os.path.exists(final_filename):
56
- return final_filename, f"βœ… Success: {os.path.basename(final_filename)}"
57
  else:
58
- return None, "❌ Error: File not found after download."
59
 
60
  except Exception as e:
61
- return None, f"❌ Error: {str(e)}"
 
62
 
63
- # 2. UI Wrapper that yields updates
64
- def ui_logic(url):
65
  if not url:
66
  yield None, "Please enter a URL.", "Waiting..."
67
  return
68
 
69
  yield None, "Starting download...", "Initializing..."
70
 
71
- file_path, status_msg = core_download(url)
72
 
73
- # Return the actual file path to the Gradio File component
74
- yield file_path, status_msg, "Complete!"
75
 
76
- # 3. Improved UI Layout
77
  with gr.Blocks(title="UltraMax Downloader") as app:
78
  gr.Markdown("## 🎡 UltraMax YouTube to MP3")
79
 
80
  with gr.Row():
81
  url_input = gr.Textbox(label="YouTube URL", placeholder="Paste link here...", scale=3)
82
- btn = gr.Button("Download", variant="primary", scale=1)
 
 
 
83
 
84
- # Output components
85
  with gr.Row():
86
- # This component lets the user actually download the file
87
  file_output = gr.File(label="Download MP3 Here")
88
-
89
  with gr.Column():
90
  status_text = gr.Textbox(label="Status")
91
  log_text = gr.Textbox(label="System Log")
92
 
93
  btn.click(
94
  fn=ui_logic,
95
- inputs=url_input,
96
  outputs=[file_output, status_text, log_text]
97
  )
98
 
 
2
  import os
3
  import gradio as gr
4
  import re
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  OUTPUT_DIR = "downloads"
7
  os.makedirs(OUTPUT_DIR, exist_ok=True)
8
 
9
+ # 1. Modified Engine to accept Proxy
10
+ def core_download(url, proxy_url=None, progress=gr.Progress()):
11
  filepaths = []
12
 
13
+ # Clean up the proxy string (strip spaces)
14
+ if proxy_url:
15
+ proxy_url = proxy_url.strip()
16
+ if proxy_url == "":
17
+ proxy_url = None
18
+
19
  def progress_hook(d):
20
  if d['status'] == 'downloading':
21
  try:
 
35
  }],
36
  'progress_hooks': [progress_hook],
37
  'noplaylist': True,
38
+ # KEY FIXES:
39
+ 'force_ipv4': True, # 1. Force IPv4 to fix DNS resolution errors
40
+ 'proxy': proxy_url, # 2. Use the user-provided Proxy IP
41
  }
42
 
43
  try:
 
47
  final_filename = os.path.splitext(filename)[0] + ".mp3"
48
 
49
  if os.path.exists(final_filename):
50
+ return final_filename, f"βœ… Success: {os.path.basename(final_filename)}", "Download complete!"
51
  else:
52
+ return None, "❌ Error: File not found.", "Failed."
53
 
54
  except Exception as e:
55
+ # Detailed error logging
56
+ return None, f"❌ Error: {str(e)}", f"Failed log: {str(e)}"
57
 
58
+ # 2. UI Wrapper
59
+ def ui_logic(url, proxy):
60
  if not url:
61
  yield None, "Please enter a URL.", "Waiting..."
62
  return
63
 
64
  yield None, "Starting download...", "Initializing..."
65
 
66
+ file_path, status_msg, log_msg = core_download(url, proxy)
67
 
68
+ yield file_path, status_msg, log_msg
 
69
 
70
+ # 3. Improved UI with Proxy Field
71
  with gr.Blocks(title="UltraMax Downloader") as app:
72
  gr.Markdown("## 🎡 UltraMax YouTube to MP3")
73
 
74
  with gr.Row():
75
  url_input = gr.Textbox(label="YouTube URL", placeholder="Paste link here...", scale=3)
76
+ # NEW FIELD FOR IP/PROXY
77
+ proxy_input = gr.Textbox(label="Proxy IP (Optional)", placeholder="http://user:pass@ip:port", scale=1)
78
+
79
+ btn = gr.Button("Download", variant="primary")
80
 
 
81
  with gr.Row():
 
82
  file_output = gr.File(label="Download MP3 Here")
 
83
  with gr.Column():
84
  status_text = gr.Textbox(label="Status")
85
  log_text = gr.Textbox(label="System Log")
86
 
87
  btn.click(
88
  fn=ui_logic,
89
+ inputs=[url_input, proxy_input],
90
  outputs=[file_output, status_text, log_text]
91
  )
92