フルアーカイブのポスト数を取得するには、Self-serve または Enterprise へのアクセスが必要です。
全アーカイブの投稿数を取得する
1
クエリを作成する
全アーカイブ検索と同じクエリ構文を使用します。たとえば、@XDevelopers からの投稿数を取得するには次のようにします:
from:XDevelopers
2
時間範囲を設定する
特定の過去期間を検索するために
start_time と end_time を指定します:| パラメータ | 形式 | 例 |
|---|---|---|
start_time | ISO 8601 | 2020-01-01T00:00:00Z |
end_time | ISO 8601 | 2020-12-31T23:59:59Z |
3
リクエストを送信する
cURL
curl "https://api.x.com/2/tweets/counts/all?\
query=from%3AXDevelopers&\
start_time=2020-01-01T00%3A00%3A00Z&\
end_time=2020-12-31T23%3A59%3A59Z&\
granularity=day" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# 全アーカイブの投稿数を取得
response = client.posts.count_all(
query="from:XDevelopers",
start_time="2020-01-01T00:00:00Z",
end_time="2020-12-31T23:59:59Z",
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" });
// Get full-archive Post counts
const response = await client.posts.countAll({
query: "from:XDevelopers",
startTime: "2020-01-01T00:00:00Z",
endTime: "2020-12-31T23:59:59Z",
granularity: "day",
});
response.data?.forEach((bucket) => {
console.log(`${bucket.start}: ${bucket.tweet_count} Posts`);
});
console.log(`Total: ${response.meta?.total_tweet_count}`);
4
レスポンスを確認する
{
"data": [
{
"end": "2020-01-02T00:00:00.000Z",
"start": "2020-01-01T00:00:00.000Z",
"tweet_count": 3
},
{
"end": "2020-01-03T00:00:00.000Z",
"start": "2020-01-02T00:00:00.000Z",
"tweet_count": 5
}
],
"meta": {
"total_tweet_count": 8
}
}
粒度オプション
| 粒度 | 説明 |
|---|---|
minute | 分ごとのカウント |
hour | 時間ごとのカウント (デフォルト) |
day | 日ごとのカウント |
ページネーションで結果を取得する
next_token を使用します。
cURL
curl "https://api.x.com/2/tweets/counts/all?\
query=from%3AXDevelopers&\
start_time=2015-01-01T00%3A00%3A00Z&\
end_time=2020-12-31T23%3A59%3A59Z&\
granularity=day&\
next_token=abc123" \
-H "Authorization: Bearer $BEARER_TOKEN"
from xdk import Client
client = Client(bearer_token="YOUR_BEARER_TOKEN")
# ページネーションを使用して件数を取得
next_token = None
while True:
response = client.posts.count_all(
query="from:XDevelopers",
start_time="2015-01-01T00:00:00Z",
end_time="2020-12-31T23:59:59Z",
granularity="day",
next_token=next_token
)
for bucket in response.data:
print(f"{bucket.start}: {bucket.tweet_count}")
next_token = response.meta.next_token
if not next_token:
break
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });
// ページネーションを使用して件数を取得
let nextToken = undefined;
do {
const response = await client.posts.countAll({
query: "from:XDevelopers",
startTime: "2015-01-01T00:00:00Z",
endTime: "2020-12-31T23:59:59Z",
granularity: "day",
nextToken,
});
response.data?.forEach((bucket) => {
console.log(`${bucket.start}: ${bucket.tweet_count}`);
});
nextToken = response.meta?.next_token;
} while (nextToken);
最近の件数との主な違い
| 機能 | Recent Counts | Full-Archive Counts |
|---|---|---|
| 時間範囲 | 直近7日間 | 2006年3月から現在まで |
| 必要なアクセスレベル | すべての開発者 | 従量課金、エンタープライズ |
| デフォルトの時間範囲 | 直近7日間 | 直近30日間 |
共通パラメーター
| パラメーター | 説明 | デフォルト |
|---|---|---|
query | 検索クエリ (必須) | — |
granularity | 時間バケットの粒度 | hour |
start_time | 最も古いタイムスタンプ (ISO 8601) | 30日前 |
end_time | 最も新しいタイムスタンプ (ISO 8601) | 現在 |
next_token | ページネーション用トークン | — |
次のステップ
最近の件数
直近の投稿数を取得する
クエリを構築する
クエリ構文を使いこなす
APIリファレンス
エンドポイントの完全なドキュメント