mobenta commited on
Commit
fa938bf
·
verified ·
1 Parent(s): 8feefa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -34
app.py CHANGED
@@ -42,7 +42,6 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
42
  params = {
43
  "part": "snippet",
44
  "q": query,
45
- "type": "video",
46
  "maxResults": 50,
47
  "order": sort_by
48
  }
@@ -54,7 +53,7 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
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,11 +75,31 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
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,26 +113,16 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
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,25 +186,27 @@ with gr.Blocks(css="""
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()
 
42
  params = {
43
  "part": "snippet",
44
  "q": query,
 
45
  "maxResults": 50,
46
  "order": sort_by
47
  }
 
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
 
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"],
82
+ 'id': result["id"]["videoId"],
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
  print(f"Error during YouTube API request: {e}")
114
  return []
115
 
116
+ def show_video(video_id, video_type):
117
+ if video_type == "video":
118
+ embed_url = f"https://www.youtube.com/embed/{video_id}"
119
+ elif video_type == "channel":
120
+ embed_url = f"https://www.youtube.com/channel/{video_id}"
121
+ elif video_type == "playlist":
122
+ embed_url = f"https://www.youtube.com/playlist?list={video_id}"
123
+ else:
 
 
 
 
 
 
 
 
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
  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['id'])
196
+ video_types.append(item['type'])
197
+ return gallery_items, video_ids, video_types
198
 
199
+ def on_video_select(evt: gr.SelectData, video_ids, video_types):
200
  index = evt.index
201
  selected_video_id = video_ids[index]
202
+ selected_video_type = video_types[index]
203
+ return selected_video_id, selected_video_type
204
 
205
+ def play_video(video_id, video_type):
206
+ return show_video(video_id, video_type)
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=[video_ids_state, gr.State()], outputs=[selected_video_link, gr.State()])
210
+ play_video_button.click(play_video, inputs=[selected_video_link, gr.State()], outputs=video_output)
211
 
212
  demo.launch()