-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathget_reposted_by.js
More file actions
54 lines (44 loc) · 1.72 KB
/
get_reposted_by.js
File metadata and controls
54 lines (44 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Get Reposted By (Users who reposted) by Post ID
// https://developer.twitter.com/en/docs/twitter-api/tweets/retweets/api-reference/get-tweets-id-retweeted_by
const { Client, UserPaginator } = require('@xdevplatform/xdk');
// The code below sets the bearer token from your environment variables
// To set environment variables on macOS or Linux, run the export command below from the terminal:
// export BEARER_TOKEN='YOUR-TOKEN'
const bearerToken = process.env.BEARER_TOKEN;
const client = new Client({ bearerToken: bearerToken });
// You can replace the ID given with the Post ID you wish to lookup reposting users for
// You can find an ID by using the Post lookup endpoint
const postId = "1980412193624785337";
const getRepostedBy = async () => {
console.log("Retrieving users who reposted...");
// Use paginator for automatic pagination
const repostedBy = new UserPaginator(
async (token) => {
const res = await client.posts.getRepostedBy(postId, {
maxResults: 100,
paginationToken: token,
tweetFields: ['lang', 'author_id'],
userFields: ['created_at']
});
return {
data: res.data ?? [],
meta: res.meta,
includes: res.includes,
errors: res.errors
};
}
);
// Fetch all pages
await repostedBy.fetchNext();
while (!repostedBy.done) {
await repostedBy.fetchNext();
}
console.dir(repostedBy.users, {
depth: null
});
console.log(`Got ${repostedBy.users.length} users who reposted Post ID ${postId}!`);
}
getRepostedBy().catch(err => {
console.error('Error:', err);
process.exit(-1);
});