mobenta commited on
Commit
99ade4d
·
verified ·
1 Parent(s): 9bc5941

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -2,7 +2,7 @@ import subprocess
2
  import sys
3
  import random
4
  import time
5
- import os # <-- Added import for os module to handle file checks
6
 
7
  # Ensure compatible versions of httpx and httpcore are installed
8
  subprocess.check_call([sys.executable, "-m", "pip", "install", "httpx==0.18.2", "httpcore==0.13.6"])
@@ -33,24 +33,36 @@ def get_random_proxy():
33
 
34
  # Function to search YouTube videos using yt-dlp for better reliability
35
  def youtube_search(query, max_results=10):
36
- cookies_file = "cookies.txt" # You need to provide this file with cookies exported from YouTube
37
  proxies_attempted = 0
38
  max_proxy_attempts = len(PROXIES)
39
  success = False
40
  gallery_items = []
41
  error_message = ""
42
 
43
- while not success and proxies_attempted < max_proxy_attempts:
44
- proxy = get_random_proxy()
45
- logging.debug(f"Trying proxy: {proxy}")
46
- ydl_opts = {
47
- 'quiet': False, # Set to False to get more detailed output from yt-dlp
48
- 'logger': logging.getLogger(), # Use the logging module to capture yt-dlp logs
49
- 'simulate': True,
50
- 'noplaylist': True, # Avoid extracting playlists
51
- 'format': 'best',
52
- 'proxy': f'http://{proxy}',
53
- }
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  if cookies_file and os.path.exists(cookies_file):
56
  ydl_opts['cookiefile'] = cookies_file
@@ -103,10 +115,10 @@ def youtube_search(query, max_results=10):
103
  def show_video(video_url):
104
  video_id = None
105
  patterns = [
106
- r"youtube\\.com/watch\\?v=([^&?\\/]+)",
107
- r"youtube\\.com/embed/([^&?\\/]+)",
108
- r"youtube\\.com/v/([^&?\\/]+)",
109
- r"youtu\\.be/([^&?\\/]+)"
110
  ]
111
 
112
  for pattern in patterns:
@@ -151,7 +163,7 @@ with gr.Blocks() as demo:
151
  if error_message:
152
  return [], error_message, gr.update(visible=True)
153
 
154
- gallery_items_display = [(item["thumbnail"], f"{item['title']}\\n{item['description']}", item["video_id"]) for item in gallery_items]
155
 
156
  return gallery_items_display, "", gr.update(visible=False)
157
 
@@ -169,4 +181,4 @@ with gr.Blocks() as demo:
169
  search_output.select(on_video_select, inputs=None, outputs=selected_video_link)
170
  play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
171
 
172
- demo.launch()
 
2
  import sys
3
  import random
4
  import time
5
+ import os # Added import for os module to handle file checks
6
 
7
  # Ensure compatible versions of httpx and httpcore are installed
8
  subprocess.check_call([sys.executable, "-m", "pip", "install", "httpx==0.18.2", "httpcore==0.13.6"])
 
33
 
34
  # Function to search YouTube videos using yt-dlp for better reliability
35
  def youtube_search(query, max_results=10):
36
+ cookies_file = "cookies.txt"
37
  proxies_attempted = 0
38
  max_proxy_attempts = len(PROXIES)
39
  success = False
40
  gallery_items = []
41
  error_message = ""
42
 
43
+ while not success and proxies_attempted < max_proxy_attempts + 1: # +1 to allow fallback without a proxy
44
+ if proxies_attempted < max_proxy_attempts:
45
+ proxy = get_random_proxy()
46
+ logging.debug(f"Trying proxy: {proxy}")
47
+ ydl_opts = {
48
+ 'quiet': False,
49
+ 'logger': logging.getLogger(),
50
+ 'simulate': True,
51
+ 'noplaylist': True,
52
+ 'format': 'best',
53
+ 'proxy': f'http://{proxy}',
54
+ 'socket_timeout': 30 # Extended timeout
55
+ }
56
+ else:
57
+ logging.debug("All proxies failed. Trying without a proxy.")
58
+ ydl_opts = {
59
+ 'quiet': False,
60
+ 'logger': logging.getLogger(),
61
+ 'simulate': True,
62
+ 'noplaylist': True,
63
+ 'format': 'best',
64
+ 'socket_timeout': 30 # Extended timeout
65
+ }
66
 
67
  if cookies_file and os.path.exists(cookies_file):
68
  ydl_opts['cookiefile'] = cookies_file
 
115
  def show_video(video_url):
116
  video_id = None
117
  patterns = [
118
+ r"youtube\.com/watch\?v=([^&?\/]+)",
119
+ r"youtube\.com/embed/([^&?\/]+)",
120
+ r"youtube\.com/v/([^&?\/]+)",
121
+ r"youtu\.be/([^&?\/]+)"
122
  ]
123
 
124
  for pattern in patterns:
 
163
  if error_message:
164
  return [], error_message, gr.update(visible=True)
165
 
166
+ gallery_items_display = [(item["thumbnail"], f"{item['title']}\n{item['description']}", item["video_id"]) for item in gallery_items]
167
 
168
  return gallery_items_display, "", gr.update(visible=False)
169
 
 
181
  search_output.select(on_video_select, inputs=None, outputs=selected_video_link)
182
  play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
183
 
184
+ demo.launch()