seawolf2357 commited on
Commit
f166d5c
ยท
verified ยท
1 Parent(s): 9d69618

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -17
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, extend_search=False, search_all_countries=False):
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
- if not extend_search:
45
- two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
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 not extend_search:
70
- return get_news(keyword, article_count, country, extend_search=True)
71
- elif not search_all_countries and country != 'All Countries':
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 extend_search:
86
- html_output += "<p><em>Note: Search range was extended due to limited recent results.</em></p>"
87
- if search_all_countries:
88
- html_output += "<p><em>Note: Search was expanded to all countries due to limited results in the specified country.</em></p>"
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. The search will automatically extend if results are limited.",
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