直近のポスト数を取得
1
クエリを作成する
recent search と同じクエリ構文を使用します。たとえば、@XDevelopers からのポスト数をカウントするには次のように指定します。
from:XDevelopers
2
リクエストを送信する
cURL
curl "https://api.x.com/2/tweets/counts/recent?\
query=from%3AXDevelopers&\
granularity=day" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# 直近のポスト数を取得
response = client.posts.count_recent(
query="from:XDevelopers",
granularity="day"
)
for bucket in response.data:
print(f"{bucket.start}: {bucket.tweet_count} Posts")
print(f"Total: {response.meta.total_tweet_count}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
// 直近のポスト数を取得
const response = await client.posts.countRecent({
query: "from:XDevelopers",
granularity: "day",
});
response.data?.forEach((bucket) => {
console.log(`${bucket.start}: ${bucket.tweet_count} Posts`);
});
console.log(`Total: ${response.meta?.total_tweet_count}`);
3
レスポンスを確認する
{
"data": [
{
"end": "2024-01-16T00:00:00.000Z",
"start": "2024-01-15T00:00:00.000Z",
"tweet_count": 5
},
{
"end": "2024-01-17T00:00:00.000Z",
"start": "2024-01-16T00:00:00.000Z",
"tweet_count": 3
},
{
"end": "2024-01-18T00:00:00.000Z",
"start": "2024-01-17T00:00:00.000Z",
"tweet_count": 8
}
],
"meta": {
"total_tweet_count": 16
}
}
粒度オプション
| Granularity | 説明 |
|---|---|
minute | 1分ごとのカウント数 |
hour | 1時間ごとのカウント数 (デフォルト) |
day | 1日ごとのカウント数 |
cURL
# 1時間ごとのカウント数を取得
curl "https://api.x.com/2/tweets/counts/recent?\
query=python%20lang%3Aen&\
granularity=hour" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# 1時間ごとのカウント数を取得
response = client.posts.count_recent(
query="python lang:en",
granularity="hour"
)
for bucket in response.data:
print(f"{bucket.start}: {bucket.tweet_count}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
// 1時間ごとのカウント数を取得
const response = await client.posts.countRecent({
query: "python lang:en",
granularity: "hour",
});
response.data?.forEach((bucket) => {
console.log(`${bucket.start}: ${bucket.tweet_count}`);
});
時間範囲でフィルターする
cURL
curl "https://api.x.com/2/tweets/counts/recent?\
query=from%3AXDevelopers&\
start_time=2024-01-10T00%3A00%3A00Z&\
end_time=2024-01-15T00%3A00%3A00Z&\
granularity=day" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# 特定の時間範囲の件数を取得します
response = client.posts.count_recent(
query="from:XDevelopers",
start_time="2024-01-10T00:00:00Z",
end_time="2024-01-15T00:00:00Z",
granularity="day"
)
print(f"Total Posts: {response.meta.total_tweet_count}")
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
// 特定の時間範囲の件数を取得します
const response = await client.posts.countRecent({
query: "from:XDevelopers",
startTime: "2024-01-10T00:00:00Z",
endTime: "2024-01-15T00:00:00Z",
granularity: "day",
});
console.log(`Total Posts: ${response.meta?.total_tweet_count}`);
共通パラメータ
| パラメータ | 説明 | デフォルト |
|---|---|---|
query | 検索クエリ (必須) | — |
granularity | 時間バケットの粒度 | hour |
start_time | 最古のタイムスタンプ (ISO 8601) | 7日前 |
end_time | 最新のタイムスタンプ (ISO 8601) | 現在 |
次のステップ
フルアーカイブのカウント
過去の投稿数を取得する
クエリを作成する
クエリ構文をマスターする
APIリファレンス
エンドポイントの詳細ドキュメント