-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunlikeTweets.js
49 lines (44 loc) · 1.7 KB
/
unlikeTweets.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
require('dotenv').config();
const { TwitterApi } = require('twitter-api-v2');
const prompt = require('prompt-sync')();
const { allEnvKeysPresent } = require('./lib/common');
if (! allEnvKeysPresent()) {
process.exit(1);
}
const main = async function() {
const client = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_KEY,
appSecret: process.env.TWITTER_CONSUMER_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN,
accessSecret: process.env.TWITTER_ACCESS_SECRET
});
const user = await client.currentUserV2();
const promptMessage = `Are you sure you want to unlike all tweets for @${user.data.username}`;
const response = prompt(`${promptMessage}? [y/N]: `).toLowerCase();
if (response !== 'y') {
console.log('Aborting');
process.exit(0);
}
const likedTweetsPaginator = await client.v2.userLikedTweets(user.data.id);
for await (const tweet of likedTweetsPaginator) {
console.log(`Unliking tweet ${tweet.id}: ${tweet.text}`);
let success = false;
do {
try {
await client.v2.unlike(user.data.id, tweet.id);
success = true;
} catch (err) {
if (err.code === 429) {
const sleepTimeMs = parseInt(Math.ceil((err.rateLimit.reset - (Date.now() / 1000.0)) * 1000)) + 1000;
console.log(` - Rate limited; reset in ${sleepTimeMs / 1000} seconds - sleeping...`);
await new Promise(resolve => setTimeout(resolve, sleepTimeMs));
} else {
throw err;
}
}
} while (! success);
}
};
main().then(() => {
process.exit(0);
});