최근 포스트 개수 가져오기
1
쿼리 작성
최근 검색과 동일한 쿼리 구문을 사용합니다. 예를 들어 @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} 포스트")
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} 포스트`);
});
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 | 분 단위 집계 |
hour | 시간 단위 집계(기본값) |
day | 일 단위 집계 |
cURL
# 시간 단위 집계 가져오기
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")
# 시간 단위 집계 가져오기
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" });
// 시간 단위 집계 가져오기
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}`);
공통 파라미터
| Parameter | Description | Default |
|---|---|---|
query | 검색 쿼리 (필수) | — |
granularity | 시간 구간 단위(버킷 크기) | hour |
start_time | 가장 오래된 타임스탬프 (ISO 8601) | 7일 전 |
end_time | 가장 최신 타임스탬프 (ISO 8601) | 현재 |
다음 단계
전체 아카이브 개수
과거 포스트 개수 가져오기
쿼리 만들기
쿼리 구문 완벽히 익히기
API 참조 문서
엔드포인트 전체 문서