money-radar / app.py
seawolf2357's picture
Update app.py
f42fdaf verified
raw
history blame
1.99 kB
import gradio as gr
import requests
import feedparser
from datetime import datetime, timedelta
SUPPORTED_COUNTRIES = {
'United States': 'https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en',
'United Kingdom': 'https://news.google.com/rss?hl=en-GB&gl=GB&ceid=GB:en',
'Australia': 'https://news.google.com/rss?hl=en-AU&gl=AU&ceid=AU:en',
'India': 'https://news.google.com/rss?hl=en-IN&gl=IN&ceid=IN:en',
'Canada': 'https://news.google.com/rss?hl=en-CA&gl=CA&ceid=CA:en'
}
def get_news(country, keyword):
if country not in SUPPORTED_COUNTRIES:
return "Selected country is not supported."
url = SUPPORTED_COUNTRIES[country]
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
except requests.RequestException as e:
return f"Error fetching news: {str(e)}"
feed = feedparser.parse(response.content)
time_threshold = datetime.now() - timedelta(hours=48) # 48시간으로 확장
filtered_news = []
for entry in feed.entries:
if (keyword.lower() in entry.title.lower() or
('summary' in entry and keyword.lower() in entry.summary.lower())):
pub_date = datetime(*entry.published_parsed[:6])
if pub_date > time_threshold:
filtered_news.append(f"Title: {entry.title}\nLink: {entry.link}\nDate: {pub_date}\n")
if filtered_news:
return "\n".join(filtered_news)
else:
return (f"No recent news found for the keyword '{keyword}' in {country} "
f"within the last 48 hours.\nTry a different keyword or check back later.")
iface = gr.Interface(
fn=get_news,
inputs=[
gr.Dropdown(choices=list(SUPPORTED_COUNTRIES.keys()), label="Select Country"),
gr.Textbox(label="Enter keyword")
],
outputs="text",
title="Google News Search",
description="Search for news articles from the last 48 hours using Google News RSS feeds."
)
iface.launch()