Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
seawolf2357
commited on
Update app.py
Browse files
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(
|
169 |
-
if
|
170 |
-
return "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 |
-
|
195 |
-
return format_results_from_raw(
|
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;}
|