Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,41 +8,30 @@ import gradio as gr
|
|
8 |
import requests
|
9 |
from bs4 import BeautifulSoup
|
10 |
import re
|
|
|
11 |
|
12 |
-
# Function to search YouTube videos using
|
13 |
def youtube_search(query, max_results=50):
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
}
|
|
|
18 |
|
19 |
try:
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
video_id = video['href'].split('=')[-1]
|
35 |
-
video_title = video.get_text(strip=True)
|
36 |
-
thumbnail_url = f"https://img.youtube.com/vi/{video_id}/mqdefault.jpg"
|
37 |
-
|
38 |
-
# Append tuple (thumbnail, video ID)
|
39 |
-
gallery_items.append((thumbnail_url, video_id))
|
40 |
-
|
41 |
-
return gallery_items
|
42 |
-
|
43 |
-
except requests.exceptions.RequestException as e:
|
44 |
-
# Print the error message to help debug issues
|
45 |
-
print(f"Error during YouTube web scraping request: {e}")
|
46 |
return []
|
47 |
|
48 |
# Function to display the video using the video URL
|
|
|
8 |
import requests
|
9 |
from bs4 import BeautifulSoup
|
10 |
import re
|
11 |
+
import yt_dlp
|
12 |
|
13 |
+
# Function to search YouTube videos using yt-dlp for better reliability
|
14 |
def youtube_search(query, max_results=50):
|
15 |
+
ydl_opts = {
|
16 |
+
'quiet': True,
|
17 |
+
'extract_flat': 'in_playlist',
|
18 |
}
|
19 |
+
search_url = f"ytsearch{max_results}:{query}"
|
20 |
|
21 |
try:
|
22 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
23 |
+
result = ydl.extract_info(search_url, download=False)
|
24 |
+
gallery_items = []
|
25 |
+
|
26 |
+
if 'entries' in result:
|
27 |
+
for entry in result['entries']:
|
28 |
+
video_id = entry.get('id')
|
29 |
+
thumbnail_url = entry.get('thumbnail')
|
30 |
+
if video_id and thumbnail_url:
|
31 |
+
gallery_items.append((thumbnail_url, video_id))
|
32 |
+
return gallery_items
|
33 |
+
except Exception as e:
|
34 |
+
print(f"Error during YouTube yt-dlp request: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
return []
|
36 |
|
37 |
# Function to display the video using the video URL
|