-
Notifications
You must be signed in to change notification settings - Fork 4
/
check.js
123 lines (106 loc) · 3.9 KB
/
check.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const settings = require("./settings.json");
const https = require('https');
https.globalAgent.maxSockets = settings.general.maxSockets;
const async = require('async');
const moment = require('moment');
const utils = require('./utils');
const db = require('./models/db');
const Tweet = require('./models/tweet');
const Handle = require('./models/handle');
let tty = process.stdout.isTTY ? true : false;
const charm = require('charm')();
if (tty) {
charm.pipe(process.stdout);
charm.cursor(false);
}
if (settings.general.rate === 0 || settings.general.timeout === 0) {
if (tty) {
console.log("Please run benchmarks or manually edit the rate/timeout settings before running the checker.");
} else {
process.stdout.write(JSON.stringify({text: "Please run benchmarks or manually edit the rate/timeout settings before running the checker."}));
}
process.exit();
}
let total = 0, fails = 0, startTotal = 0, totalDeletedTweets = 0;
let start = Date.now();
let rate;
var lastSuccess = Date.now();
var breaks = 0;
let q = async.queue((tweet, cb) => {
utils.tweetExists(tweet.handle, tweet.tweetid, exists => {
if (exists === "fail") {
q.push(tweet);
fails++;
} else {
lastSuccess = Date.now();
}
rate = total / ((Date.now() - start)/1000);
let raw = moment.duration((startTotal+fails - total)/rate*1000);
let format = `${utils.pad(raw.minutes(), 2)}:${utils.pad(raw.seconds(), 2)}`;
++total;
if (total % 25 === 0) {
if (tty) {
charm.left(255);
charm.erase('line');
charm.write(`${total} / ${startTotal}+${fails} | ${startTotal+fails-total} | ${Math.floor((total/startTotal)*100)} | ${Math.floor(rate)} tweets/sec | eta: ${format} | ${tweet.handle} | https://twitter.com/${tweet.handle}/status/${tweet.tweetid}`)
} else {
process.stdout.write(JSON.stringify({status: `${total} / ${startTotal}+${fails}`, remaining: startTotal+fails-total, percent: Math.floor((total/startTotal)*100), rate: Math.floor(rate), eta: format, user: tweet.handle, url: `https://twitter.com/${tweet.handle}/status/${tweet.tweetid}`}));
}
}
if (!exists) {
Tweet.getCond({tweetid: tweet.tweetid}).then(lookup => {
if (lookup.checking !== 1) {
totalDeletedTweets++;
Handle.incVal('deleted', 1, tweet.handle).then(() => {
charm.left(255);
charm.erase('line');
charm.write(`${total} / ${startTotal}+${fails} | ${startTotal+fails-total} | ${Math.floor(rate)} tweets/sec | eta: ${format} | ${tweet.handle} | https://twitter.com/${tweet.handle}/status/${tweet.tweetid}`)
charm.write(`\n\t${tweet.content}\n\n`);
Tweet.deleted(tweet.id).then(() => {
cb();
});
});
// Tweet is currently being snapshotted so push it back into the queue and try again later.
} else {
q.push(tweet);
cb();
}
});
} else {
cb();
}
});
}, settings.general.rate);
q.drain = () => {
if (tty) {
charm.down(1);
charm.cursor(true);
console.log(`\n${totalDeletedTweets} tweets deleted`);
} else {
process.stdout.write(JSON.stringify({text: `${totalDeletedTweets} tweets deleted`}));
}
process.exit(0);
};
setInterval(() => {
// If more than 10 seconds have passed since last success
if (Date.now() - lastSuccess > 10000) {
// More than 3 breaks
if (++breaks === 3) {
console.log("Script quitting...too many failures kept occuring. You might want to adjust some values in your settings");
process.exit();
// Take a 5 second break
} else {
lastSuccess = Date.now() + 5000;
q.pause();
setTimeout(() => {
q.resume();
}, 5000);
}
}
}, 1000)
db.init(() => {
Tweet.getAllAvailable((process.argv[2] || settings.general.checkerDaysBack)).then(tweets => {
startTotal = tweets.length;
q.push(tweets);
});
});