seawolf2357 commited on
Commit
1ee305b
ยท
verified ยท
1 Parent(s): f2842d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -31
app.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,10 @@ 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 +34,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
 
@@ -47,13 +53,9 @@ def search_serphouse(query, country, page, num_result):
47
  return f"Error: {str(e)}"
48
 
49
  def format_results(results):
50
- all_results = "## ๋ชจ๋“  ๋‰ด์Šค ๊ฒฐ๊ณผ\n\n"
51
- result_table = "## ๋‰ด์Šค ๊ฒฐ๊ณผ ํ‘œ\n\n"
52
- result_table += "| ์ œ๋ชฉ | ๋งํฌ | ์‹œ๊ฐ„ | ์ถœ์ฒ˜ | ๋””๋ฒ„๊ทธ ์ •๋ณด |\n"
53
- result_table += "|------|------|------|------|-------------|\n"
54
-
55
- debug_info = "## ๋””๋ฒ„๊ทธ ์ •๋ณด\n\n"
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):
@@ -63,46 +65,48 @@ def format_results(results):
63
  raise ValueError("'results' ํ‚ค๊ฐ€ ์‘๋‹ต์— ์—†์Šต๋‹ˆ๋‹ค.")
64
 
65
  news_results = results["results"].get("news", [])
66
- debug_info += f"๋‰ด์Šค ๊ฒฐ๊ณผ ์ˆ˜: {len(news_results)}\n\n"
67
 
68
- for idx, result in enumerate(news_results):
69
  title = result.get("title", "์ œ๋ชฉ ์—†์Œ")
70
  url = result.get("url", "#")
71
  snippet = result.get("snippet", "๋‚ด์šฉ ์—†์Œ")
72
  channel = result.get("channel", "์•Œ ์ˆ˜ ์—†์Œ")
73
  time_str = result.get("time", "์•Œ ์ˆ˜ ์—†๋Š” ์‹œ๊ฐ„")
74
 
75
- single_debug_info = f"๋‰ด์Šค {idx + 1} - ์ œ๋ชฉ: {title}, ๋งํฌ: {url}, ์‹œ๊ฐ„: {time_str}, ์ถœ์ฒ˜: {channel}"
76
-
77
- result_table += f"| {title[:30]}... | [{url[:30]}...]({url}) | {time_str} | {channel} | {single_debug_info[:50]}... |\n"
78
-
79
  article_info = f"""
80
- ### [{title}]({url})
81
- {snippet}
82
- **์ถœ์ฒ˜:** {channel} - {time_str}
83
- ---
84
- """
 
85
  all_results += article_info
86
 
 
 
87
  except Exception as e:
88
  error_message = f"๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
89
- debug_info += error_message + "\n"
90
- all_results = error_message + "\n\n"
91
-
92
- result_table += f"| ์˜ค๋ฅ˜ ๋ฐœ์ƒ | - | - | - | {error_message} |\n"
93
 
94
- return all_results, result_table + "\n\n" + debug_info
95
-
96
 
97
  def serphouse_search(query, country, page, num_result):
98
  results = search_serphouse(query, country, page, num_result)
99
- all_results, result_table_and_debug = format_results(results)
100
- return all_results, result_table_and_debug
101
 
102
  css = """
103
  footer {
104
  visibility: hidden;
105
  }
 
 
 
 
 
 
106
  """
107
 
108
  iface = gr.Interface(
@@ -114,11 +118,11 @@ iface = gr.Interface(
114
  gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์ˆ˜")
115
  ],
116
  outputs=[
117
- gr.Markdown(label="๋ชจ๋“  ๊ฒฐ๊ณผ"),
118
- gr.Markdown(label="๊ฒฐ๊ณผ ํ‘œ ๋ฐ ๋””๋ฒ„๊ทธ ์ •๋ณด")
119
  ],
120
- title="SERPHouse ๋‰ด์Šค ๊ฒ€์ƒ‰ ์ธํ„ฐํŽ˜์ด์Šค",
121
- description="๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๊ตญ๊ฐ€๋ฅผ ์„ ํƒํ•˜์—ฌ SERPHouse API์—์„œ ๋‰ด์Šค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.",
122
  theme="Nymbo/Nymbo_Theme",
123
  css=css
124
  )
 
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
+
27
  payload = {
28
  "data": {
29
  "q": query,
 
34
  "serp_type": "news",
35
  "page": str(page),
36
  "verbatim": "1",
37
+ "num": str(num_result),
38
+ "date_range": f"{yesterday.strftime('%Y-%m-%d')}:{now.strftime('%Y-%m-%d')}"
39
  }
40
  }
41
 
 
53
  return f"Error: {str(e)}"
54
 
55
  def format_results(results):
56
+ all_results = "<h2>๋ชจ๋“  ๋‰ด์Šค ๊ฒฐ๊ณผ (24์‹œ๊ฐ„ ์ด๋‚ด)</h2><ol>"
57
+ debug_info = "<h2>๋””๋ฒ„๊ทธ ์ •๋ณด</h2>"
58
+ debug_info += f"<pre>{json.dumps(results, indent=2, ensure_ascii=False)}</pre>"
 
 
 
 
59
 
60
  try:
61
  if not isinstance(results, dict):
 
65
  raise ValueError("'results' ํ‚ค๊ฐ€ ์‘๋‹ต์— ์—†์Šต๋‹ˆ๋‹ค.")
66
 
67
  news_results = results["results"].get("news", [])
68
+ debug_info += f"<p>๋‰ด์Šค ๊ฒฐ๊ณผ ์ˆ˜: {len(news_results)}</p>"
69
 
70
+ for result in news_results:
71
  title = result.get("title", "์ œ๋ชฉ ์—†์Œ")
72
  url = result.get("url", "#")
73
  snippet = result.get("snippet", "๋‚ด์šฉ ์—†์Œ")
74
  channel = result.get("channel", "์•Œ ์ˆ˜ ์—†์Œ")
75
  time_str = result.get("time", "์•Œ ์ˆ˜ ์—†๋Š” ์‹œ๊ฐ„")
76
 
 
 
 
 
77
  article_info = f"""
78
+ <li>
79
+ <h3><a href="{url}" target="_blank">{title}</a></h3>
80
+ <p>{snippet}</p>
81
+ <p><strong>์ถœ์ฒ˜:</strong> {channel} - {time_str}</p>
82
+ </li>
83
+ """
84
  all_results += article_info
85
 
86
+ all_results += "</ol>"
87
+
88
  except Exception as e:
89
  error_message = f"๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
90
+ debug_info += f"<p>{error_message}</p>"
91
+ all_results = f"<p>{error_message}</p>"
 
 
92
 
93
+ return all_results, debug_info
 
94
 
95
  def serphouse_search(query, country, page, num_result):
96
  results = search_serphouse(query, country, page, num_result)
97
+ all_results, debug_info = format_results(results)
98
+ return all_results, debug_info
99
 
100
  css = """
101
  footer {
102
  visibility: hidden;
103
  }
104
+ ol {
105
+ padding-left: 20px;
106
+ }
107
+ li {
108
+ margin-bottom: 20px;
109
+ }
110
  """
111
 
112
  iface = gr.Interface(
 
118
  gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์ˆ˜")
119
  ],
120
  outputs=[
121
+ gr.HTML(label="๋ชจ๋“  ๊ฒฐ๊ณผ"),
122
+ gr.HTML(label="๋””๋ฒ„๊ทธ ์ •๋ณด")
123
  ],
124
+ title="SERPHouse 24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค ๊ฒ€์ƒ‰ ์ธํ„ฐํŽ˜์ด์Šค",
125
+ description="๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๊ตญ๊ฐ€๋ฅผ ์„ ํƒํ•˜์—ฌ SERPHouse API์—์„œ 24์‹œ๊ฐ„ ์ด๋‚ด์˜ ๋‰ด์Šค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.",
126
  theme="Nymbo/Nymbo_Theme",
127
  css=css
128
  )