-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathsearch_all.js
More file actions
50 lines (41 loc) · 1.5 KB
/
search_all.js
File metadata and controls
50 lines (41 loc) · 1.5 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
// Search for public posts across the whole Twitter archive
// https://developer.twitter.com/en/docs/twitter-api/tweets/search/quick-start/full-archive-search
const { Client, PostPaginator } = 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 });
const query = 'from:xdevelopers';
const searchAll = async () => {
console.log("Searching full archive...");
// Use paginator for automatic pagination
const searchResults = new PostPaginator(
async (token) => {
const res = await client.posts.searchAll(query, {
maxResults: 100,
nextToken: token,
tweetFields: ['author_id']
});
return {
data: res.data ?? [],
meta: res.meta,
includes: res.includes,
errors: res.errors
};
}
);
// Fetch all pages
await searchResults.fetchNext();
while (!searchResults.done) {
await searchResults.fetchNext();
}
console.dir(searchResults.posts, {
depth: null
});
console.log(`Got ${searchResults.posts.length} posts for query: ${query}`);
}
searchAll().catch(err => {
console.error('Error:', err);
process.exit(-1);
});