Spaces:
openfree
/
Running on CPU Upgrade

seawolf2357 commited on
Commit
6f5dfd4
·
verified ·
1 Parent(s): 68aa430

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -136,12 +136,17 @@ def translate_query(query, country):
136
  def search_serphouse(query, country, page=1, num_result=10):
137
  url = "https://api.serphouse.com/serp/live"
138
 
 
 
 
 
 
139
  payload = {
140
  "data": {
141
- "q": query, # 원본 검색어 사용
142
  "domain": "google.com",
143
  "loc": COUNTRY_LOCATIONS.get(country, "United States"),
144
- "lang": "en", # 기본 영어로 설정
145
  "device": "desktop",
146
  "serp_type": "news",
147
  "page": "1",
@@ -161,15 +166,18 @@ def search_serphouse(query, country, page=1, num_result=10):
161
  print("Response status:", response.status_code)
162
 
163
  response.raise_for_status()
164
- return response.json()
165
  except requests.RequestException as e:
166
- return {"error": f"Error: {str(e)}"}
167
 
168
- def format_results_from_raw(results, translated_query):
169
- if isinstance(results, dict) and "error" in results:
170
- return "Error: " + results["error"], []
171
 
172
  try:
 
 
 
173
  news_results = results.get('results', {}).get('results', {}).get('news', [])
174
  if not news_results:
175
  return "검색 결과가 없습니다.", []
@@ -191,8 +199,8 @@ def format_results_from_raw(results, translated_query):
191
  return f"결과 처리 중 오류 발생: {str(e)}", []
192
 
193
  def serphouse_search(query, country):
194
- results, translated_query = search_serphouse(query, country)
195
- return format_results_from_raw(results, translated_query)
196
 
197
  css = """
198
  footer {visibility: hidden;}
 
136
  def search_serphouse(query, country, page=1, num_result=10):
137
  url = "https://api.serphouse.com/serp/live"
138
 
139
+ # 검색어 번역
140
+ translated_query = translate_query(query, country)
141
+ print(f"Original query: {query}")
142
+ print(f"Translated query: {translated_query}")
143
+
144
  payload = {
145
  "data": {
146
+ "q": query,
147
  "domain": "google.com",
148
  "loc": COUNTRY_LOCATIONS.get(country, "United States"),
149
+ "lang": "en",
150
  "device": "desktop",
151
  "serp_type": "news",
152
  "page": "1",
 
166
  print("Response status:", response.status_code)
167
 
168
  response.raise_for_status()
169
+ return {"results": response.json(), "translated_query": translated_query}
170
  except requests.RequestException as e:
171
+ return {"error": f"Error: {str(e)}", "translated_query": query}
172
 
173
+ def format_results_from_raw(response_data):
174
+ if "error" in response_data:
175
+ return "Error: " + response_data["error"], []
176
 
177
  try:
178
+ results = response_data["results"]
179
+ translated_query = response_data["translated_query"]
180
+
181
  news_results = results.get('results', {}).get('results', {}).get('news', [])
182
  if not news_results:
183
  return "검색 결과가 없습니다.", []
 
199
  return f"결과 처리 중 오류 발생: {str(e)}", []
200
 
201
  def serphouse_search(query, country):
202
+ response_data = search_serphouse(query, country)
203
+ return format_results_from_raw(response_data)
204
 
205
  css = """
206
  footer {visibility: hidden;}