|
import gradio as gr |
|
from gnewsclient import gnewsclient |
|
from datetime import datetime, timedelta |
|
|
|
|
|
client = gnewsclient.NewsClient() |
|
|
|
|
|
supported_countries = client.locations |
|
|
|
def get_news(country, keyword): |
|
|
|
client.location = country |
|
client.language = 'en' |
|
|
|
|
|
time_threshold = datetime.now() - timedelta(hours=24) |
|
|
|
|
|
news_items = client.get_news(keyword) |
|
|
|
|
|
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." |
|
|
|
|
|
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() |