Update app.py
Browse files
app.py
CHANGED
@@ -8,19 +8,16 @@ import json
|
|
8 |
API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
|
9 |
|
10 |
|
|
|
11 |
COUNTRY_CODES = "ae ar at au be bg br ca ch cn co cu cz de eg fr gb gr hk hu id ie il in it jp kr lt lv ma mx my ng nl no nz ph pl pt ro rs ru sa se sg si sk th tr tw ua us ve za".split()
|
12 |
|
13 |
COUNTRIES = {'all': 'All Countries'}
|
14 |
COUNTRIES.update({code: pycountry.countries.get(alpha_2=code.upper()).name for code in COUNTRY_CODES if pycountry.countries.get(alpha_2=code.upper())})
|
15 |
|
16 |
def get_related_keywords(keyword):
|
17 |
-
# ๊ฐ๋จํ ๊ด๋ จ ํค์๋ ์์ฑ ๋ก์ง (์ค์ ๋ก๋ ๋ ๋ณต์กํ ์๊ณ ๋ฆฌ์ฆ์ด๋ API๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค)
|
18 |
return [f"{keyword} news", f"{keyword} technology", f"{keyword} company", f"latest {keyword}"]
|
19 |
|
20 |
-
def get_news(keyword, article_count, country
|
21 |
-
if search_all_countries:
|
22 |
-
country = 'All Countries'
|
23 |
-
|
24 |
country_code = next((code for code, name in COUNTRIES.items() if name == country), 'all')
|
25 |
|
26 |
if country_code == 'all':
|
@@ -41,9 +38,8 @@ def get_news(keyword, article_count, country, extend_search=False, search_all_co
|
|
41 |
'pageSize': article_count
|
42 |
}
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
params['from'] = two_days_ago
|
47 |
|
48 |
debug_info = f"API Request URL: {base_url}\n"
|
49 |
debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
|
@@ -66,10 +62,9 @@ def get_news(keyword, article_count, country, extend_search=False, search_all_co
|
|
66 |
articles = news_data['articles']
|
67 |
|
68 |
if not articles:
|
69 |
-
if
|
70 |
-
|
71 |
-
|
72 |
-
return get_news(keyword, article_count, 'All Countries', extend_search=True, search_all_countries=True)
|
73 |
else:
|
74 |
related_keywords = get_related_keywords(keyword)
|
75 |
suggestions = (f"<p>No news found for the keyword '<strong>{keyword}</strong>' in {country}.</p>"
|
@@ -82,10 +77,10 @@ def get_news(keyword, article_count, country, extend_search=False, search_all_co
|
|
82 |
return suggestions + f"<pre>{debug_info}</pre>"
|
83 |
|
84 |
html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
|
85 |
-
if
|
86 |
-
html_output += "<p><em>
|
87 |
-
|
88 |
-
html_output += "<p><em>
|
89 |
|
90 |
for article in articles:
|
91 |
title = article['title']
|
@@ -115,7 +110,7 @@ iface = gr.Interface(
|
|
115 |
],
|
116 |
outputs=gr.HTML(),
|
117 |
title="Advanced Visual News Search",
|
118 |
-
description="Search for news articles using NewsAPI.
|
119 |
theme=gr.themes.Soft()
|
120 |
)
|
121 |
|
|
|
8 |
API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
|
9 |
|
10 |
|
11 |
+
|
12 |
COUNTRY_CODES = "ae ar at au be bg br ca ch cn co cu cz de eg fr gb gr hk hu id ie il in it jp kr lt lv ma mx my ng nl no nz ph pl pt ro rs ru sa se sg si sk th tr tw ua us ve za".split()
|
13 |
|
14 |
COUNTRIES = {'all': 'All Countries'}
|
15 |
COUNTRIES.update({code: pycountry.countries.get(alpha_2=code.upper()).name for code in COUNTRY_CODES if pycountry.countries.get(alpha_2=code.upper())})
|
16 |
|
17 |
def get_related_keywords(keyword):
|
|
|
18 |
return [f"{keyword} news", f"{keyword} technology", f"{keyword} company", f"latest {keyword}"]
|
19 |
|
20 |
+
def get_news(keyword, article_count, country):
|
|
|
|
|
|
|
21 |
country_code = next((code for code, name in COUNTRIES.items() if name == country), 'all')
|
22 |
|
23 |
if country_code == 'all':
|
|
|
38 |
'pageSize': article_count
|
39 |
}
|
40 |
|
41 |
+
two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
|
42 |
+
params['from'] = two_days_ago
|
|
|
43 |
|
44 |
debug_info = f"API Request URL: {base_url}\n"
|
45 |
debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
|
|
|
62 |
articles = news_data['articles']
|
63 |
|
64 |
if not articles:
|
65 |
+
if country_code != 'all':
|
66 |
+
# ํน์ ๊ตญ๊ฐ์์ ๊ฒฐ๊ณผ๊ฐ ์์ ๊ฒฝ์ฐ, ์ ์ฒด ๊ตญ๊ฐ์์ ๊ฒ์
|
67 |
+
return get_news(keyword, article_count, 'All Countries')
|
|
|
68 |
else:
|
69 |
related_keywords = get_related_keywords(keyword)
|
70 |
suggestions = (f"<p>No news found for the keyword '<strong>{keyword}</strong>' in {country}.</p>"
|
|
|
77 |
return suggestions + f"<pre>{debug_info}</pre>"
|
78 |
|
79 |
html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
|
80 |
+
if country_code == 'all':
|
81 |
+
html_output += "<p><em>Showing results from all countries</em></p>"
|
82 |
+
else:
|
83 |
+
html_output += f"<p><em>Showing results specifically for {country}</em></p>"
|
84 |
|
85 |
for article in articles:
|
86 |
title = article['title']
|
|
|
110 |
],
|
111 |
outputs=gr.HTML(),
|
112 |
title="Advanced Visual News Search",
|
113 |
+
description="Search for news articles using NewsAPI. Results will be specific to the selected country when applicable.",
|
114 |
theme=gr.themes.Soft()
|
115 |
)
|
116 |
|