Update app.py
Browse files
app.py
CHANGED
@@ -52,24 +52,38 @@ def format_results(results):
|
|
52 |
now = datetime.now()
|
53 |
html_output = "<h2>Search Results</h2>"
|
54 |
|
|
|
|
|
|
|
55 |
if isinstance(results, dict) and "results" in results:
|
56 |
news_results = results["results"].get("news", [])
|
|
|
|
|
57 |
for result in news_results:
|
58 |
time_str = result.get("time", "").strip()
|
59 |
if time_str.endswith("ago"):
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
else:
|
72 |
-
html_output += "<p>
|
73 |
|
74 |
return html_output
|
75 |
|
@@ -79,13 +93,7 @@ def serphouse_search(query, country, verbatim, page, num_result):
|
|
79 |
results = search_serphouse(query, country, verbatim, page, num_result)
|
80 |
return format_results(results)
|
81 |
|
82 |
-
|
83 |
-
footer {
|
84 |
-
visibility: hidden;
|
85 |
-
}
|
86 |
-
"""
|
87 |
-
|
88 |
-
iface = gr.Interface(theme="Nymbo/Nymbo_Theme", css=css,
|
89 |
fn=serphouse_search,
|
90 |
inputs=[
|
91 |
gr.Textbox(label="Search Query"),
|
@@ -95,7 +103,8 @@ iface = gr.Interface(theme="Nymbo/Nymbo_Theme", css=css,
|
|
95 |
gr.Slider(1, 100, 10, label="Number of Results")
|
96 |
],
|
97 |
outputs="html",
|
98 |
-
title="
|
|
|
99 |
)
|
100 |
|
101 |
iface.launch()
|
|
|
52 |
now = datetime.now()
|
53 |
html_output = "<h2>Search Results</h2>"
|
54 |
|
55 |
+
# 디버깅: API 응답 전체를 출력
|
56 |
+
html_output += f"<pre>{json.dumps(results, indent=2)}</pre>"
|
57 |
+
|
58 |
if isinstance(results, dict) and "results" in results:
|
59 |
news_results = results["results"].get("news", [])
|
60 |
+
if not news_results:
|
61 |
+
html_output += "<p>No news results found in the API response.</p>"
|
62 |
for result in news_results:
|
63 |
time_str = result.get("time", "").strip()
|
64 |
if time_str.endswith("ago"):
|
65 |
+
time_parts = time_str.split()
|
66 |
+
if len(time_parts) >= 2:
|
67 |
+
try:
|
68 |
+
time_value = int(time_parts[0])
|
69 |
+
time_unit = time_parts[1]
|
70 |
+
|
71 |
+
if time_unit in ["minute", "minutes", "hour", "hours"] and time_value <= 24:
|
72 |
+
html_output += f"""
|
73 |
+
<div style='border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;'>
|
74 |
+
<h3><a href='{result.get('url', '#')}'>{result.get('title', 'No Title')}</a></h3>
|
75 |
+
<p>{result.get('snippet', 'No Snippet')}</p>
|
76 |
+
<p><small>Source: {result.get('channel', 'Unknown')} - {result.get('time', 'Unknown time')}</small></p>
|
77 |
+
</div>
|
78 |
+
"""
|
79 |
+
except ValueError:
|
80 |
+
html_output += f"<p>Error parsing time: {time_str}</p>"
|
81 |
+
else:
|
82 |
+
html_output += f"<p>Invalid time format: {time_str}</p>"
|
83 |
+
else:
|
84 |
+
html_output += f"<p>Unexpected time format: {time_str}</p>"
|
85 |
else:
|
86 |
+
html_output += "<p>Unexpected response format or no results found.</p>"
|
87 |
|
88 |
return html_output
|
89 |
|
|
|
93 |
results = search_serphouse(query, country, verbatim, page, num_result)
|
94 |
return format_results(results)
|
95 |
|
96 |
+
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
fn=serphouse_search,
|
98 |
inputs=[
|
99 |
gr.Textbox(label="Search Query"),
|
|
|
103 |
gr.Slider(1, 100, 10, label="Number of Results")
|
104 |
],
|
105 |
outputs="html",
|
106 |
+
title="SERPHouse News Search Interface",
|
107 |
+
description="Enter your search query and select a country to get news results from the SERPHouse API. Only articles from the last 24 hours will be displayed."
|
108 |
)
|
109 |
|
110 |
iface.launch()
|