Update app.py
Browse files
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)).
|
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':
|
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 |
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|