File size: 1,439 Bytes
ad0f104
ab30506
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad0f104
ab30506
 
 
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
import gradio as gr
from gnewsclient import gnewsclient
from datetime import datetime, timedelta

# Google ๋‰ด์Šค ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
client = gnewsclient.NewsClient()

# ์ง€์›๋˜๋Š” ๊ตญ๊ฐ€ ๋ชฉ๋ก
supported_countries = client.locations

def get_news(country, keyword):
    # ๊ตญ๊ฐ€ ์„ค์ •
    client.location = country
    client.language = 'en'  # ์˜์–ด๋กœ ์„ค์ •
    
    # ํ˜„์žฌ ์‹œ๊ฐ„ ๊ธฐ์ค€ 24์‹œ๊ฐ„ ์ „ ์‹œ๊ฐ„ ๊ณ„์‚ฐ
    time_threshold = datetime.now() - timedelta(hours=24)
    
    # ๋‰ด์Šค ๊ฒ€์ƒ‰
    news_items = client.get_news(keyword)
    
    # 24์‹œ๊ฐ„ ์ด๋‚ด์˜ ๋‰ด์Šค๋งŒ ํ•„ํ„ฐ๋งํ•˜๊ณ  ์ œ๋ชฉ๊ณผ ๋งํฌ ์ถ”์ถœ
    filtered_news = []
    for item in news_items:
        if 'published' in item:
            news_date = datetime.strptime(item['published'], "%a, %d %b %Y %H:%M:%S %Z")
            if news_date > time_threshold:
                filtered_news.append(f"Title: {item['title']}\nLink: {item['link']}\n")
    
    return "\n".join(filtered_news) if filtered_news else "No recent news found for the given keyword."

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
iface = gr.Interface(
    fn=get_news,
    inputs=[
        gr.Dropdown(choices=supported_countries, label="Select Country"),
        gr.Textbox(label="Enter keyword")
    ],
    outputs="text",
    title="Google News Search",
    description="Search for news articles from the last 24 hours using Google News."
)

# ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
iface.launch()