-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_tweets.js
81 lines (74 loc) · 1.91 KB
/
get_tweets.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const Twitter = require("twitter");
const fs = require("fs");
require("dotenv").config();
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
const urlRegex = new RegExp(
"([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?"
);
const SAMPLE_COUNT = Number(process.argv[3]);
let allTweets = [];
let oldest = null;
let params = {
screen_name: process.argv[2],
exclude_replies: true,
tweet_mode: "extended",
count: 200,
include_rts: false,
};
const getTweets = async () => {
if (oldest !== null) {
params.max_id = oldest;
}
return new Promise((res, rej) => {
client.get("statuses/user_timeline", params, (error, tweets, response) => {
if (error) {
console.log(error);
res(false);
}
console.log(`Grabbing tweets before ${oldest}`);
if (allTweets.length >= SAMPLE_COUNT) {
res(false);
}
for (tweet of tweets) {
const text = tweet.full_text.replace(/(\r\n|\n|\r|&)/gm, " ");
if (!urlRegex.test(text)) {
allTweets.push(text);
}
}
if (tweets.length > 0) {
oldest = tweets[tweets.length - 1].id - 1;
}
console.log(`${allTweets.length} tweets downloaded so far`);
res(true);
});
});
};
const getAllTweets = async () => {
while (true) {
const result = await getTweets();
if (result === false) {
writeToFile();
break;
}
}
};
getAllTweets();
const writeToFile = () => {
fs.writeFile(
"./tweets.json",
JSON.stringify({ tweetList: allTweets }),
(err) => {
if (err) {
console.error(err);
return;
} else {
console.log("Wrote tweets to JSON");
}
}
);
};