Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,10 @@ 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):
|
@@ -18,20 +22,28 @@ def youtube_search(query, max_results=50):
|
|
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 |
-
|
35 |
return []
|
36 |
|
37 |
# Function to display the video using the video URL
|
@@ -49,14 +61,17 @@ def show_video(video_url):
|
|
49 |
match = re.search(pattern, video_url)
|
50 |
if match:
|
51 |
video_id = match.group(1)
|
|
|
52 |
break
|
53 |
|
54 |
# If no video ID is found, return an error message
|
55 |
if not video_id:
|
|
|
56 |
return "Invalid YouTube URL. Please enter a valid YouTube video link."
|
57 |
|
58 |
# Create the embed URL
|
59 |
embed_url = f"https://www.youtube.com/embed/{video_id}"
|
|
|
60 |
|
61 |
# Return an iframe with the video
|
62 |
html_code = f'''
|
@@ -84,6 +99,7 @@ with gr.Blocks() as demo:
|
|
84 |
# Define search button behavior
|
85 |
def update_search_results(query):
|
86 |
gallery_items = youtube_search(query)
|
|
|
87 |
return gallery_items
|
88 |
|
89 |
# Update the selected video link field when a video is clicked in the gallery
|
@@ -91,10 +107,12 @@ with gr.Blocks() as demo:
|
|
91 |
# Extract the video ID from the event value, which is a dictionary containing details of the selected item
|
92 |
selected_video_id = evt.value["caption"]
|
93 |
video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
|
|
|
94 |
return video_url
|
95 |
|
96 |
# Play the video when the Play Video button is clicked
|
97 |
def play_video(video_url):
|
|
|
98 |
return show_video(video_url)
|
99 |
|
100 |
search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
|
|
|
9 |
from bs4 import BeautifulSoup
|
10 |
import re
|
11 |
import yt_dlp
|
12 |
+
import logging
|
13 |
+
|
14 |
+
# Configure logging for debugging purposes
|
15 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
16 |
|
17 |
# Function to search YouTube videos using yt-dlp for better reliability
|
18 |
def youtube_search(query, max_results=50):
|
|
|
22 |
}
|
23 |
search_url = f"ytsearch{max_results}:{query}"
|
24 |
|
25 |
+
logging.debug(f"Starting YouTube search for query: {query}")
|
26 |
+
|
27 |
try:
|
28 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
29 |
result = ydl.extract_info(search_url, download=False)
|
30 |
gallery_items = []
|
31 |
|
32 |
if 'entries' in result:
|
33 |
+
logging.debug(f"Number of entries found: {len(result['entries'])}")
|
34 |
for entry in result['entries']:
|
35 |
video_id = entry.get('id')
|
36 |
thumbnail_url = entry.get('thumbnail')
|
37 |
if video_id and thumbnail_url:
|
38 |
gallery_items.append((thumbnail_url, video_id))
|
39 |
+
logging.debug(f"Added video: ID={video_id}, Thumbnail={thumbnail_url}")
|
40 |
+
else:
|
41 |
+
logging.debug(f"Missing video ID or thumbnail for entry: {entry}")
|
42 |
+
else:
|
43 |
+
logging.warning("No entries found in search result.")
|
44 |
return gallery_items
|
45 |
except Exception as e:
|
46 |
+
logging.error(f"Error during YouTube yt-dlp request: {e}")
|
47 |
return []
|
48 |
|
49 |
# Function to display the video using the video URL
|
|
|
61 |
match = re.search(pattern, video_url)
|
62 |
if match:
|
63 |
video_id = match.group(1)
|
64 |
+
logging.debug(f"Extracted video ID: {video_id}")
|
65 |
break
|
66 |
|
67 |
# If no video ID is found, return an error message
|
68 |
if not video_id:
|
69 |
+
logging.error("Invalid YouTube URL. Please enter a valid YouTube video link.")
|
70 |
return "Invalid YouTube URL. Please enter a valid YouTube video link."
|
71 |
|
72 |
# Create the embed URL
|
73 |
embed_url = f"https://www.youtube.com/embed/{video_id}"
|
74 |
+
logging.debug(f"Embed URL generated: {embed_url}")
|
75 |
|
76 |
# Return an iframe with the video
|
77 |
html_code = f'''
|
|
|
99 |
# Define search button behavior
|
100 |
def update_search_results(query):
|
101 |
gallery_items = youtube_search(query)
|
102 |
+
logging.debug(f"Number of gallery items returned: {len(gallery_items)}")
|
103 |
return gallery_items
|
104 |
|
105 |
# Update the selected video link field when a video is clicked in the gallery
|
|
|
107 |
# Extract the video ID from the event value, which is a dictionary containing details of the selected item
|
108 |
selected_video_id = evt.value["caption"]
|
109 |
video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
|
110 |
+
logging.debug(f"Video selected: {video_url}")
|
111 |
return video_url
|
112 |
|
113 |
# Play the video when the Play Video button is clicked
|
114 |
def play_video(video_url):
|
115 |
+
logging.debug(f"Playing video with URL: {video_url}")
|
116 |
return show_video(video_url)
|
117 |
|
118 |
search_button.click(update_search_results, inputs=search_query_input, outputs=search_output)
|