seawolf2357 commited on
Commit
482bc99
·
verified ·
1 Parent(s): a4f4206

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -30
app.py CHANGED
@@ -48,43 +48,52 @@ def search_serphouse(query, country, verbatim, page, num_result):
48
  else:
49
  return f"Error: {response.status_code} - {response.text}"
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def format_results(results):
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
 
90
  def serphouse_search(query, country, verbatim, page, num_result):
 
48
  else:
49
  return f"Error: {response.status_code} - {response.text}"
50
 
51
+ def is_within_24_hours(time_str):
52
+ time_parts = time_str.split()
53
+ if len(time_parts) >= 2:
54
+ try:
55
+ value = int(time_parts[0])
56
+ unit = time_parts[1].lower()
57
+ if unit in ['minute', 'minutes', 'hour', 'hours']:
58
+ return True
59
+ elif unit in ['day', 'days'] and value == 1:
60
+ return True
61
+ except ValueError:
62
+ pass
63
+ return False
64
+
65
  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: 15px; margin-bottom: 20px; border-radius: 5px; }
70
+ .news-title { font-size: 18px; color: #1a0dab; text-decoration: none; }
71
+ .news-snippet { color: #545454; margin: 10px 0; }
72
+ .news-meta { font-size: 12px; color: #006621; }
73
+ </style>
74
+ <div class="news-container">
75
+ <h2>Search Results (Last 24 Hours)</h2>
76
+ """
77
 
78
  if isinstance(results, dict) and "results" in results:
79
  news_results = results["results"].get("news", [])
80
+ filtered_results = [result for result in news_results if is_within_24_hours(result.get("time", "").strip())]
81
+
82
+ if not filtered_results:
83
+ html_output += "<p>No news results found within the last 24 hours.</p>"
84
+ else:
85
+ for result in filtered_results:
86
+ html_output += f"""
87
+ <div class="news-item">
88
+ <a href="{result.get('url', '#')}" class="news-title">{result.get('title', 'No Title')}</a>
89
+ <p class="news-snippet">{result.get('snippet', 'No Snippet')}</p>
90
+ <p class="news-meta">Source: {result.get('channel', 'Unknown')} - {result.get('time', 'Unknown time')}</p>
91
+ </div>
92
+ """
 
 
 
 
 
 
 
 
 
 
 
 
93
  else:
94
  html_output += "<p>Unexpected response format or no results found.</p>"
95
 
96
+ html_output += "</div>"
97
  return html_output
98
 
99
  def serphouse_search(query, country, verbatim, page, num_result):