seawolf2357 commited on
Commit
ed4ef14
·
verified ·
1 Parent(s): dac6155

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -7,14 +7,12 @@ import json
7
  # NewsAPI key
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
- # 국가 코드와 이름 매핑
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_news(keyword, article_count, country):
18
  country_code = next((code for code, name in COUNTRIES.items() if name == country), 'all')
19
 
20
  if country_code == 'all':
@@ -35,8 +33,9 @@ def get_news(keyword, article_count, country):
35
  'pageSize': article_count
36
  }
37
 
38
- two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
39
- params['from'] = two_days_ago
 
40
 
41
  debug_info = f"API Request URL: {base_url}\n"
42
  debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
@@ -59,10 +58,22 @@ def get_news(keyword, article_count, country):
59
  articles = news_data['articles']
60
 
61
  if not articles:
62
- return (f"<p>No recent news found for the keyword '<strong>{keyword}</strong>' in {country} within the last 48 hours.<br>"
63
- f"Try a different keyword or check back later.</p><pre>{debug_info}</pre>")
 
 
 
 
 
 
 
 
 
64
 
65
  html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
 
 
 
66
  for article in articles:
67
  title = article['title']
68
  link = article['url']
@@ -91,7 +102,7 @@ iface = gr.Interface(
91
  ],
92
  outputs=gr.HTML(),
93
  title="Enhanced Visual News Search",
94
- description="Search for news articles from the last 48 hours using NewsAPI.",
95
  theme=gr.themes.Soft()
96
  )
97
 
 
7
  # NewsAPI key
8
  API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
9
 
 
10
  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()
11
 
 
12
  COUNTRIES = {'all': 'All Countries'}
13
  COUNTRIES.update({code: pycountry.countries.get(alpha_2=code.upper()).name for code in COUNTRY_CODES if pycountry.countries.get(alpha_2=code.upper())})
14
 
15
+ def get_news(keyword, article_count, country, extend_search=False):
16
  country_code = next((code for code, name in COUNTRIES.items() if name == country), 'all')
17
 
18
  if country_code == 'all':
 
33
  'pageSize': article_count
34
  }
35
 
36
+ if not extend_search:
37
+ two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
38
+ params['from'] = two_days_ago
39
 
40
  debug_info = f"API Request URL: {base_url}\n"
41
  debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
 
58
  articles = news_data['articles']
59
 
60
  if not articles:
61
+ if not extend_search:
62
+ return get_news(keyword, article_count, country, extend_search=True)
63
+ else:
64
+ return (f"<p>No news found for the keyword '<strong>{keyword}</strong>' in {country}.<br>"
65
+ f"Suggestions:</p>"
66
+ f"<ul>"
67
+ f"<li>Try a different keyword</li>"
68
+ f"<li>Expand your search to all countries</li>"
69
+ f"<li>Increase the number of articles</li>"
70
+ f"</ul>"
71
+ f"<pre>{debug_info}</pre>")
72
 
73
  html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
74
+ if extend_search:
75
+ html_output += "<p><em>Note: Search range was extended due to limited recent results.</em></p>"
76
+
77
  for article in articles:
78
  title = article['title']
79
  link = article['url']
 
102
  ],
103
  outputs=gr.HTML(),
104
  title="Enhanced Visual News Search",
105
+ description="Search for news articles using NewsAPI. If recent results are limited, the search range will be automatically extended.",
106
  theme=gr.themes.Soft()
107
  )
108