ainews-db / app.py
seawolf2357's picture
Update app.py
77e4df8 verified
raw
history blame
2.57 kB
import gradio as gr
import requests
from datetime import datetime, timedelta
# Google Custom Search API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID
API_KEY = "AIzaSyB8wNdEL8-SAvelRq-zenLLU-cUEmsj7uE"
SEARCH_ENGINE_ID = "362030334819-vd4cpdvm1mecdbbu2ue41s3ldlc41365.apps.googleusercontent.com"
# ์ง€์›๋˜๋Š” ๊ตญ๊ฐ€ ๋ฆฌ์ŠคํŠธ
COUNTRIES = {
'United States': 'countryUS', 'United Kingdom': 'countryGB', 'India': 'countryIN',
'Australia': 'countryAU', 'Canada': 'countryCA', 'Germany': 'countryDE',
'France': 'countryFR', 'Italy': 'countryIT', 'Spain': 'countryES', 'Brazil': 'countryBR',
'Mexico': 'countryMX', 'Argentina': 'countryAR', 'Japan': 'countryJP',
'South Korea': 'countryKR', 'Russia': 'countryRU', 'China': 'countryCN',
'Netherlands': 'countryNL', 'Sweden': 'countrySE', 'Poland': 'countryPL', 'Turkey': 'countryTR'
}
def search_news(keyword, country):
# 24์‹œ๊ฐ„ ์ „ ๋‚ ์งœ ๊ณ„์‚ฐ
one_day_ago = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
# API ์š”์ฒญ URL ๋ฐ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •
url = "https://www.googleapis.com/customsearch/v1"
params = {
'key': API_KEY,
'cx': SEARCH_ENGINE_ID,
'q': keyword,
'dateRestrict': 'd1', # ์ตœ๊ทผ 1์ผ ๋‚ด ๊ฒฐ๊ณผ๋งŒ
'lr': 'lang_en', # ์˜์–ด ๊ฒฐ๊ณผ๋งŒ
'sort': 'date', # ๋‚ ์งœ์ˆœ ์ •๋ ฌ
'num': 10, # ์ตœ๋Œ€ 10๊ฐœ ๊ฒฐ๊ณผ
'siteSearch': 'news.google.com', # Google News๋กœ ์ œํ•œ
}
if country != 'All Countries':
params['cr'] = COUNTRIES[country]
# API ์š”์ฒญ
response = requests.get(url, params=params)
results = response.json()
# ๊ฒฐ๊ณผ ํฌ๋งทํŒ…
formatted_results = ""
if 'items' in results:
for item in results['items']:
title = item['title']
link = item['link']
snippet = item['snippet']
formatted_results += f"<h3><a href='{link}' target='_blank'>{title}</a></h3>"
formatted_results += f"<p>{snippet}</p><br>"
else:
formatted_results = f"No news found for '{keyword}' in {country} within the last 24 hours."
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 news articles from the last 24 hours using Google Custom Search API."
)
# ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
iface.launch()