seawolf2357 commited on
Commit
68a8b0c
·
verified ·
1 Parent(s): 1ceec2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -32
app.py CHANGED
@@ -1,15 +1,16 @@
1
  import gradio as gr
2
  import requests
3
- from bs4 import BeautifulSoup
4
  from datetime import datetime, timedelta
5
- import pytz
 
 
6
 
7
  SUPPORTED_COUNTRIES = {
8
- 'United States': 'US',
9
- 'United Kingdom': 'GB',
10
- 'Australia': 'AU',
11
- 'India': 'IN',
12
- 'Canada': 'CA'
13
  }
14
 
15
  def get_news(country, keyword):
@@ -17,39 +18,44 @@ def get_news(country, keyword):
17
  if not country_code:
18
  return "Selected country is not supported."
19
 
20
- base_url = f"https://news.google.com/search?q={keyword}&hl=en-{country_code}&gl={country_code}&ceid={country_code}:en"
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  try:
23
- response = requests.get(base_url, timeout=10)
24
  response.raise_for_status()
 
25
  except requests.RequestException as e:
26
  return f"Error fetching news: {str(e)}"
27
 
28
- soup = BeautifulSoup(response.text, 'html.parser')
29
- articles = soup.find_all('article')
30
-
31
- time_threshold = datetime.now(pytz.utc) - timedelta(hours=48)
32
-
33
- filtered_news = []
34
- for article in articles:
35
- title_elem = article.find('h3', class_='ipQwMb ekueJc RD0gLb')
36
- link_elem = article.find('a', class_='VDXfz')
37
- time_elem = article.find('time', class_='WW6dff uQIVzc Sksgp')
38
-
39
- if title_elem and link_elem and time_elem:
40
- title = title_elem.text
41
- link = f"https://news.google.com{link_elem['href'][1:]}"
42
- pub_time = datetime.fromtimestamp(int(time_elem['datetime'])/1000, pytz.utc)
43
-
44
- if pub_time > time_threshold:
45
- filtered_news.append(f"Title: {title}\nLink: {link}\nDate: {pub_time}\n")
46
 
47
- if filtered_news:
48
- return "\n".join(filtered_news)
49
- else:
50
  return (f"No recent news found for the keyword '{keyword}' in {country} "
51
  f"within the last 48 hours.\nTry a different keyword or check back later.")
52
 
 
 
 
 
 
 
 
 
 
53
  iface = gr.Interface(
54
  fn=get_news,
55
  inputs=[
@@ -57,8 +63,8 @@ iface = gr.Interface(
57
  gr.Textbox(label="Enter keyword")
58
  ],
59
  outputs="text",
60
- title="Google News Search",
61
- description="Search for news articles from the last 48 hours using Google News."
62
  )
63
 
64
  iface.launch()
 
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):
 
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=[
 
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()