seawolf2357 commited on
Commit
4409afe
·
verified ·
1 Parent(s): 1ac298a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1,11 +1,10 @@
1
-
2
  import gradio as gr
3
  import requests
4
  from datetime import datetime, timedelta
5
  import pycountry
6
  import json
7
 
8
- # NewsAPI key (이것을 실제 API 키로 대체해야 합니다)
9
  API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
10
 
11
  # 국가 코드 리스트
@@ -21,21 +20,29 @@ for code in COUNTRY_CODES:
21
  COUNTRIES[code] = code.upper()
22
 
23
  def get_news(keyword, article_count, country):
24
- base_url = "https://newsapi.org/v2/top-headlines" if country != 'all' else "https://newsapi.org/v2/everything"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
27
-
28
- params = {
29
- 'apiKey': API_KEY,
30
- 'q': keyword,
31
- 'from': two_days_ago,
32
- 'language': 'en',
33
- 'sortBy': 'publishedAt',
34
- 'pageSize': article_count
35
- }
36
-
37
- if country != 'all':
38
- params['country'] = country
39
 
40
  debug_info = f"API Request URL: {base_url}\n"
41
  debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
@@ -61,7 +68,7 @@ def get_news(keyword, article_count, country):
61
  return (f"<p>No recent news found for the keyword '<strong>{keyword}</strong>' within the last 48 hours.<br>"
62
  f"Try a different keyword or check back later.</p><pre>{debug_info}</pre>")
63
 
64
- html_output = f"<h2>News results for '{keyword}' in {COUNTRIES[country]}</h2>"
65
  for article in articles:
66
  title = article['title']
67
  link = article['url']
@@ -87,8 +94,8 @@ iface = gr.Interface(
87
  gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country")
88
  ],
89
  outputs=gr.HTML(),
90
- title="Debug: Enhanced Visual News Search",
91
- description="Search for news articles from the last 48 hours using NewsAPI. Debug information is included.",
92
  theme=gr.themes.Soft()
93
  )
94
 
 
 
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
4
  import pycountry
5
  import json
6
 
7
+ # NewsAPI key
8
  API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
9
 
10
  # 국가 코드 리스트
 
20
  COUNTRIES[code] = code.upper()
21
 
22
  def get_news(keyword, article_count, country):
23
+ country_code = [code for code, name in COUNTRIES.items() if name == country][0]
24
+
25
+ if country_code == 'all':
26
+ base_url = "https://newsapi.org/v2/everything"
27
+ params = {
28
+ 'apiKey': API_KEY,
29
+ 'q': keyword,
30
+ 'language': 'en',
31
+ 'sortBy': 'publishedAt',
32
+ 'pageSize': article_count
33
+ }
34
+ else:
35
+ base_url = "https://newsapi.org/v2/top-headlines"
36
+ params = {
37
+ 'apiKey': API_KEY,
38
+ 'q': keyword,
39
+ 'language': 'en',
40
+ 'country': country_code,
41
+ 'pageSize': article_count
42
+ }
43
 
44
  two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
45
+ params['from'] = two_days_ago
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  debug_info = f"API Request URL: {base_url}\n"
48
  debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
 
68
  return (f"<p>No recent news found for the keyword '<strong>{keyword}</strong>' within the last 48 hours.<br>"
69
  f"Try a different keyword or check back later.</p><pre>{debug_info}</pre>")
70
 
71
+ html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
72
  for article in articles:
73
  title = article['title']
74
  link = article['url']
 
94
  gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country")
95
  ],
96
  outputs=gr.HTML(),
97
+ title="Enhanced Visual News Search",
98
+ description="Search for news articles from the last 48 hours using NewsAPI.",
99
  theme=gr.themes.Soft()
100
  )
101