Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,7 +43,7 @@ def youtube_search(query, upload_date, video_type, duration, max_results=50):
|
|
43 |
"part": "snippet",
|
44 |
"q": query,
|
45 |
"maxResults": 50,
|
46 |
-
"order": "relevance"
|
47 |
}
|
48 |
|
49 |
if upload_date:
|
@@ -63,47 +63,41 @@ def youtube_search(query, upload_date, video_type, duration, max_results=50):
|
|
63 |
params["videoDuration"] = duration_mapping.get(duration, "any")
|
64 |
|
65 |
try:
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
continue
|
73 |
-
|
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"],
|
80 |
-
'id': result["id"]["videoId"],
|
81 |
-
'title': result["snippet"]["title"],
|
82 |
-
'type': "video"
|
83 |
-
}
|
84 |
-
elif result["id"]["kind"] == "youtube#channel":
|
85 |
-
video_info = {
|
86 |
-
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
87 |
-
'id': result["id"]["channelId"],
|
88 |
-
'title': result["snippet"]["title"],
|
89 |
-
'type': "channel"
|
90 |
-
}
|
91 |
-
elif result["id"]["kind"] == "youtube#playlist":
|
92 |
-
video_info = {
|
93 |
-
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
94 |
-
'id': result["id"]["playlistId"],
|
95 |
-
'title': result["snippet"]["title"],
|
96 |
-
'type': "playlist"
|
97 |
-
}
|
98 |
-
else:
|
99 |
-
continue
|
100 |
-
|
101 |
-
all_results.append(video_info)
|
102 |
-
|
103 |
-
if 'nextPageToken' not in response.json() or len(all_results) >= max_results:
|
104 |
-
break
|
105 |
-
|
106 |
-
params['pageToken'] = response.json()['nextPageToken']
|
107 |
|
108 |
return all_results
|
109 |
|
@@ -111,6 +105,33 @@ def youtube_search(query, upload_date, video_type, duration, max_results=50):
|
|
111 |
print(f"Error during YouTube API request: {e}")
|
112 |
return []
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
def show_video(video_id, video_type):
|
115 |
if video_type == "video":
|
116 |
embed_url = f"https://www.youtube.com/embed/{video_id}"
|
@@ -180,6 +201,7 @@ with gr.Blocks(css="""
|
|
180 |
duration_input = gr.Dropdown(label="Duration", choices=["", "Short (<4 mins)", "Medium (4-20 mins)", "Long (>20 mins)"])
|
181 |
search_button = gr.Button("Search")
|
182 |
search_output = gr.Gallery(label="Search Results", columns=1, height="800px", elem_id="search_output")
|
|
|
183 |
|
184 |
def update_search_results(query, upload_date, video_type, duration):
|
185 |
search_results = youtube_search(query, upload_date, video_type, duration)
|
@@ -199,7 +221,13 @@ with gr.Blocks(css="""
|
|
199 |
index = evt.index
|
200 |
selected_video_id = video_ids[index]
|
201 |
selected_video_type = video_types[index]
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
def play_video(video_id, video_type):
|
205 |
return show_video(video_id.split("=")[-1], video_type)
|
@@ -212,7 +240,7 @@ with gr.Blocks(css="""
|
|
212 |
search_output.select(
|
213 |
on_video_select,
|
214 |
inputs=[video_ids_state, video_types_state],
|
215 |
-
outputs=[
|
216 |
)
|
217 |
play_video_button.click(
|
218 |
play_video,
|
|
|
43 |
"part": "snippet",
|
44 |
"q": query,
|
45 |
"maxResults": 50,
|
46 |
+
"order": "relevance"
|
47 |
}
|
48 |
|
49 |
if upload_date:
|
|
|
63 |
params["videoDuration"] = duration_mapping.get(duration, "any")
|
64 |
|
65 |
try:
|
66 |
+
params["key"] = get_api_key()
|
67 |
+
response = requests.get(search_url, params=params)
|
68 |
+
|
69 |
+
if response.status_code in [403, 429]:
|
70 |
+
print(f"Quota exceeded or forbidden for API key. Trying next key...")
|
71 |
+
return []
|
72 |
+
response.raise_for_status()
|
73 |
+
|
74 |
+
results = response.json().get("items", [])
|
75 |
+
for result in results:
|
76 |
+
if result["id"]["kind"] == "youtube#video":
|
77 |
+
video_info = {
|
78 |
+
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
79 |
+
'id': result["id"]["videoId"],
|
80 |
+
'title': result["snippet"]["title"],
|
81 |
+
'type': "video"
|
82 |
+
}
|
83 |
+
elif result["id"]["kind"] == "youtube#channel":
|
84 |
+
video_info = {
|
85 |
+
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
86 |
+
'id': result["id"]["channelId"],
|
87 |
+
'title': result["snippet"]["title"],
|
88 |
+
'type': "channel"
|
89 |
+
}
|
90 |
+
elif result["id"]["kind"] == "youtube#playlist":
|
91 |
+
video_info = {
|
92 |
+
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
93 |
+
'id': result["id"]["playlistId"],
|
94 |
+
'title': result["snippet"]["title"],
|
95 |
+
'type': "playlist"
|
96 |
+
}
|
97 |
+
else:
|
98 |
continue
|
99 |
+
|
100 |
+
all_results.append(video_info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
return all_results
|
103 |
|
|
|
105 |
print(f"Error during YouTube API request: {e}")
|
106 |
return []
|
107 |
|
108 |
+
def get_channel_videos(channel_id, max_results=10):
|
109 |
+
search_url = "https://www.googleapis.com/youtube/v3/search"
|
110 |
+
params = {
|
111 |
+
"part": "snippet",
|
112 |
+
"channelId": channel_id,
|
113 |
+
"maxResults": max_results,
|
114 |
+
"order": "date",
|
115 |
+
"type": "video",
|
116 |
+
"key": get_api_key()
|
117 |
+
}
|
118 |
+
try:
|
119 |
+
response = requests.get(search_url, params=params)
|
120 |
+
response.raise_for_status()
|
121 |
+
results = response.json().get("items", [])
|
122 |
+
channel_videos = []
|
123 |
+
for result in results:
|
124 |
+
channel_videos.append({
|
125 |
+
'thumbnail_url': result["snippet"]["thumbnails"]["high"]["url"],
|
126 |
+
'id': result["id"]["videoId"],
|
127 |
+
'title': result["snippet"]["title"],
|
128 |
+
'type': "video"
|
129 |
+
})
|
130 |
+
return channel_videos
|
131 |
+
except requests.exceptions.RequestException as e:
|
132 |
+
print(f"Error during YouTube API request: {e}")
|
133 |
+
return []
|
134 |
+
|
135 |
def show_video(video_id, video_type):
|
136 |
if video_type == "video":
|
137 |
embed_url = f"https://www.youtube.com/embed/{video_id}"
|
|
|
201 |
duration_input = gr.Dropdown(label="Duration", choices=["", "Short (<4 mins)", "Medium (4-20 mins)", "Long (>20 mins)"])
|
202 |
search_button = gr.Button("Search")
|
203 |
search_output = gr.Gallery(label="Search Results", columns=1, height="800px", elem_id="search_output")
|
204 |
+
channel_videos_output = gr.Gallery(label="Channel Videos", columns=1, height="800px", visible=False)
|
205 |
|
206 |
def update_search_results(query, upload_date, video_type, duration):
|
207 |
search_results = youtube_search(query, upload_date, video_type, duration)
|
|
|
221 |
index = evt.index
|
222 |
selected_video_id = video_ids[index]
|
223 |
selected_video_type = video_types[index]
|
224 |
+
if selected_video_type == "channel":
|
225 |
+
# Fetch videos from the channel
|
226 |
+
channel_videos = get_channel_videos(selected_video_id)
|
227 |
+
gallery_items = [(video['thumbnail_url'], video['title']) for video in channel_videos]
|
228 |
+
return gallery_items, gr.update(visible=True)
|
229 |
+
else:
|
230 |
+
return f"https://www.youtube.com/watch?v={selected_video_id}", selected_video_type
|
231 |
|
232 |
def play_video(video_id, video_type):
|
233 |
return show_video(video_id.split("=")[-1], video_type)
|
|
|
240 |
search_output.select(
|
241 |
on_video_select,
|
242 |
inputs=[video_ids_state, video_types_state],
|
243 |
+
outputs=[channel_videos_output, channel_videos_output]
|
244 |
)
|
245 |
play_video_button.click(
|
246 |
play_video,
|