Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
import
|
4 |
from datetime import datetime, timedelta
|
|
|
5 |
|
6 |
SUPPORTED_COUNTRIES = {
|
7 |
-
'United States': '
|
8 |
-
'United Kingdom': '
|
9 |
-
'Australia': '
|
10 |
-
'India': '
|
11 |
-
'Canada': '
|
12 |
}
|
13 |
|
14 |
def get_news(country, keyword):
|
15 |
-
|
|
|
16 |
return "Selected country is not supported."
|
17 |
|
18 |
-
|
19 |
|
20 |
try:
|
21 |
-
response = requests.get(
|
22 |
response.raise_for_status()
|
23 |
except requests.RequestException as e:
|
24 |
return f"Error fetching news: {str(e)}"
|
25 |
|
26 |
-
|
|
|
27 |
|
28 |
-
time_threshold = datetime.now() - timedelta(hours=48)
|
29 |
|
30 |
filtered_news = []
|
31 |
-
for
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
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()
|