seawolf2357 commited on
Commit
9a82749
·
verified ·
1 Parent(s): f42fdaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -18
app.py CHANGED
@@ -1,39 +1,48 @@
1
  import gradio as gr
2
  import requests
3
- import feedparser
4
  from datetime import datetime, timedelta
 
5
 
6
  SUPPORTED_COUNTRIES = {
7
- 'United States': 'https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en',
8
- 'United Kingdom': 'https://news.google.com/rss?hl=en-GB&gl=GB&ceid=GB:en',
9
- 'Australia': 'https://news.google.com/rss?hl=en-AU&gl=AU&ceid=AU:en',
10
- 'India': 'https://news.google.com/rss?hl=en-IN&gl=IN&ceid=IN:en',
11
- 'Canada': 'https://news.google.com/rss?hl=en-CA&gl=CA&ceid=CA:en'
12
  }
13
 
14
  def get_news(country, keyword):
15
- if country not in SUPPORTED_COUNTRIES:
 
16
  return "Selected country is not supported."
17
 
18
- url = SUPPORTED_COUNTRIES[country]
19
 
20
  try:
21
- response = requests.get(url, timeout=10)
22
  response.raise_for_status()
23
  except requests.RequestException as e:
24
  return f"Error fetching news: {str(e)}"
25
 
26
- feed = feedparser.parse(response.content)
 
27
 
28
- time_threshold = datetime.now() - timedelta(hours=48) # 48시간으로 확장
29
 
30
  filtered_news = []
31
- for entry in feed.entries:
32
- if (keyword.lower() in entry.title.lower() or
33
- ('summary' in entry and keyword.lower() in entry.summary.lower())):
34
- pub_date = datetime(*entry.published_parsed[:6])
35
- if pub_date > time_threshold:
36
- filtered_news.append(f"Title: {entry.title}\nLink: {entry.link}\nDate: {pub_date}\n")
 
 
 
 
 
 
37
 
38
  if filtered_news:
39
  return "\n".join(filtered_news)
@@ -49,7 +58,7 @@ iface = gr.Interface(
49
  ],
50
  outputs="text",
51
  title="Google News Search",
52
- description="Search for news articles from the last 48 hours using Google News RSS feeds."
53
  )
54
 
55
  iface.launch()
 
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):
16
+ country_code = SUPPORTED_COUNTRIES.get(country)
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)
 
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()