Web / app.py
nick5363's picture
Update app.py
19a5c1b verified
raw
history blame
2.07 kB
import requests
import feedparser
import gradio as gr
# API Key Marketaux
API_KEY = "A3WuGPFo3tqoY2CDWs1JP27K1nrtx8nWdi4053Jt"
# Hàm lấy tin từ Marketaux (3 bài)
def get_marketaux_news():
url = f"https://api.marketaux.com/v1/news/all?language=en&limit=3&api_token={API_KEY}"
try:
response = requests.get(url)
if response.status_code != 200:
return f"Lỗi API Marketaux: {response.status_code}\n{response.text}"
data = response.json()
news_list = data.get("data", [])
except Exception as e:
return f"Lỗi gọi Marketaux: {str(e)}"
result = "## [Marketaux] Tin Tài chính Real-time (3 bài)\n\n"
for i, article in enumerate(news_list):
title = article.get("title", "Không có tiêu đề")
link = article.get("url", "#")
source = article.get("source", "Unknown")
published = article.get("published_at", "")
result += f"**{i+1}. [{title}]({link})** \n<sub>{source}{published}</sub>\n\n"
return result
# Hàm lấy tin từ Google News RSS (Yahoo Finance)
def get_google_news():
feed_url = "https://news.google.com/rss/search?q=site:finance.yahoo.com&hl=en-US&gl=US&ceid=US:en"
feed = feedparser.parse(feed_url)
if not feed.entries:
return "Không có tin từ Google RSS."
result = "## [Google News] Tin tức Yahoo Finance (7 bài)\n\n"
for i, entry in enumerate(feed.entries[:7]):
title = entry.title
link = entry.link
published = entry.published
result += f"**{i+1}. [{title}]({link})** \n<sub>{published}</sub>\n\n"
return result
# Hàm kết hợp
def show_combined_news():
m_news = get_marketaux_news()
g_news = get_google_news()
return f"{m_news}\n\n---\n\n{g_news}"
# Giao diện
gr.Interface(
fn=show_combined_news,
inputs=None,
outputs="markdown",
title="Tin tức Tài chính Mới nhất (Marketaux + Yahoo RSS)",
description="Hiển thị 3 tin real-time từ Marketaux + 7 tin từ Yahoo Finance (Google RSS)."
).launch()