seawolf2357
commited on
Commit
โข
2e959ba
1
Parent(s):
f4723df
Update app-backup.py
Browse files- app-backup.py +59 -44
app-backup.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
|
|
4 |
|
5 |
API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
|
6 |
|
@@ -19,6 +20,11 @@ MAJOR_COUNTRIES = [
|
|
19 |
|
20 |
def search_serphouse(query, country, page, num_result):
|
21 |
url = "https://api.serphouse.com/serp/live"
|
|
|
|
|
|
|
|
|
|
|
22 |
payload = {
|
23 |
"data": {
|
24 |
"q": query,
|
@@ -29,7 +35,8 @@ def search_serphouse(query, country, page, num_result):
|
|
29 |
"serp_type": "news",
|
30 |
"page": str(page),
|
31 |
"verbatim": "1",
|
32 |
-
"num": str(num_result)
|
|
|
33 |
}
|
34 |
}
|
35 |
|
@@ -44,16 +51,21 @@ def search_serphouse(query, country, page, num_result):
|
|
44 |
response.raise_for_status()
|
45 |
return response.json()
|
46 |
except requests.RequestException as e:
|
47 |
-
|
|
|
|
|
|
|
48 |
|
49 |
def format_results(results):
|
50 |
-
all_results = "
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
-
debug_info
|
56 |
-
debug_info += f"Raw API Response:\n```json\n{json.dumps(results, indent=2)}\n```\n\n"
|
57 |
|
58 |
try:
|
59 |
if not isinstance(results, dict):
|
@@ -62,52 +74,53 @@ def format_results(results):
|
|
62 |
if "results" not in results:
|
63 |
raise ValueError("'results' ํค๊ฐ ์๋ต์ ์์ต๋๋ค.")
|
64 |
|
65 |
-
# 'news' ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ด
|
66 |
news_results = results["results"].get("news", [])
|
67 |
-
debug_info += f"
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
|
77 |
-
single_debug_info = f"๋ด์ค {idx + 1} - ์ ๋ชฉ: {title}, ๋งํฌ: {url}, ์๊ฐ: {time_str}, ์ถ์ฒ: {channel}"
|
78 |
-
|
79 |
-
# ๊ฒฐ๊ณผ ํ์ ํ ์ถ๊ฐ (๋๋ฒ๊ทธ ์ ๋ณด ํฌํจ)
|
80 |
-
result_table += f"| {title[:30]}... | [{url[:30]}...]({url}) | {time_str} | {channel} | {single_debug_info[:50]}... |\n"
|
81 |
-
|
82 |
-
# ๋ด์ค ๊ธฐ์ฌ ์ ๋ณด
|
83 |
-
article_info = f"""
|
84 |
-
### [{title}]({url})
|
85 |
-
{snippet}
|
86 |
-
**์ถ์ฒ:** {channel} - {time_str}
|
87 |
-
---
|
88 |
-
"""
|
89 |
-
all_results += article_info
|
90 |
|
91 |
except Exception as e:
|
92 |
error_message = f"๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
93 |
-
debug_info += error_message
|
94 |
-
all_results
|
95 |
-
|
96 |
-
# ์ค๋ฅ ์ ๋๋ฒ๊ทธ ์ ๋ณด๋ ํ์ ์ถ๊ฐ
|
97 |
-
result_table += f"| ์ค๋ฅ ๋ฐ์ | - | - | - | {error_message} |\n"
|
98 |
|
99 |
-
return all_results,
|
100 |
-
|
101 |
|
102 |
def serphouse_search(query, country, page, num_result):
|
103 |
results = search_serphouse(query, country, page, num_result)
|
104 |
-
all_results,
|
105 |
-
return all_results,
|
106 |
|
107 |
css = """
|
108 |
footer {
|
109 |
visibility: hidden;
|
110 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
"""
|
112 |
|
113 |
iface = gr.Interface(
|
@@ -119,13 +132,15 @@ iface = gr.Interface(
|
|
119 |
gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์")
|
120 |
],
|
121 |
outputs=[
|
122 |
-
gr.
|
123 |
-
gr.
|
124 |
],
|
125 |
-
|
126 |
-
|
|
|
|
|
127 |
theme="Nymbo/Nymbo_Theme",
|
128 |
css=css
|
129 |
)
|
130 |
|
131 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
4 |
+
from datetime import datetime, timedelta
|
5 |
|
6 |
API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
|
7 |
|
|
|
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,
|
|
|
35 |
"serp_type": "news",
|
36 |
"page": str(page),
|
37 |
"verbatim": "1",
|
38 |
+
"num": str(num_result),
|
39 |
+
"date_range": date_range
|
40 |
}
|
41 |
}
|
42 |
|
|
|
51 |
response.raise_for_status()
|
52 |
return response.json()
|
53 |
except requests.RequestException as e:
|
54 |
+
error_msg = f"Error: {str(e)}"
|
55 |
+
if response.text:
|
56 |
+
error_msg += f"\nResponse content: {response.text}"
|
57 |
+
return {"error": error_msg}
|
58 |
|
59 |
def format_results(results):
|
60 |
+
all_results = "<h2>๋ชจ๋ ๋ด์ค ๊ฒฐ๊ณผ (24์๊ฐ ์ด๋ด)</h2>"
|
61 |
+
debug_info = "<h2>๋๋ฒ๊ทธ ์ ๋ณด</h2>"
|
62 |
+
|
63 |
+
if isinstance(results, dict) and "error" in results:
|
64 |
+
all_results += f"<p>์ค๋ฅ ๋ฐ์: {results['error']}</p>"
|
65 |
+
debug_info += f"<pre>{results['error']}</pre>"
|
66 |
+
return all_results, debug_info
|
67 |
|
68 |
+
debug_info += f"<pre>{json.dumps(results, indent=2, ensure_ascii=False)}</pre>"
|
|
|
69 |
|
70 |
try:
|
71 |
if not isinstance(results, dict):
|
|
|
74 |
if "results" not in results:
|
75 |
raise ValueError("'results' ํค๊ฐ ์๋ต์ ์์ต๋๋ค.")
|
76 |
|
|
|
77 |
news_results = results["results"].get("news", [])
|
78 |
+
debug_info += f"<p>๋ด์ค ๊ฒฐ๊ณผ ์: {len(news_results)}</p>"
|
79 |
|
80 |
+
if not news_results:
|
81 |
+
all_results += "<p>๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.</p>"
|
82 |
+
else:
|
83 |
+
all_results += "<ol>"
|
84 |
+
for result in news_results:
|
85 |
+
title = result.get("title", "์ ๋ชฉ ์์")
|
86 |
+
url = result.get("url", "#")
|
87 |
+
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
88 |
+
channel = result.get("channel", "์ ์ ์์")
|
89 |
+
time_str = result.get("time", "์ ์ ์๋ ์๊ฐ")
|
90 |
+
|
91 |
+
article_info = f"""
|
92 |
+
<li>
|
93 |
+
<h3><a href="{url}" target="_blank">{title}</a></h3>
|
94 |
+
<p>{snippet}</p>
|
95 |
+
<p><strong>์ถ์ฒ:</strong> {channel} - {time_str}</p>
|
96 |
+
</li>
|
97 |
+
"""
|
98 |
+
all_results += article_info
|
99 |
|
100 |
+
all_results += "</ol>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
except Exception as e:
|
103 |
error_message = f"๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
104 |
+
debug_info += f"<p>{error_message}</p>"
|
105 |
+
all_results += f"<p>{error_message}</p>"
|
|
|
|
|
|
|
106 |
|
107 |
+
return all_results, debug_info
|
|
|
108 |
|
109 |
def serphouse_search(query, country, page, num_result):
|
110 |
results = search_serphouse(query, country, page, num_result)
|
111 |
+
all_results, debug_info = format_results(results)
|
112 |
+
return all_results, debug_info
|
113 |
|
114 |
css = """
|
115 |
footer {
|
116 |
visibility: hidden;
|
117 |
}
|
118 |
+
ol {
|
119 |
+
padding-left: 20px;
|
120 |
+
}
|
121 |
+
li {
|
122 |
+
margin-bottom: 20px;
|
123 |
+
}
|
124 |
"""
|
125 |
|
126 |
iface = gr.Interface(
|
|
|
132 |
gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์")
|
133 |
],
|
134 |
outputs=[
|
135 |
+
gr.HTML(label="๋ชจ๋ ๊ฒฐ๊ณผ"),
|
136 |
+
gr.HTML(label="๋๋ฒ๊ทธ ์ ๋ณด")
|
137 |
],
|
138 |
+
|
139 |
+
|
140 |
+
title="24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค",
|
141 |
+
description="๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ๊ตญ๊ฐ๋ฅผ ์ ํํ์ฌ 24์๊ฐ ์ด๋ด์ ๋ด์ค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.",
|
142 |
theme="Nymbo/Nymbo_Theme",
|
143 |
css=css
|
144 |
)
|
145 |
|
146 |
+
iface.launch(auth=("gini","pick"))
|