Spaces:
Building
Building
import gradio as gr | |
import requests | |
from datetime import datetime, timedelta | |
import json | |
from functools import lru_cache | |
# Google Custom Search API ํค์ ๊ฒ์ ์์ง ID | |
API_KEY = "AIzaSyB8wNdEL8-SAvelRq-zenLLU-cUEmsj7uE" | |
SEARCH_ENGINE_ID = "c01abc75e1b95483d" | |
# ์ง์๋๋ ๊ตญ๊ฐ ๋ฆฌ์คํธ | |
COUNTRIES = { | |
'United States': 'US', 'United Kingdom': 'GB', 'Canada': 'CA', 'Australia': 'AU', 'India': 'IN', | |
'Germany': 'DE', 'France': 'FR', 'Japan': 'JP', 'South Korea': 'KR', 'Brazil': 'BR', | |
'Mexico': 'MX', 'Spain': 'ES', 'Italy': 'IT', 'Netherlands': 'NL', 'Russia': 'RU', | |
'Sweden': 'SE', 'Switzerland': 'CH', 'Poland': 'PL', 'Turkey': 'TR', 'Saudi Arabia': 'SA' | |
} | |
def cached_search(cache_key): | |
return search_news_impl(*json.loads(cache_key)) | |
def search_news(keyword, country): | |
cache_key = json.dumps((keyword, country)) | |
return cached_search(cache_key) | |
def search_news_impl(keyword, country): | |
url = "https://www.googleapis.com/customsearch/v1" | |
# ๋ ์ง ๋ฒ์ ์ค์ (์ต๊ทผ 24์๊ฐ) | |
end_date = datetime.utcnow() | |
start_date = end_date - timedelta(days=1) | |
params = { | |
'key': API_KEY, | |
'cx': SEARCH_ENGINE_ID, | |
'q': f"{keyword} site:news.google.com", | |
'lr': 'lang_en', # ์์ด ๊ฒฐ๊ณผ๋ง | |
'sort': 'date:r:20230101:99999999', # ๋ ์ง์ ์ ๋ ฌ | |
'num': 100, # ์ต๋ 100๊ฐ ๊ฒฐ๊ณผ | |
'dateRestrict': 'd1', # ์ต๊ทผ 1์ผ ๋ด ๊ฒฐ๊ณผ๋ง | |
} | |
if country != 'All Countries': | |
params['gl'] = COUNTRIES[country] | |
try: | |
response = requests.get(url, params=params) | |
response.raise_for_status() | |
results = response.json() | |
debug_info = f"API Request URL: {response.url}\n" | |
debug_info += f"API Response Status: {response.status_code}\n" | |
debug_info += f"API Response Body: {json.dumps(results, indent=2)}\n" | |
formatted_results = "" | |
if 'items' in results: | |
for item in results['items']: | |
title = item['title'] | |
link = item['link'] | |
snippet = item.get('snippet', 'No snippet available') | |
formatted_results += f"<h3><a href='{link}' target='_blank'>{title}</a></h3>" | |
formatted_results += f"<p>{snippet}</p><br>" | |
formatted_results += f"<p>Total results: {len(results['items'])}</p>" | |
else: | |
formatted_results = f"No news found for '{keyword}' in {country} within the last 24 hours." | |
except requests.exceptions.HTTPError as e: | |
formatted_results = f"An error occurred: {str(e)}" | |
debug_info = f"Error: {str(e)}\n" | |
debug_info += f"API Response Status: {e.response.status_code}\n" | |
debug_info += f"API Response Body: {e.response.text}\n" | |
except Exception as e: | |
formatted_results = f"An unexpected error occurred: {str(e)}" | |
debug_info = f"Unexpected Error: {str(e)}\n" | |
formatted_results += f"<details><summary>Debug Info</summary><pre>{debug_info}</pre></details>" | |
return formatted_results | |
# Gradio ์ธํฐํ์ด์ค ์์ฑ | |
iface = gr.Interface( | |
fn=search_news, | |
inputs=[ | |
gr.Textbox(label="Enter keyword (in English)"), | |
gr.Dropdown(choices=['All Countries'] + list(COUNTRIES.keys()), label="Select Country") | |
], | |
outputs=gr.HTML(), | |
title="Google News Search", | |
description="Search for up to 100 news articles from the last 24 hours using Google Custom Search API." | |
) | |
# ์ ํ๋ฆฌ์ผ์ด์ ์คํ | |
iface.launch() |