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()