Spaces:
openfree
/
Running on CPU Upgrade

seawolf2357 commited on
Commit
eb43e13
ยท
verified ยท
1 Parent(s): fbd003b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -3,7 +3,7 @@ import requests
3
  import json
4
  from datetime import datetime, timedelta
5
 
6
- API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
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
- # 'organic' ํ‚ค ํ™•์ธ
74
- if 'organic' in results_content:
75
- news_results = results_content['organic']
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
- source = result.get("source", result.get("channel", "์•Œ ์ˆ˜ ์—†์Œ"))
90
- date = result.get("date", result.get("time", "์•Œ ์ˆ˜ ์—†๋Š” ์‹œ๊ฐ„"))
91
- image_url = result.get("thumbnail", result.get("img", ""))
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>{source}</strong> - {date}</p>
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>