Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import requests
|
|
3 |
import json
|
4 |
from datetime import datetime, timedelta
|
5 |
|
6 |
-
API_KEY = "
|
7 |
|
8 |
MAJOR_COUNTRIES = [
|
9 |
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
@@ -20,11 +20,11 @@ MAJOR_COUNTRIES = [
|
|
20 |
|
21 |
def search_serphouse(query, country, page, num_result):
|
22 |
url = "https://api.serphouse.com/serp/live"
|
23 |
-
|
24 |
now = datetime.utcnow()
|
25 |
yesterday = now - timedelta(days=1)
|
26 |
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
|
27 |
-
|
28 |
payload = {
|
29 |
"data": {
|
30 |
"q": query,
|
@@ -39,13 +39,13 @@ def search_serphouse(query, country, page, num_result):
|
|
39 |
"date_range": date_range
|
40 |
}
|
41 |
}
|
42 |
-
|
43 |
headers = {
|
44 |
"accept": "application/json",
|
45 |
"content-type": "application/json",
|
46 |
"authorization": f"Bearer {API_KEY}"
|
47 |
}
|
48 |
-
|
49 |
try:
|
50 |
response = requests.post(url, json=payload, headers=headers)
|
51 |
response.raise_for_status()
|
@@ -58,9 +58,8 @@ def search_serphouse(query, country, page, num_result):
|
|
58 |
|
59 |
def format_results_from_raw(results):
|
60 |
try:
|
61 |
-
# ๋๋ฒ๊ทธ ์ ๋ณด ์ถ๋ ฅ
|
62 |
debug_info = f"Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}\n\n"
|
63 |
-
|
64 |
if isinstance(results, dict) and "error" in results:
|
65 |
return "Error: " + results["error"], debug_info
|
66 |
|
@@ -70,9 +69,9 @@ def format_results_from_raw(results):
|
|
70 |
# 'results' ํค ๋ด๋ถ์ ๊ตฌ์กฐ ํ์ธ
|
71 |
if 'results' in results:
|
72 |
results_content = results['results']
|
73 |
-
# '
|
74 |
-
if '
|
75 |
-
news_results = results_content['
|
76 |
else:
|
77 |
news_results = []
|
78 |
else:
|
@@ -86,12 +85,12 @@ def format_results_from_raw(results):
|
|
86 |
title = result.get("title", "์ ๋ชฉ ์์")
|
87 |
link = result.get("url", result.get("link", "#"))
|
88 |
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
89 |
-
|
90 |
-
|
91 |
-
image_url = result.get("
|
92 |
|
93 |
-
#
|
94 |
-
if image_url:
|
95 |
image_html = f'<img src="{image_url}" alt="Image" style="max-width: 100%; height: auto;">'
|
96 |
else:
|
97 |
image_html = ''
|
@@ -99,7 +98,7 @@ def format_results_from_raw(results):
|
|
99 |
article_html = f"""
|
100 |
<div style="margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-bottom: 20px;">
|
101 |
<h3><a href="{link}" target="_blank">{title}</a></h3>
|
102 |
-
<p><strong>{
|
103 |
{image_html}
|
104 |
<p>{snippet}</p>
|
105 |
</div>
|
|
|
3 |
import json
|
4 |
from datetime import datetime, timedelta
|
5 |
|
6 |
+
API_KEY = "YOUR_API_KEY"
|
7 |
|
8 |
MAJOR_COUNTRIES = [
|
9 |
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
|
|
20 |
|
21 |
def search_serphouse(query, country, page, num_result):
|
22 |
url = "https://api.serphouse.com/serp/live"
|
23 |
+
|
24 |
now = datetime.utcnow()
|
25 |
yesterday = now - timedelta(days=1)
|
26 |
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
|
27 |
+
|
28 |
payload = {
|
29 |
"data": {
|
30 |
"q": query,
|
|
|
39 |
"date_range": date_range
|
40 |
}
|
41 |
}
|
42 |
+
|
43 |
headers = {
|
44 |
"accept": "application/json",
|
45 |
"content-type": "application/json",
|
46 |
"authorization": f"Bearer {API_KEY}"
|
47 |
}
|
48 |
+
|
49 |
try:
|
50 |
response = requests.post(url, json=payload, headers=headers)
|
51 |
response.raise_for_status()
|
|
|
58 |
|
59 |
def format_results_from_raw(results):
|
60 |
try:
|
|
|
61 |
debug_info = f"Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}\n\n"
|
62 |
+
|
63 |
if isinstance(results, dict) and "error" in results:
|
64 |
return "Error: " + results["error"], debug_info
|
65 |
|
|
|
69 |
# 'results' ํค ๋ด๋ถ์ ๊ตฌ์กฐ ํ์ธ
|
70 |
if 'results' in results:
|
71 |
results_content = results['results']
|
72 |
+
# 'news' ํค ํ์ธ
|
73 |
+
if 'news' in results_content:
|
74 |
+
news_results = results_content['news']
|
75 |
else:
|
76 |
news_results = []
|
77 |
else:
|
|
|
85 |
title = result.get("title", "์ ๋ชฉ ์์")
|
86 |
link = result.get("url", result.get("link", "#"))
|
87 |
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
88 |
+
channel = result.get("channel", result.get("source", "์ ์ ์์"))
|
89 |
+
time = result.get("time", result.get("date", "์ ์ ์๋ ์๊ฐ"))
|
90 |
+
image_url = result.get("img", result.get("thumbnail", ""))
|
91 |
|
92 |
+
# base64๋ก ์ธ์ฝ๋ฉ๋ ์ด๋ฏธ์ง๋ฅผ ์ฒ๋ฆฌ
|
93 |
+
if image_url and not image_url.startswith("data:image"):
|
94 |
image_html = f'<img src="{image_url}" alt="Image" style="max-width: 100%; height: auto;">'
|
95 |
else:
|
96 |
image_html = ''
|
|
|
98 |
article_html = f"""
|
99 |
<div style="margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-bottom: 20px;">
|
100 |
<h3><a href="{link}" target="_blank">{title}</a></h3>
|
101 |
+
<p><strong>{channel}</strong> - {time}</p>
|
102 |
{image_html}
|
103 |
<p>{snippet}</p>
|
104 |
</div>
|