seawolf2357 commited on
Commit
158b524
ยท
verified ยท
1 Parent(s): 94b1606

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
 
4
 
5
 
6
  # Google Custom Search API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID
@@ -68,10 +69,9 @@ COUNTRIES = {
68
  'Wallis and Futuna': 'countryWF', 'Western Sahara': 'countryEH', 'Yemen': 'countryYE', 'Zambia': 'countryZM', 'Zimbabwe': 'countryZW'
69
  }
70
 
71
-
72
  def search_news(keyword, country):
73
  # 24์‹œ๊ฐ„ ์ „ ๋‚ ์งœ ๊ณ„์‚ฐ
74
- one_day_ago = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
75
 
76
  # API ์š”์ฒญ URL ๋ฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •
77
  url = "https://www.googleapis.com/customsearch/v1"
@@ -82,7 +82,7 @@ def search_news(keyword, country):
82
  'dateRestrict': 'd1', # ์ตœ๊ทผ 1์ผ ๋‚ด ๊ฒฐ๊ณผ๋งŒ
83
  'lr': 'lang_en', # ์˜์–ด ๊ฒฐ๊ณผ๋งŒ
84
  'sort': 'date', # ๋‚ ์งœ์ˆœ ์ •๋ ฌ
85
- 'num': 10, # ์ตœ๋Œ€ 10๊ฐœ ๊ฒฐ๊ณผ
86
  'siteSearch': 'news.google.com', # Google News๋กœ ์ œํ•œ
87
  }
88
 
@@ -93,6 +93,11 @@ def search_news(keyword, country):
93
  response = requests.get(url, params=params)
94
  results = response.json()
95
 
 
 
 
 
 
96
  # ๊ฒฐ๊ณผ ํฌ๋งทํŒ…
97
  formatted_results = ""
98
  if 'items' in results:
@@ -102,9 +107,12 @@ def search_news(keyword, country):
102
  snippet = item['snippet']
103
  formatted_results += f"<h3><a href='{link}' target='_blank'>{title}</a></h3>"
104
  formatted_results += f"<p>{snippet}</p><br>"
 
105
  else:
106
  formatted_results = f"No news found for '{keyword}' in {country} within the last 24 hours."
107
 
 
 
108
  return formatted_results
109
 
110
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
@@ -116,7 +124,7 @@ iface = gr.Interface(
116
  ],
117
  outputs=gr.HTML(),
118
  title="Google News Search",
119
- description="Search for news articles from the last 24 hours using Google Custom Search API."
120
  )
121
 
122
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
 
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
4
+ import json
5
 
6
 
7
  # Google Custom Search API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID
 
69
  'Wallis and Futuna': 'countryWF', 'Western Sahara': 'countryEH', 'Yemen': 'countryYE', 'Zambia': 'countryZM', 'Zimbabwe': 'countryZW'
70
  }
71
 
 
72
  def search_news(keyword, country):
73
  # 24์‹œ๊ฐ„ ์ „ ๋‚ ์งœ ๊ณ„์‚ฐ
74
+ one_day_ago = (datetime.now() - timedelta(days=1)).isoformat()
75
 
76
  # API ์š”์ฒญ URL ๋ฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •
77
  url = "https://www.googleapis.com/customsearch/v1"
 
82
  'dateRestrict': 'd1', # ์ตœ๊ทผ 1์ผ ๋‚ด ๊ฒฐ๊ณผ๋งŒ
83
  'lr': 'lang_en', # ์˜์–ด ๊ฒฐ๊ณผ๋งŒ
84
  'sort': 'date', # ๋‚ ์งœ์ˆœ ์ •๋ ฌ
85
+ 'num': 100, # ์ตœ๋Œ€ 100๊ฐœ ๊ฒฐ๊ณผ
86
  'siteSearch': 'news.google.com', # Google News๋กœ ์ œํ•œ
87
  }
88
 
 
93
  response = requests.get(url, params=params)
94
  results = response.json()
95
 
96
+ # ๋””๋ฒ„๊ทธ ์ •๋ณด
97
+ debug_info = f"API Request URL: {response.url}\n"
98
+ debug_info += f"API Response Status: {response.status_code}\n"
99
+ debug_info += f"API Response Body: {json.dumps(results, indent=2)}\n"
100
+
101
  # ๊ฒฐ๊ณผ ํฌ๋งทํŒ…
102
  formatted_results = ""
103
  if 'items' in results:
 
107
  snippet = item['snippet']
108
  formatted_results += f"<h3><a href='{link}' target='_blank'>{title}</a></h3>"
109
  formatted_results += f"<p>{snippet}</p><br>"
110
+ formatted_results += f"<p>Total results: {len(results['items'])}</p>"
111
  else:
112
  formatted_results = f"No news found for '{keyword}' in {country} within the last 24 hours."
113
 
114
+ formatted_results += f"<details><summary>Debug Info</summary><pre>{debug_info}</pre></details>"
115
+
116
  return formatted_results
117
 
118
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
 
124
  ],
125
  outputs=gr.HTML(),
126
  title="Google News Search",
127
+ description="Search for up to 100 news articles from the last 24 hours using Google Custom Search API."
128
  )
129
 
130
  # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰