seawolf2357
commited on
Commit
•
dc63a3a
1
Parent(s):
46a86d7
Update app.py
Browse files
app.py
CHANGED
@@ -73,44 +73,50 @@ def format_results(results):
|
|
73 |
debug_info += f"Raw API Response:\n```json\n{json.dumps(results, indent=2)}\n```\n\n"
|
74 |
|
75 |
try:
|
76 |
-
if isinstance(results, dict)
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
-
{
|
90 |
|
91 |
-
**Source:** {
|
92 |
|
93 |
---
|
94 |
|
95 |
"""
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
debug_info += "No 'news' key found in results['results']\n"
|
104 |
-
all_results += "*No news results found in the API response.*\n\n"
|
105 |
-
recent_results += "*No news results found in the API response.*\n\n"
|
106 |
-
else:
|
107 |
-
debug_info += "Invalid API response structure\n"
|
108 |
-
all_results += "*Invalid API response structure.*\n\n"
|
109 |
-
recent_results += "*Invalid API response structure.*\n\n"
|
110 |
except Exception as e:
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
114 |
|
115 |
return all_results, recent_results, debug_info
|
116 |
|
|
|
73 |
debug_info += f"Raw API Response:\n```json\n{json.dumps(results, indent=2)}\n```\n\n"
|
74 |
|
75 |
try:
|
76 |
+
if not isinstance(results, dict):
|
77 |
+
raise ValueError("Results is not a dictionary")
|
78 |
+
|
79 |
+
if "results" not in results:
|
80 |
+
raise ValueError("No 'results' key in the response")
|
81 |
+
|
82 |
+
if "news" not in results["results"]:
|
83 |
+
raise ValueError("No 'news' key in results")
|
84 |
+
|
85 |
+
news_results = results["results"]["news"]
|
86 |
+
debug_info += f"Number of news results: {len(news_results)}\n\n"
|
87 |
+
|
88 |
+
for result in news_results:
|
89 |
+
title = result.get("title", "No Title")
|
90 |
+
url = result.get("url", "#")
|
91 |
+
snippet = result.get("snippet", "No Snippet")
|
92 |
+
channel = result.get("channel", "Unknown")
|
93 |
+
time_str = result.get("time", "Unknown time")
|
94 |
+
|
95 |
+
is_recent = is_recent_news(time_str)
|
96 |
+
debug_info += f"Article: {title}\nTime: {time_str}, Is Recent: {is_recent}\n\n"
|
97 |
+
|
98 |
+
article_info = f"""
|
99 |
+
### [{title}]({url})
|
100 |
|
101 |
+
{snippet}
|
102 |
|
103 |
+
**Source:** {channel} - {time_str}
|
104 |
|
105 |
---
|
106 |
|
107 |
"""
|
108 |
+
all_results += article_info
|
109 |
+
if is_recent:
|
110 |
+
recent_results += article_info
|
111 |
+
|
112 |
+
if recent_results == "## Recent News Results (Within 1 Day)\n\n":
|
113 |
+
recent_results += "*No recent news results found within 1 day.*\n\n"
|
114 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
except Exception as e:
|
116 |
+
error_message = f"Error processing results: {str(e)}"
|
117 |
+
debug_info += error_message + "\n"
|
118 |
+
all_results = error_message + "\n\n"
|
119 |
+
recent_results = error_message + "\n\n"
|
120 |
|
121 |
return all_results, recent_results, debug_info
|
122 |
|