seawolf2357 commited on
Commit
ba53ac8
·
verified ·
1 Parent(s): 2001e6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
app.py CHANGED
@@ -41,12 +41,12 @@ def search_serphouse(query, country, verbatim, page, num_result):
41
  "authorization": f"Bearer {API_KEY}"
42
  }
43
 
44
- response = requests.post(url, json=payload, headers=headers)
45
-
46
- if response.status_code == 200:
47
  return response.json()
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()
@@ -72,28 +72,35 @@ def format_results(results):
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
@@ -110,8 +117,7 @@ footer {
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"),
@@ -122,7 +128,9 @@ iface = gr.Interface(theme="Nymbo/Nymbo_Theme", css=css,
122
  ],
123
  outputs="html",
124
  title="SERPHouse News Search Interface",
125
- 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."
 
 
126
  )
127
 
128
  iface.launch()
 
41
  "authorization": f"Bearer {API_KEY}"
42
  }
43
 
44
+ try:
45
+ response = requests.post(url, json=payload, headers=headers)
46
+ response.raise_for_status()
47
  return response.json()
48
+ except requests.RequestException as e:
49
+ return f"Error: {str(e)}"
50
 
51
  def is_within_24_hours(time_str):
52
  time_parts = time_str.split()
 
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
+ .debug-info { background-color: #f0f0f0; padding: 10px; margin-top: 20px; border-radius: 5px; white-space: pre-wrap; }
76
  </style>
77
  <div class="news-container">
78
  <h2 style="text-align: center; color: #333;">Search Results (Last 24 Hours)</h2>
79
  """
80
 
81
+ # 디버깅 정보 추가
82
+ html_output += f'<div class="debug-info"><h3>Debug Info:</h3><pre>{json.dumps(results, indent=2)}</pre></div>'
83
+
84
+ try:
85
+ if isinstance(results, dict) and "results" in results:
86
+ news_results = results["results"].get("news", [])
87
+ filtered_results = [result for result in news_results if is_within_24_hours(result.get("time", "").strip())]
88
+
89
+ if not filtered_results:
90
+ html_output += '<p class="no-results">No news results found within the last 24 hours.</p>'
91
+ else:
92
+ for result in filtered_results:
93
+ html_output += f"""
94
+ <div class="news-item">
95
+ <a href="{result.get('url', '#')}" class="news-title" target="_blank">{result.get('title', 'No Title')}</a>
96
+ <p class="news-snippet">{result.get('snippet', 'No Snippet')}</p>
97
+ <p class="news-meta">Source: {result.get('channel', 'Unknown')} - {result.get('time', 'Unknown time')}</p>
98
+ </div>
99
+ """
100
  else:
101
+ html_output += f'<p class="no-results">Unexpected response format: {type(results)}</p>'
102
+ except Exception as e:
103
+ html_output += f'<p class="no-results">Error processing results: {str(e)}</p>'
 
 
 
 
 
 
 
104
 
105
  html_output += "</div>"
106
  return html_output
 
117
  }
118
  """
119
 
120
+ iface = gr.Interface(
 
121
  fn=serphouse_search,
122
  inputs=[
123
  gr.Textbox(label="Search Query"),
 
128
  ],
129
  outputs="html",
130
  title="SERPHouse News Search Interface",
131
+ 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.",
132
+ theme="Nymbo/Nymbo_Theme",
133
+ css=css
134
  )
135
 
136
  iface.launch()