Spaces:
Building
Building
import gradio as gr | |
import requests | |
from datetime import datetime, timedelta | |
import pycountry | |
import json | |
# NewsAPI key | |
API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6" | |
COUNTRY_CODES = "ae ar at au be bg br ca ch cn co cu cz de eg fr gb gr hk hu id ie il in it jp kr lt lv ma mx my ng nl no nz ph pl pt ro rs ru sa se sg si sk th tr tw ua us ve za".split() | |
COUNTRIES = {'all': 'All Countries'} | |
COUNTRIES.update({code: pycountry.countries.get(alpha_2=code.upper()).name for code in COUNTRY_CODES if pycountry.countries.get(alpha_2=code.upper())}) | |
def get_related_keywords(keyword): | |
# ๊ฐ๋จํ ๊ด๋ จ ํค์๋ ์์ฑ ๋ก์ง (์ค์ ๋ก๋ ๋ ๋ณต์กํ ์๊ณ ๋ฆฌ์ฆ์ด๋ API๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค) | |
return [f"{keyword} news", f"{keyword} technology", f"{keyword} company", f"latest {keyword}"] | |
def get_news(keyword, article_count, country, extend_search=False, search_all_countries=False): | |
if search_all_countries: | |
country = 'All Countries' | |
country_code = next((code for code, name in COUNTRIES.items() if name == country), 'all') | |
if country_code == 'all': | |
base_url = "https://newsapi.org/v2/everything" | |
params = { | |
'apiKey': API_KEY, | |
'q': keyword, | |
'language': 'en', | |
'sortBy': 'publishedAt', | |
'pageSize': article_count | |
} | |
else: | |
base_url = "https://newsapi.org/v2/top-headlines" | |
params = { | |
'apiKey': API_KEY, | |
'q': keyword, | |
'country': country_code, | |
'pageSize': article_count | |
} | |
if not extend_search: | |
two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat() | |
params['from'] = two_days_ago | |
debug_info = f"API Request URL: {base_url}\n" | |
debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n" | |
try: | |
response = requests.get(base_url, params=params, timeout=10) | |
response.raise_for_status() | |
news_data = response.json() | |
debug_info += f"API Response Status: {response.status_code}\n" | |
debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n\n" | |
debug_info += f"API Response Body: {json.dumps(news_data, indent=2)}\n\n" | |
except requests.RequestException as e: | |
return f"<p style='color: red;'>Error fetching news: {str(e)}</p><pre>{debug_info}</pre>" | |
if news_data['status'] != 'ok': | |
return f"<p style='color: red;'>API Error: {news_data.get('message', 'Unknown error occurred')}</p><pre>{debug_info}</pre>" | |
articles = news_data['articles'] | |
if not articles: | |
if not extend_search: | |
return get_news(keyword, article_count, country, extend_search=True) | |
elif not search_all_countries and country != 'All Countries': | |
return get_news(keyword, article_count, 'All Countries', extend_search=True, search_all_countries=True) | |
else: | |
related_keywords = get_related_keywords(keyword) | |
suggestions = (f"<p>No news found for the keyword '<strong>{keyword}</strong>' in {country}.</p>" | |
f"<p>Suggestions:</p>" | |
f"<ul>" | |
f"<li>Try one of these related keywords: {', '.join(related_keywords)}</li>" | |
f"<li>Increase the number of articles</li>" | |
f"<li>Check for any spelling errors in your keyword</li>" | |
f"</ul>") | |
return suggestions + f"<pre>{debug_info}</pre>" | |
html_output = f"<h2>News results for '{keyword}' in {country}</h2>" | |
if extend_search: | |
html_output += "<p><em>Note: Search range was extended due to limited recent results.</em></p>" | |
if search_all_countries: | |
html_output += "<p><em>Note: Search was expanded to all countries due to limited results in the specified country.</em></p>" | |
for article in articles: | |
title = article['title'] | |
link = article['url'] | |
pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ") | |
source = article.get('source', {}).get('name', 'Unknown Source') | |
description = article.get('description', 'No description available') | |
html_output += f""" | |
<div style='margin-bottom: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;'> | |
<h3><a href='{link}' target='_blank' style='text-decoration: none; color: #1a0dab;'>{title}</a></h3> | |
<p style='color: #006621;'>{source}</p> | |
<p style='color: #545454;'>{pub_date.strftime('%Y-%m-%d %H:%M:%S')}</p> | |
<p>{description}</p> | |
</div> | |
""" | |
html_output += f"<details><summary>Debug Info</summary><pre>{debug_info}</pre></details>" | |
return html_output | |
iface = gr.Interface( | |
fn=get_news, | |
inputs=[ | |
gr.Textbox(label="Enter keyword"), | |
gr.Slider(minimum=10, maximum=100, step=10, value=10, label="Number of Articles"), | |
gr.Dropdown(choices=list(COUNTRIES.values()), value="All Countries", label="Select Country") | |
], | |
outputs=gr.HTML(), | |
title="Advanced Visual News Search", | |
description="Search for news articles using NewsAPI. The search will automatically extend if results are limited.", | |
theme=gr.themes.Soft() | |
) | |
iface.launch() |