News Sentiment API について
News Sentiment API は、トピックに関するニュースの論調を時系列と成長率で提供します。メディアのセンチメント変化を追う開発者や分析担当者向けで、Python クライアントと HTTP から利用できます。
主な機能
- 01ニュースセンチメントの時系列
- 02期間指定による成長率
- 03Python クライアントと非同期クライアント
- 04Pandas でのデータ処理
活用シーン
- 01企業に関するニュース論調の追跡
- 02期間ごとのセンチメント変化の比較
設定
リモートサーバーはインストール不要です。クライアントを選んで設定をコピーするか、ワンクリックで導入できます。
https://news-sentiment.api.trendsapi.ai/mcpclaude mcp add --transport http ai-trendsapi-news-sentiment https://news-sentiment.api.trendsapi.ai/mcp{"mcpServers":{"ai-trendsapi-news-sentiment":{"type":"http","url":"https://news-sentiment.api.trendsapi.ai/mcp"}}}Cursor で開く{"servers":{"ai-trendsapi-news-sentiment":{"type":"http","url":"https://news-sentiment.api.trendsapi.ai/mcp"}}}VS Code で開く{"mcpServers":{"ai-trendsapi-news-sentiment":{"type":"http","url":"https://news-sentiment.api.trendsapi.ai/mcp"}}}{"mcpServers":{"ai-trendsapi-news-sentiment":{"serverUrl":"https://news-sentiment.api.trendsapi.ai/mcp"}}}コマンドはターミナルで実行し、JSON は各クライアントの MCP 設定に貼り付けて再起動します。認可が必要なサーバーは初回接続時にログインを求めます。
https://news-sentiment.api.trendsapi.ai/mcp認証が必要ツール
詳細
ニュース感情 API
Trends API を使って、ニュースのトーンの推移を確認できます。メディアをスクレイピングせずに、感情の履歴と成長率を取得できます。
API キー:trendsapi.ai/#get-key。HTTP の仕様とすべてのデータソースについては、trendsapi-ai/trendsapi を参照してください。
認証
pip install trendsapi-news-sentiment
export TRENDSAPI_KEY=your_key
Python 3.9 以降。同じキーを HTTP API と共用します。
from trendsapi_news_sentiment import TrendsAPI
client = TrendsAPI() # TRENDSAPI_KEY
# client = TrendsAPI(api_key="YOUR_KEY")
キーワード用ヘルパーは、デフォルトで source: "news sentiment" を使用します。同じクライアントで別のプラットフォームを利用するには、source= を渡してください。公式のフルクライアント(すべてのデータソースに対応、プリセットなし):trendsapi。
メソッド
| メソッド | REST mode | 戻り値 |
|---|---|---|
get_time_series(keyword, source=, data_mode=) | get_time_series | list[TrendsDataPoint] |
get_growth(keyword, percent_growth=, source=, data_mode=) | get_growth | GetGrowthResponse |
get_top_trends(type=, ...) | get_top_trends | GetTopTrendsResponse |
source は小文字(news sentiment)で指定します。type は正確に指定します(Google Trends)。これらを混在させると 400 エラーになります。
from trendsapi_news_sentiment import TrendsAPI
client = TrendsAPI() # TRENDSAPI_KEY
# client = TrendsAPI(api_key="YOUR_KEY")
series = client.get_time_series("nvidia")
print(series[-1].date, series[-1].value)
growth = client.get_growth("nvidia", percent_growth=["3M", "12M"])
print(growth.results[0].growth, growth.results[0].direction)
get_time_series
points = client.get_time_series("nvidia")
各データポイント:
| フィールド | 常に | 意味 |
|---|---|---|
date | はい | YYYY-MM-DD |
value | はい | この系列の 0〜100 のインデックス |
keyword | はい | 入力値のエコー |
volume | いいえ | 利用可能な場合の絶対ボリューム |
source or datatype | いいえ | パイプラインのラベル |
Python では list[TrendsDataPoint] が返されます。["date"] ではなく .date と .value を使用してください。
JS では同じフィールドがオブジェクトのプロパティとして返されます。
get_growth
g = client.get_growth("nvidia", percent_growth=["12M", "3M", "YTD"])
print(g.results[0].growth, g.results[0].direction)
percent_growth のデフォルト値:["12M"]。プリセット:7D 14D 30D 1M 2M 3M 6M 9M 12M/1Y 18M 24M/2Y 36M/3Y 48M 60M/5Y MTD QTD YTD。カスタム指定:{"name": "Launch", "recent": "2024-06-01", "baseline": "2024-01-01"}。
| フィールド | 意味 |
|---|---|
search_term | キーワード |
data_source | データソース |
results | 期間ごとに 1 つのオブジェクト(period、growth、direction、日付、値) |
metadata | 件数/成功フラグ |
複数の期間を指定しても、リクエストは 1 回としてカウントされます。Python:growth.results[0].growth。JS:growth.results[0].growth。
非同期
import asyncio
from trendsapi_news_sentiment import AsyncTrendsAPI
async def main():
c = AsyncTrendsAPI()
return await asyncio.gather(
c.get_time_series("nvidia"),
c.get_time_series("nvidia", source="google search"),
)
asyncio.run(main())
200 の応答ごとに、課金対象のリクエストが 1 回としてカウントされます。
Pandas
from dataclasses import asdict
import pandas as pd
from trendsapi_news_sentiment import TrendsAPI
df = pd.DataFrame(asdict(p) for p in TrendsAPI().get_time_series("nvidia"))
df["date"] = pd.to_datetime(df["date"])
print(df.set_index("date")["value"].resample("ME").mean().tail())
呼び出し(curl)
| フィールド | 値 |
|---|---|
| エンドポイント | POST https://api.trendsapi.ai/api |
| 認証 | Authorization: Bearer $TRENDSAPI_KEY |
| 履歴 | get_time_series または get_growth で source: news sentiment を指定 |
| キーワード | 任意の語句(例:nvidia) |
ライブ type | 該当なし |
curl -sS -X POST https://api.trendsapi.ai/api \
-H "Authorization: Bearer $TRENDSAPI_KEY" \
-H "Content-Type: application/json" \
--max-time 60 \
-d '{"mode":"get_time_series","source":"news sentiment","keyword":"nvidia"}'
データソースに関する注意
growthが負の値の場合、前の期間に比べてトーンが低下したことを示します。企業が否定的であるという意味ではありません。- 言及数には
news-trends-api(source: news volume)を使用します。 - 記事ごとのラベルやメディア別の内訳はありません。
エラー
| HTTP | クライアント |
|---|---|
| 200 | ペイロードを解析します。Python はデータクラス、JS は型付きオブジェクトです |
| 400 | 例外が発生します。source または type の表記を修正してください |
| 401 | 例外が発生します。TRENDSAPI_KEY を確認してください |
| 404 | 例外が発生します。そのキーワードの系列はありません。再試行しないでください |
| 429 | 例外が発生します。クォータ超過です |
| 5xx | クライアントが再試行した後、例外が発生します |
HTTP の body フィールドは JSON 文字列です。SDK はデコードします。生の curl では body をもう一度解析する必要があります。
サイト:https://trendsapi.ai/trends/news-sentiment。
ライセンス
MIT。LICENSE を参照してください。
コメント
AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。