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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# .envファイルの読み込み
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# 環境変数からAPIキーやURLを取得
|
| 11 |
+
API_KEY = os.getenv("API_KEY")
|
| 12 |
+
NEWS_API_URL = os.getenv("NEWS_API_URL")
|
| 13 |
+
|
| 14 |
+
def fetch_news(query="ecolab", sort_by="publishedAt", page_size=10):
|
| 15 |
+
"""
|
| 16 |
+
NewsAPIからニュース情報を取得する関数
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
headers = {
|
| 20 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 21 |
+
"User-Agent": "NewsApp/1.0"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
params = {
|
| 25 |
+
"q": query,
|
| 26 |
+
"sortBy": sort_by,
|
| 27 |
+
"pageSize": page_size,
|
| 28 |
+
"language": "en"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
response = requests.get(NEWS_API_URL, headers=headers, params=params)
|
| 32 |
+
|
| 33 |
+
if response.status_code == 200:
|
| 34 |
+
data = response.json()
|
| 35 |
+
|
| 36 |
+
if data["status"] == "ok" and data["totalResults"] > 0:
|
| 37 |
+
articles = data["articles"]
|
| 38 |
+
|
| 39 |
+
# データを整理
|
| 40 |
+
news_data = []
|
| 41 |
+
for article in articles:
|
| 42 |
+
news_data.append({
|
| 43 |
+
"タイトル": article.get("title", "N/A"),
|
| 44 |
+
"説明": article.get("description", "N/A"),
|
| 45 |
+
"ソース": article.get("source", {}).get("name", "N/A"),
|
| 46 |
+
"公開日": article.get("publishedAt", "N/A"),
|
| 47 |
+
"URL": article.get("url", "N/A")
|
| 48 |
+
})
|
| 49 |
+
|
| 50 |
+
# DataFrameに変換
|
| 51 |
+
df = pd.DataFrame(news_data)
|
| 52 |
+
|
| 53 |
+
return df, f"✅ {len(articles)}件のニュースを取得しました"
|
| 54 |
+
else:
|
| 55 |
+
return pd.DataFrame(), "❌ ニュースが見つかりませんでした"
|
| 56 |
+
|
| 57 |
+
else:
|
| 58 |
+
return pd.DataFrame(), f"❌ APIエラー: {response.status_code} - {response.text}"
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return pd.DataFrame(), f"❌ エラーが発生しました: {str(e)}"
|
| 62 |
+
|
| 63 |
+
def search_news(query: str, sort_by: str, page_size: int):
|
| 64 |
+
"""
|
| 65 |
+
Main function for searching news.
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
query (str): The search keyword for news articles.
|
| 69 |
+
sort_by (str): The sorting method for the news results.
|
| 70 |
+
Options are "publishedAt", "relevancy", or "popularity".
|
| 71 |
+
page_size (int): The number of news articles to retrieve.
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
tuple:
|
| 75 |
+
- pd.DataFrame: A DataFrame containing the news articles.
|
| 76 |
+
Columns include "タイトル", "説明", "ソース", "公開日", and "URL".
|
| 77 |
+
- str: A status message indicating the result of the search.
|
| 78 |
+
"""
|
| 79 |
+
if not query.strip():
|
| 80 |
+
return pd.DataFrame(), "❌ 検索キーワードを入力してください"
|
| 81 |
+
|
| 82 |
+
df, message = fetch_news(query, sort_by, page_size)
|
| 83 |
+
return df, message
|
| 84 |
+
|
| 85 |
+
# Gradioインターフェース
|
| 86 |
+
with gr.Blocks(title="📰 News API アプリ", theme=gr.themes.Soft()) as demo:
|
| 87 |
+
gr.Markdown(
|
| 88 |
+
"""
|
| 89 |
+
# 📰 News API ニュース検索アプリ
|
| 90 |
+
|
| 91 |
+
NewsAPIを使用してリアルタイムのニュース情報を検索・取得できます。
|
| 92 |
+
|
| 93 |
+
**使い方:**
|
| 94 |
+
1. 検索キーワードを入力
|
| 95 |
+
2. ソート方法を選択
|
| 96 |
+
3. 取得件数を選択
|
| 97 |
+
4. 「ニュース検索」ボタンをクリック
|
| 98 |
+
"""
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
with gr.Column():
|
| 103 |
+
# 入力コンポーネント
|
| 104 |
+
query_input = gr.Textbox(
|
| 105 |
+
label="🔍 検索キーワード",
|
| 106 |
+
placeholder="例: ecolab, technology, business...",
|
| 107 |
+
value="ecolab"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
sort_by_dropdown = gr.Dropdown(
|
| 111 |
+
choices=["publishedAt", "relevancy", "popularity"],
|
| 112 |
+
label="📊 ソート方法",
|
| 113 |
+
value="publishedAt"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
page_size_slider = gr.Slider(
|
| 117 |
+
minimum=5,
|
| 118 |
+
maximum=50,
|
| 119 |
+
value=10,
|
| 120 |
+
step=5,
|
| 121 |
+
label="📄 取得件数"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
search_btn = gr.Button("🔍 ニュース検索", variant="primary")
|
| 125 |
+
|
| 126 |
+
with gr.Column():
|
| 127 |
+
# 出力コンポーネント
|
| 128 |
+
status_output = gr.Textbox(
|
| 129 |
+
label="📋 ステータス",
|
| 130 |
+
interactive=False
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# 結果表示
|
| 134 |
+
results_df = gr.Dataframe(
|
| 135 |
+
label="📰 ニュース結果",
|
| 136 |
+
wrap=True,
|
| 137 |
+
row_count=30,
|
| 138 |
+
interactive=False
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# イベントハンドラー
|
| 142 |
+
search_btn.click(
|
| 143 |
+
fn=search_news,
|
| 144 |
+
inputs=[query_input, sort_by_dropdown, page_size_slider],
|
| 145 |
+
outputs=[results_df, status_output],
|
| 146 |
+
show_api=True,
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# Enter キーでも検索可能
|
| 150 |
+
query_input.submit(
|
| 151 |
+
fn=search_news,
|
| 152 |
+
inputs=[query_input, sort_by_dropdown, page_size_slider],
|
| 153 |
+
outputs=[results_df, status_output],
|
| 154 |
+
show_api=False,
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# 初期ロード時にデフォルト検索を実行
|
| 158 |
+
demo.load(
|
| 159 |
+
fn=lambda: search_news("ecolab", "publishedAt", 10),
|
| 160 |
+
outputs=[results_df, status_output],
|
| 161 |
+
show_api=False,
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# アプリ起動
|
| 165 |
+
if __name__ == "__main__":
|
| 166 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio[mcp]
|
| 2 |
+
requests
|
| 3 |
+
pandas
|