Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -42,6 +42,7 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
|
|
42 |
params = {
|
43 |
"part": "snippet",
|
44 |
"q": query,
|
|
|
45 |
"maxResults": 50,
|
46 |
"order": sort_by
|
47 |
}
|
@@ -53,7 +54,7 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
|
|
53 |
params["publishedAfter"] = published_after
|
54 |
|
55 |
if video_type and video_type != "All":
|
56 |
-
params["type"] = video_type.lower()
|
57 |
|
58 |
if duration:
|
59 |
duration_mapping = {
|
@@ -75,31 +76,11 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
|
|
75 |
|
76 |
results = response.json().get("items", [])
|
77 |
for result in results:
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
'title': result["snippet"]["title"],
|
84 |
-
'type': "video"
|
85 |
-
}
|
86 |
-
elif result["id"]["kind"] == "youtube#channel":
|
87 |
-
video_info = {
|
88 |
-
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
89 |
-
'id': result["id"]["channelId"],
|
90 |
-
'title': result["snippet"]["title"],
|
91 |
-
'type': "channel"
|
92 |
-
}
|
93 |
-
elif result["id"]["kind"] == "youtube#playlist":
|
94 |
-
video_info = {
|
95 |
-
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
96 |
-
'id': result["id"]["playlistId"],
|
97 |
-
'title': result["snippet"]["title"],
|
98 |
-
'type': "playlist"
|
99 |
-
}
|
100 |
-
else:
|
101 |
-
continue # Skip if it's an unknown type
|
102 |
-
|
103 |
all_results.append(video_info)
|
104 |
|
105 |
if 'nextPageToken' not in response.json() or len(all_results) >= max_results:
|
@@ -113,16 +94,26 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
|
|
113 |
print(f"Error during YouTube API request: {e}")
|
114 |
return []
|
115 |
|
116 |
-
def show_video(
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
return "Invalid YouTube URL. Please enter a valid YouTube video link."
|
125 |
|
|
|
|
|
126 |
html_code = f'''
|
127 |
<iframe width="100%" height="562" src="{embed_url}"
|
128 |
frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
@@ -186,27 +177,25 @@ with gr.Blocks(css="""
|
|
186 |
search_results = youtube_search(query, upload_date, video_type, duration, sort_by)
|
187 |
gallery_items = []
|
188 |
video_ids = []
|
189 |
-
video_types = []
|
190 |
for item in search_results:
|
191 |
image_url = item['thumbnail_url']
|
192 |
title = item['title']
|
193 |
caption = f"{title}"
|
194 |
gallery_items.append((image_url, caption))
|
195 |
-
video_ids.append(item['
|
196 |
-
|
197 |
-
return gallery_items, video_ids, video_types
|
198 |
|
199 |
-
def on_video_select(evt: gr.SelectData, video_ids
|
200 |
index = evt.index
|
201 |
selected_video_id = video_ids[index]
|
202 |
-
|
203 |
-
return
|
204 |
|
205 |
-
def play_video(
|
206 |
-
return show_video(
|
207 |
|
208 |
search_button.click(update_search_results, inputs=[search_query_input, upload_date_input, video_type_input, duration_input, sort_by_input], outputs=[search_output, video_ids_state])
|
209 |
-
search_output.select(on_video_select, inputs=
|
210 |
-
play_video_button.click(play_video, inputs=
|
211 |
|
212 |
demo.launch()
|
|
|
42 |
params = {
|
43 |
"part": "snippet",
|
44 |
"q": query,
|
45 |
+
"type": "video",
|
46 |
"maxResults": 50,
|
47 |
"order": sort_by
|
48 |
}
|
|
|
54 |
params["publishedAfter"] = published_after
|
55 |
|
56 |
if video_type and video_type != "All":
|
57 |
+
params["type"] = video_type.lower() # API expects lowercase values
|
58 |
|
59 |
if duration:
|
60 |
duration_mapping = {
|
|
|
76 |
|
77 |
results = response.json().get("items", [])
|
78 |
for result in results:
|
79 |
+
video_info = {
|
80 |
+
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
81 |
+
'video_id': result["id"]["videoId"],
|
82 |
+
'title': result["snippet"]["title"]
|
83 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
all_results.append(video_info)
|
85 |
|
86 |
if 'nextPageToken' not in response.json() or len(all_results) >= max_results:
|
|
|
94 |
print(f"Error during YouTube API request: {e}")
|
95 |
return []
|
96 |
|
97 |
+
def show_video(video_url):
|
98 |
+
video_id = None
|
99 |
+
patterns = [
|
100 |
+
r"youtube\.com/watch\?v=([^\&\?\/]+)",
|
101 |
+
r"youtube\.com/embed/([^\&\?\/]+)",
|
102 |
+
r"youtube\.com/v/([^\&\?\/]+)",
|
103 |
+
r"youtu\.be/([^\&\?\/]+)"
|
104 |
+
]
|
105 |
+
|
106 |
+
for pattern in patterns:
|
107 |
+
match = re.search(pattern, video_url)
|
108 |
+
if match:
|
109 |
+
video_id = match.group(1)
|
110 |
+
break
|
111 |
+
|
112 |
+
if not video_id:
|
113 |
return "Invalid YouTube URL. Please enter a valid YouTube video link."
|
114 |
|
115 |
+
embed_url = f"https://www.youtube.com/embed/{video_id}"
|
116 |
+
|
117 |
html_code = f'''
|
118 |
<iframe width="100%" height="562" src="{embed_url}"
|
119 |
frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
|
|
177 |
search_results = youtube_search(query, upload_date, video_type, duration, sort_by)
|
178 |
gallery_items = []
|
179 |
video_ids = []
|
|
|
180 |
for item in search_results:
|
181 |
image_url = item['thumbnail_url']
|
182 |
title = item['title']
|
183 |
caption = f"{title}"
|
184 |
gallery_items.append((image_url, caption))
|
185 |
+
video_ids.append(item['video_id'])
|
186 |
+
return gallery_items, video_ids
|
|
|
187 |
|
188 |
+
def on_video_select(evt: gr.SelectData, video_ids):
|
189 |
index = evt.index
|
190 |
selected_video_id = video_ids[index]
|
191 |
+
video_url = f"https://www.youtube.com/watch?v={selected_video_id}"
|
192 |
+
return video_url
|
193 |
|
194 |
+
def play_video(video_url):
|
195 |
+
return show_video(video_url)
|
196 |
|
197 |
search_button.click(update_search_results, inputs=[search_query_input, upload_date_input, video_type_input, duration_input, sort_by_input], outputs=[search_output, video_ids_state])
|
198 |
+
search_output.select(on_video_select, inputs=video_ids_state, outputs=selected_video_link)
|
199 |
+
play_video_button.click(play_video, inputs=selected_video_link, outputs=video_output)
|
200 |
|
201 |
demo.launch()
|