seawolf2357 commited on
Commit
aa8bc02
ยท
verified ยท
1 Parent(s): 68a8b0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -1,70 +1,69 @@
 
 
1
  import gradio as gr
2
  import requests
3
  from datetime import datetime, timedelta
 
4
 
5
  # NewsAPI key (์ด๊ฒƒ์„ ์‹ค์ œ API ํ‚ค๋กœ ๋Œ€์ฒดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค)
6
  API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
7
 
8
- SUPPORTED_COUNTRIES = {
9
- 'United States': 'us',
10
- 'United Kingdom': 'gb',
11
- 'Australia': 'au',
12
- 'India': 'in',
13
- 'Canada': 'ca'
14
- }
15
-
16
- def get_news(country, keyword):
17
- country_code = SUPPORTED_COUNTRIES.get(country)
18
- if not country_code:
19
- return "Selected country is not supported."
20
-
21
- base_url = "https://newsapi.org/v2/top-headlines"
22
 
23
  # 48์‹œ๊ฐ„ ์ „์˜ ๋‚ ์งœ๋ฅผ ISO ํ˜•์‹์œผ๋กœ ์–ป๊ธฐ
24
  two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
25
 
26
  params = {
27
  'apiKey': API_KEY,
28
- 'country': country_code,
29
  'q': keyword,
30
  'from': two_days_ago,
31
- 'language': 'en'
 
32
  }
33
 
 
 
 
34
  try:
35
  response = requests.get(base_url, params=params, timeout=10)
36
  response.raise_for_status()
37
  news_data = response.json()
 
 
 
 
 
38
  except requests.RequestException as e:
39
- return f"Error fetching news: {str(e)}"
40
 
41
  if news_data['status'] != 'ok':
42
- return f"API Error: {news_data.get('message', 'Unknown error occurred')}"
43
 
44
  articles = news_data['articles']
45
 
46
  if not articles:
47
- return (f"No recent news found for the keyword '{keyword}' in {country} "
48
- f"within the last 48 hours.\nTry a different keyword or check back later.")
49
 
50
  filtered_news = []
51
- for article in articles:
52
  title = article['title']
53
  link = article['url']
54
  pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
55
  filtered_news.append(f"Title: {title}\nLink: {link}\nDate: {pub_date}\n")
56
 
57
- return "\n".join(filtered_news)
 
58
 
59
  iface = gr.Interface(
60
  fn=get_news,
61
  inputs=[
62
- gr.Dropdown(choices=list(SUPPORTED_COUNTRIES.keys()), label="Select Country"),
63
  gr.Textbox(label="Enter keyword")
64
  ],
65
  outputs="text",
66
- title="News Search",
67
- description="Search for news articles from the last 48 hours using NewsAPI."
68
  )
69
 
70
  iface.launch()
 
1
+
2
+
3
  import gradio as gr
4
  import requests
5
  from datetime import datetime, timedelta
6
+ import json
7
 
8
  # NewsAPI key (์ด๊ฒƒ์„ ์‹ค์ œ API ํ‚ค๋กœ ๋Œ€์ฒดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค)
9
  API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
10
 
11
+ def get_news(keyword):
12
+ base_url = "https://newsapi.org/v2/everything"
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # 48์‹œ๊ฐ„ ์ „์˜ ๋‚ ์งœ๋ฅผ ISO ํ˜•์‹์œผ๋กœ ์–ป๊ธฐ
15
  two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
16
 
17
  params = {
18
  'apiKey': API_KEY,
 
19
  'q': keyword,
20
  'from': two_days_ago,
21
+ 'language': 'en',
22
+ 'sortBy': 'publishedAt'
23
  }
24
 
25
+ debug_info = f"API Request URL: {base_url}\n"
26
+ debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
27
+
28
  try:
29
  response = requests.get(base_url, params=params, timeout=10)
30
  response.raise_for_status()
31
  news_data = response.json()
32
+
33
+ debug_info += f"API Response Status: {response.status_code}\n"
34
+ debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n\n"
35
+ debug_info += f"API Response Body: {json.dumps(news_data, indent=2)}\n\n"
36
+
37
  except requests.RequestException as e:
38
+ return f"Error fetching news: {str(e)}\n\nDebug Info:\n{debug_info}"
39
 
40
  if news_data['status'] != 'ok':
41
+ return f"API Error: {news_data.get('message', 'Unknown error occurred')}\n\nDebug Info:\n{debug_info}"
42
 
43
  articles = news_data['articles']
44
 
45
  if not articles:
46
+ return (f"No recent news found for the keyword '{keyword}' within the last 48 hours.\n"
47
+ f"Try a different keyword or check back later.\n\nDebug Info:\n{debug_info}")
48
 
49
  filtered_news = []
50
+ for article in articles[:10]: # ์ตœ๋Œ€ 10๊ฐœ์˜ ๊ธฐ์‚ฌ๋งŒ ํ‘œ์‹œ
51
  title = article['title']
52
  link = article['url']
53
  pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
54
  filtered_news.append(f"Title: {title}\nLink: {link}\nDate: {pub_date}\n")
55
 
56
+ result = "\n".join(filtered_news)
57
+ return f"{result}\n\nDebug Info:\n{debug_info}"
58
 
59
  iface = gr.Interface(
60
  fn=get_news,
61
  inputs=[
 
62
  gr.Textbox(label="Enter keyword")
63
  ],
64
  outputs="text",
65
+ title="News Search (Debug Version)",
66
+ description="Search for news articles from the last 48 hours using NewsAPI. This version includes debug information."
67
  )
68
 
69
  iface.launch()