Update app.py
Browse files
app.py
CHANGED
@@ -67,7 +67,7 @@ COUNTRIES = {
|
|
67 |
'Wallis and Futuna': 'countryWF', 'Western Sahara': 'countryEH', 'Yemen': 'countryYE', 'Zambia': 'countryZM', 'Zimbabwe': 'countryZW'
|
68 |
}
|
69 |
|
70 |
-
def search_news(keyword, country):
|
71 |
# 24시간 전 날짜 계산
|
72 |
one_day_ago = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
|
73 |
|
@@ -80,11 +80,11 @@ def search_news(keyword, country):
|
|
80 |
'dateRestrict': 'd1', # 최근 1일 내 결과만
|
81 |
'lr': 'lang_en', # 영어 결과만
|
82 |
'sort': 'date', # 날짜순 정렬
|
83 |
-
'num':
|
84 |
'siteSearch': 'news.google.com', # Google News로 제한
|
85 |
}
|
86 |
|
87 |
-
if country != 'All Countries':
|
88 |
params['cr'] = COUNTRIES[country]
|
89 |
|
90 |
# API 요청
|
@@ -105,16 +105,46 @@ def search_news(keyword, country):
|
|
105 |
|
106 |
return formatted_results
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
# Gradio 인터페이스 생성
|
109 |
iface = gr.Interface(
|
110 |
-
fn=
|
111 |
inputs=[
|
112 |
gr.Textbox(label="Enter keyword (in English)"),
|
113 |
-
gr.Dropdown(choices=['All Countries'] + list(COUNTRIES.keys()), label="Select Country")
|
|
|
114 |
],
|
115 |
outputs=gr.HTML(),
|
116 |
title="Google News Search",
|
117 |
-
description="Search for news articles from the last 24 hours using Google Custom Search API."
|
118 |
)
|
119 |
|
120 |
# 애플리케이션 실행
|
|
|
67 |
'Wallis and Futuna': 'countryWF', 'Western Sahara': 'countryEH', 'Yemen': 'countryYE', 'Zambia': 'countryZM', 'Zimbabwe': 'countryZW'
|
68 |
}
|
69 |
|
70 |
+
def search_news(keyword, country, search_all_countries=False):
|
71 |
# 24시간 전 날짜 계산
|
72 |
one_day_ago = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
|
73 |
|
|
|
80 |
'dateRestrict': 'd1', # 최근 1일 내 결과만
|
81 |
'lr': 'lang_en', # 영어 결과만
|
82 |
'sort': 'date', # 날짜순 정렬
|
83 |
+
'num': 100, # 최대 100개 결과
|
84 |
'siteSearch': 'news.google.com', # Google News로 제한
|
85 |
}
|
86 |
|
87 |
+
if not search_all_countries and country != 'All Countries':
|
88 |
params['cr'] = COUNTRIES[country]
|
89 |
|
90 |
# API 요청
|
|
|
105 |
|
106 |
return formatted_results
|
107 |
|
108 |
+
def search_all_countries(keyword):
|
109 |
+
def search_country(country_name):
|
110 |
+
result = search_news(keyword, country_name)
|
111 |
+
if "No news found" not in result:
|
112 |
+
return country_name, result
|
113 |
+
return None
|
114 |
+
|
115 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
|
116 |
+
future_to_country = {executor.submit(search_country, country): country for country in COUNTRIES.keys()}
|
117 |
+
matching_countries = []
|
118 |
+
for future in concurrent.futures.as_completed(future_to_country):
|
119 |
+
result = future.result()
|
120 |
+
if result:
|
121 |
+
matching_countries.append(result)
|
122 |
+
|
123 |
+
if matching_countries:
|
124 |
+
output = "<h2>Countries with matching news:</h2>"
|
125 |
+
for country, news in matching_countries:
|
126 |
+
output += f"<h3>{country}</h3>{news}<hr>"
|
127 |
+
return output
|
128 |
+
else:
|
129 |
+
return f"No news found for '{keyword}' in any country within the last 24 hours."
|
130 |
+
|
131 |
+
def news_search(keyword, country, search_all):
|
132 |
+
if search_all:
|
133 |
+
return search_all_countries(keyword)
|
134 |
+
else:
|
135 |
+
return search_news(keyword, country)
|
136 |
+
|
137 |
# Gradio 인터페이스 생성
|
138 |
iface = gr.Interface(
|
139 |
+
fn=news_search,
|
140 |
inputs=[
|
141 |
gr.Textbox(label="Enter keyword (in English)"),
|
142 |
+
gr.Dropdown(choices=['All Countries'] + list(COUNTRIES.keys()), label="Select Country"),
|
143 |
+
gr.Checkbox(label="Search all countries")
|
144 |
],
|
145 |
outputs=gr.HTML(),
|
146 |
title="Google News Search",
|
147 |
+
description="Search for news articles from the last 24 hours using Google Custom Search API. You can search in a specific country or across all countries."
|
148 |
)
|
149 |
|
150 |
# 애플리케이션 실행
|