Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -56,7 +56,7 @@ def is_within_24_hours(time_str):
|
|
56 |
unit = time_parts[1].lower()
|
57 |
if unit in ['minute', 'minutes', 'hour', 'hours']:
|
58 |
return True
|
59 |
-
elif unit
|
60 |
return True
|
61 |
except ValueError:
|
62 |
pass
|
@@ -66,42 +66,34 @@ def format_results(results):
|
|
66 |
html_output = """
|
67 |
<style>
|
68 |
.news-container { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; }
|
69 |
-
.news-item { border: 1px solid #ddd; padding:
|
70 |
-
.news-title { font-size:
|
71 |
-
.news-
|
72 |
-
.news-
|
73 |
-
.
|
|
|
74 |
</style>
|
75 |
<div class="news-container">
|
76 |
-
<h2>Search Results (Last 24 Hours)</h2>
|
77 |
"""
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
if
|
84 |
-
|
85 |
-
if not news_results:
|
86 |
-
html_output += "<p>No news results found in the API response.</p>"
|
87 |
-
else:
|
88 |
-
filtered_results = [result for result in news_results if is_within_24_hours(result.get("time", "").strip())]
|
89 |
-
|
90 |
-
if not filtered_results:
|
91 |
-
html_output += "<p>No news results found within the last 24 hours.</p>"
|
92 |
-
else:
|
93 |
-
for result in filtered_results:
|
94 |
-
html_output += f"""
|
95 |
-
<div class="news-item">
|
96 |
-
<a href="{result.get('url', '#')}" class="news-title">{result.get('title', 'No Title')}</a>
|
97 |
-
<p class="news-snippet">{result.get('snippet', 'No Snippet')}</p>
|
98 |
-
<p class="news-meta">Source: {result.get('channel', 'Unknown')} - {result.get('time', 'Unknown time')}</p>
|
99 |
-
</div>
|
100 |
-
"""
|
101 |
else:
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
html_output += "</div>"
|
107 |
return html_output
|
@@ -112,7 +104,14 @@ def serphouse_search(query, country, verbatim, page, num_result):
|
|
112 |
results = search_serphouse(query, country, verbatim, page, num_result)
|
113 |
return format_results(results)
|
114 |
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
fn=serphouse_search,
|
117 |
inputs=[
|
118 |
gr.Textbox(label="Search Query"),
|
|
|
56 |
unit = time_parts[1].lower()
|
57 |
if unit in ['minute', 'minutes', 'hour', 'hours']:
|
58 |
return True
|
59 |
+
elif unit == 'day' and value == 1:
|
60 |
return True
|
61 |
except ValueError:
|
62 |
pass
|
|
|
66 |
html_output = """
|
67 |
<style>
|
68 |
.news-container { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; }
|
69 |
+
.news-item { background-color: #f9f9f9; border: 1px solid #ddd; padding: 20px; margin-bottom: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
|
70 |
+
.news-title { font-size: 20px; color: #1a0dab; text-decoration: none; font-weight: bold; }
|
71 |
+
.news-title:hover { text-decoration: underline; }
|
72 |
+
.news-snippet { color: #545454; margin: 15px 0; line-height: 1.4; }
|
73 |
+
.news-meta { font-size: 14px; color: #006621; }
|
74 |
+
.no-results { text-align: center; color: #666; font-style: italic; }
|
75 |
</style>
|
76 |
<div class="news-container">
|
77 |
+
<h2 style="text-align: center; color: #333;">Search Results (Last 24 Hours)</h2>
|
78 |
"""
|
79 |
|
80 |
+
if isinstance(results, dict) and "results" in results:
|
81 |
+
news_results = results["results"].get("news", [])
|
82 |
+
filtered_results = [result for result in news_results if is_within_24_hours(result.get("time", "").strip())]
|
83 |
+
|
84 |
+
if not filtered_results:
|
85 |
+
html_output += '<p class="no-results">No news results found within the last 24 hours.</p>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
else:
|
87 |
+
for result in filtered_results:
|
88 |
+
html_output += f"""
|
89 |
+
<div class="news-item">
|
90 |
+
<a href="{result.get('url', '#')}" class="news-title" target="_blank">{result.get('title', 'No Title')}</a>
|
91 |
+
<p class="news-snippet">{result.get('snippet', 'No Snippet')}</p>
|
92 |
+
<p class="news-meta">Source: {result.get('channel', 'Unknown')} - {result.get('time', 'Unknown time')}</p>
|
93 |
+
</div>
|
94 |
+
"""
|
95 |
+
else:
|
96 |
+
html_output += '<p class="no-results">No results found or unexpected response format.</p>'
|
97 |
|
98 |
html_output += "</div>"
|
99 |
return html_output
|
|
|
104 |
results = search_serphouse(query, country, verbatim, page, num_result)
|
105 |
return format_results(results)
|
106 |
|
107 |
+
css = """
|
108 |
+
footer {
|
109 |
+
visibility: hidden;
|
110 |
+
}
|
111 |
+
"""
|
112 |
+
|
113 |
+
|
114 |
+
iface = gr.Interface(theme="Nymbo/Nymbo_Theme", css=css,
|
115 |
fn=serphouse_search,
|
116 |
inputs=[
|
117 |
gr.Textbox(label="Search Query"),
|