seawolf2357 commited on
Commit
2e959ba
โ€ข
1 Parent(s): f4723df

Update app-backup.py

Browse files
Files changed (1) hide show
  1. 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
- 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):
@@ -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"๋‰ด์Šค ๊ฒฐ๊ณผ ์ˆ˜: {len(news_results)}\n\n"
68
 
69
- for idx, result in enumerate(news_results):
70
- title = result.get("title", "์ œ๋ชฉ ์—†์Œ")
71
- url = result.get("url", "#")
72
- snippet = result.get("snippet", "๋‚ด์šฉ ์—†์Œ")
73
- channel = result.get("channel", "์•Œ ์ˆ˜ ์—†์Œ")
74
- time_str = result.get("time", "์•Œ ์ˆ˜ ์—†๋Š” ์‹œ๊ฐ„")
 
 
 
 
 
 
 
 
 
 
 
 
 
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 + "\n"
94
- all_results = error_message + "\n\n"
95
-
96
- # ์˜ค๋ฅ˜ ์‹œ ๋””๋ฒ„๊ทธ ์ •๋ณด๋„ ํ‘œ์— ์ถ”๊ฐ€
97
- result_table += f"| ์˜ค๋ฅ˜ ๋ฐœ์ƒ | - | - | - | {error_message} |\n"
98
 
99
- return all_results, result_table + "\n\n" + debug_info
100
-
101
 
102
  def serphouse_search(query, country, page, num_result):
103
  results = search_serphouse(query, country, page, num_result)
104
- all_results, result_table_and_debug = format_results(results)
105
- return all_results, result_table_and_debug
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.Markdown(label="๋ชจ๋“  ๊ฒฐ๊ณผ"),
123
- gr.Markdown(label="๊ฒฐ๊ณผ ํ‘œ ๋ฐ ๋””๋ฒ„๊ทธ ์ •๋ณด")
124
  ],
125
- title="SERPHouse ๋‰ด์Šค ๊ฒ€์ƒ‰ ์ธํ„ฐํŽ˜์ด์Šค",
126
- description="๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๊ตญ๊ฐ€๋ฅผ ์„ ํƒํ•˜์—ฌ SERPHouse API์—์„œ ๋‰ด์Šค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.",
 
 
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"))