-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (48 loc) · 1.6 KB
/
index.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
require('dotenv').config();
const Twitter = require('twitter');
const fs = require('fs');
const maxRequests = Number(process.env.MAX_REQUESTS);
const client = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN_KEY,
access_token_secret: process.env.ACCESS_TOKEN_SECRET
});
function getTweetsFromJSONFile() {
return JSON.parse(fs.readFileSync('tweets.json'));
}
function getTweetsFromTweetJs() {
let rawData = fs.readFileSync('tweet.js', { encoding: 'utf8' } );
let tweets = JSON.parse(rawData.split('window.YTD.tweet.part0 = ')[1]);
tweets.sort((a, b) => Number(a.tweet.id) - Number(b.tweet.id));
return tweets;
}
function saveJSONFileWith(tweets) {
fs.writeFileSync('tweets.json', JSON.stringify(tweets));
}
function execute() {
let tweets = []
try {
tweets = getTweetsFromJSONFile();
} catch(error) {
if(error.code === 'ENOENT') {
console.log(`JSON file not found. Will create a new JSON file from tweet.js.`);
tweets = getTweetsFromTweetJs();
saveJSONFileWith(tweets);
} else {
throw error;
}
}
const rest = tweets.filter((tweet, i) => {
if(i > maxRequests - 1) return true;
let error = null;
console.log(`${i}) Will try to delete tweet with id ${tweet.tweet.id}`);
client.post(`statuses/destroy/${tweet.tweet.id}.json`, err => error = err);
if(error) return true;
console.log(`${i}) Deleted tweet with id ${tweet.tweet.id}`);
return false;
});
console.log(`${rest.length} tweets left.`);
saveJSONFileWith(rest);
}
execute();