Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -46,7 +46,6 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
|
|
46 |
"order": sort_by
|
47 |
}
|
48 |
|
49 |
-
# Add filter parameters based on user selection
|
50 |
if upload_date:
|
51 |
published_after = get_iso8601_date(upload_date)
|
52 |
if published_after:
|
@@ -75,7 +74,6 @@ 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 |
-
# Determine the type of the result and extract the correct ID
|
79 |
if result["id"]["kind"] == "youtube#video":
|
80 |
video_info = {
|
81 |
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
@@ -98,7 +96,7 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
|
|
98 |
'type': "playlist"
|
99 |
}
|
100 |
else:
|
101 |
-
continue
|
102 |
|
103 |
all_results.append(video_info)
|
104 |
|
@@ -162,12 +160,14 @@ with gr.Blocks(css="""
|
|
162 |
""") as demo:
|
163 |
gr.Markdown("## YouTube Video Search, Selection, and Playback")
|
164 |
video_ids_state = gr.State()
|
|
|
165 |
|
166 |
with gr.Row(elem_id="video_container"):
|
167 |
video_output = gr.HTML(label="Video Player", elem_id="video_output")
|
168 |
|
169 |
with gr.Row():
|
170 |
selected_video_link = gr.Textbox(label="Selected Video Link", interactive=False)
|
|
|
171 |
play_video_button = gr.Button("Play Video")
|
172 |
|
173 |
with gr.Row():
|
@@ -176,7 +176,7 @@ with gr.Blocks(css="""
|
|
176 |
placeholder="Enter your search query here",
|
177 |
elem_id="search_query_input")
|
178 |
upload_date_input = gr.Dropdown(label="Upload Date", choices=["", "Last Hour", "Today", "This Week", "This Month", "This Year"])
|
179 |
-
video_type_input = gr.Dropdown(label="Type", choices=["All", "Video", "Channel", "Playlist"
|
180 |
duration_input = gr.Dropdown(label="Duration", choices=["", "Short (<4 mins)", "Medium (4-20 mins)", "Long (>20 mins)"])
|
181 |
sort_by_input = gr.Dropdown(label="Sort By", choices=["relevance", "date", "viewCount", "rating"], value="relevance")
|
182 |
search_button = gr.Button("Search")
|
@@ -200,13 +200,26 @@ with gr.Blocks(css="""
|
|
200 |
index = evt.index
|
201 |
selected_video_id = video_ids[index]
|
202 |
selected_video_type = video_types[index]
|
203 |
-
|
|
|
204 |
|
205 |
def play_video(video_id, video_type):
|
206 |
return show_video(video_id, video_type)
|
207 |
|
208 |
-
search_button.click(
|
209 |
-
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
demo.launch()
|
|
|
46 |
"order": sort_by
|
47 |
}
|
48 |
|
|
|
49 |
if upload_date:
|
50 |
published_after = get_iso8601_date(upload_date)
|
51 |
if published_after:
|
|
|
74 |
|
75 |
results = response.json().get("items", [])
|
76 |
for result in results:
|
|
|
77 |
if result["id"]["kind"] == "youtube#video":
|
78 |
video_info = {
|
79 |
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
|
|
96 |
'type': "playlist"
|
97 |
}
|
98 |
else:
|
99 |
+
continue
|
100 |
|
101 |
all_results.append(video_info)
|
102 |
|
|
|
160 |
""") as demo:
|
161 |
gr.Markdown("## YouTube Video Search, Selection, and Playback")
|
162 |
video_ids_state = gr.State()
|
163 |
+
video_types_state = gr.State()
|
164 |
|
165 |
with gr.Row(elem_id="video_container"):
|
166 |
video_output = gr.HTML(label="Video Player", elem_id="video_output")
|
167 |
|
168 |
with gr.Row():
|
169 |
selected_video_link = gr.Textbox(label="Selected Video Link", interactive=False)
|
170 |
+
selected_video_type = gr.Textbox(label="Selected Video Type", visible=False, interactive=False)
|
171 |
play_video_button = gr.Button("Play Video")
|
172 |
|
173 |
with gr.Row():
|
|
|
176 |
placeholder="Enter your search query here",
|
177 |
elem_id="search_query_input")
|
178 |
upload_date_input = gr.Dropdown(label="Upload Date", choices=["", "Last Hour", "Today", "This Week", "This Month", "This Year"])
|
179 |
+
video_type_input = gr.Dropdown(label="Type", choices=["All", "Video", "Channel", "Playlist"])
|
180 |
duration_input = gr.Dropdown(label="Duration", choices=["", "Short (<4 mins)", "Medium (4-20 mins)", "Long (>20 mins)"])
|
181 |
sort_by_input = gr.Dropdown(label="Sort By", choices=["relevance", "date", "viewCount", "rating"], value="relevance")
|
182 |
search_button = gr.Button("Search")
|
|
|
200 |
index = evt.index
|
201 |
selected_video_id = video_ids[index]
|
202 |
selected_video_type = video_types[index]
|
203 |
+
video_url = f"https://www.youtube.com/watch?v={selected_video_id}" if selected_video_type == "video" else ""
|
204 |
+
return video_url, selected_video_type
|
205 |
|
206 |
def play_video(video_id, video_type):
|
207 |
return show_video(video_id, video_type)
|
208 |
|
209 |
+
search_button.click(
|
210 |
+
update_search_results,
|
211 |
+
inputs=[search_query_input, upload_date_input, video_type_input, duration_input, sort_by_input],
|
212 |
+
outputs=[search_output, video_ids_state, video_types_state]
|
213 |
+
)
|
214 |
+
search_output.select(
|
215 |
+
on_video_select,
|
216 |
+
inputs=[video_ids_state, video_types_state],
|
217 |
+
outputs=[selected_video_link, selected_video_type]
|
218 |
+
)
|
219 |
+
play_video_button.click(
|
220 |
+
play_video,
|
221 |
+
inputs=[selected_video_link, selected_video_type],
|
222 |
+
outputs=video_output
|
223 |
+
)
|
224 |
|
225 |
demo.launch()
|