Spaces:
Building
Building
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() |