File size: 1,993 Bytes
ad0f104
18c9d60
 
ab30506
 
18c9d60
 
 
 
 
 
 
ab30506
 
18c9d60
 
 
 
 
f42fdaf
 
 
 
 
 
18c9d60
ab30506
f42fdaf
ab30506
 
18c9d60
f42fdaf
 
18c9d60
 
f42fdaf
ab30506
f42fdaf
 
 
 
 
ab30506
 
 
 
18c9d60
ab30506
ad0f104
ab30506
 
f42fdaf
ad0f104
 
ab30506
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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()