Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import os
|
4 |
import re
|
|
|
5 |
|
6 |
# Fetch the keys from the environment variable and convert them into a list
|
7 |
YOUTUBE_API_KEYS = os.getenv("YOUTUBE_API_KEYS")
|
@@ -20,6 +21,21 @@ def get_api_key():
|
|
20 |
key_index = (key_index + 1) % len(YOUTUBE_API_KEYS) # Rotate to the next key
|
21 |
return api_key
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def youtube_search(query, upload_date, video_type, duration, sort_by, max_results=50):
|
24 |
search_url = "https://www.googleapis.com/youtube/v3/search"
|
25 |
all_results = []
|
@@ -33,11 +49,20 @@ def youtube_search(query, upload_date, video_type, duration, sort_by, max_result
|
|
33 |
|
34 |
# Add filter parameters based on user selection
|
35 |
if upload_date:
|
36 |
-
|
|
|
|
|
|
|
37 |
if video_type and video_type != "All":
|
38 |
params["type"] = video_type.lower() # API expects lowercase values
|
|
|
39 |
if duration:
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
try:
|
43 |
while len(all_results) < max_results:
|
@@ -149,26 +174,7 @@ with gr.Blocks(css="""
|
|
149 |
search_output = gr.Gallery(label="Search Results", columns=1, height="800px", elem_id="search_output")
|
150 |
|
151 |
def update_search_results(query, upload_date, video_type, duration, sort_by):
|
152 |
-
|
153 |
-
upload_date_mapping = {
|
154 |
-
"Last Hour": "now-1h",
|
155 |
-
"Today": "now-1d",
|
156 |
-
"This Week": "now-7d",
|
157 |
-
"This Month": "now-30d",
|
158 |
-
"This Year": "now-365d"
|
159 |
-
}
|
160 |
-
|
161 |
-
duration_mapping = {
|
162 |
-
"Short (<4 mins)": "short",
|
163 |
-
"Medium (4-20 mins)": "medium",
|
164 |
-
"Long (>20 mins)": "long"
|
165 |
-
}
|
166 |
-
|
167 |
-
# Convert user-friendly values to API-compatible parameters
|
168 |
-
upload_date_param = upload_date_mapping.get(upload_date, "")
|
169 |
-
duration_param = duration_mapping.get(duration, "")
|
170 |
-
|
171 |
-
search_results = youtube_search(query, upload_date_param, video_type, duration_param, sort_by)
|
172 |
gallery_items = []
|
173 |
video_ids = []
|
174 |
for item in search_results:
|
|
|
2 |
import requests
|
3 |
import os
|
4 |
import re
|
5 |
+
from datetime import datetime, timedelta
|
6 |
|
7 |
# Fetch the keys from the environment variable and convert them into a list
|
8 |
YOUTUBE_API_KEYS = os.getenv("YOUTUBE_API_KEYS")
|
|
|
21 |
key_index = (key_index + 1) % len(YOUTUBE_API_KEYS) # Rotate to the next key
|
22 |
return api_key
|
23 |
|
24 |
+
def get_iso8601_date(time_frame):
|
25 |
+
now = datetime.utcnow()
|
26 |
+
if time_frame == "Last Hour":
|
27 |
+
return (now - timedelta(hours=1)).isoformat("T") + "Z"
|
28 |
+
elif time_frame == "Today":
|
29 |
+
return (now - timedelta(days=1)).isoformat("T") + "Z"
|
30 |
+
elif time_frame == "This Week":
|
31 |
+
return (now - timedelta(weeks=1)).isoformat("T") + "Z"
|
32 |
+
elif time_frame == "This Month":
|
33 |
+
return (now - timedelta(days=30)).isoformat("T") + "Z"
|
34 |
+
elif time_frame == "This Year":
|
35 |
+
return (now - timedelta(days=365)).isoformat("T") + "Z"
|
36 |
+
else:
|
37 |
+
return ""
|
38 |
+
|
39 |
def youtube_search(query, upload_date, video_type, duration, sort_by, max_results=50):
|
40 |
search_url = "https://www.googleapis.com/youtube/v3/search"
|
41 |
all_results = []
|
|
|
49 |
|
50 |
# Add filter parameters based on user selection
|
51 |
if upload_date:
|
52 |
+
published_after = get_iso8601_date(upload_date)
|
53 |
+
if published_after:
|
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 = {
|
61 |
+
"Short (<4 mins)": "short",
|
62 |
+
"Medium (4-20 mins)": "medium",
|
63 |
+
"Long (>20 mins)": "long"
|
64 |
+
}
|
65 |
+
params["videoDuration"] = duration_mapping.get(duration, "any")
|
66 |
|
67 |
try:
|
68 |
while len(all_results) < max_results:
|
|
|
174 |
search_output = gr.Gallery(label="Search Results", columns=1, height="800px", elem_id="search_output")
|
175 |
|
176 |
def update_search_results(query, upload_date, video_type, duration, sort_by):
|
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:
|