-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
83 lines (68 loc) · 1.79 KB
/
bot.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
require("dotenv").config();
const fs = require("fs");
const path = require("path");
const twit = require("./twit");
const paramsPath = path.join(__dirname, "params.json");
function writeParams(data) {
console.log("We are writing the params file ...", data);
return fs.writeFileSync(paramsPath, JSON.stringify(data));
}
function readParams() {
console.log("We are reading the params file ...");
const data = fs.readFileSync(paramsPath);
return JSON.parse(data.toString());
}
function getTweets(since_id) {
return new Promise((resolve, reject) => {
let params = {
q: process.env.QUERY,
result_type: "mixed",
count: 10,
};
if (since_id) {
params.since_id = since_id;
}
console.log("We are getting the tweets ...", params);
twit.get("search/tweets", params, (err, data) => {
if (err) {
return reject(err);
}
return resolve(data);
});
});
}
function postRetweet(id) {
return new Promise((resolve, reject) => {
let params = {
id,
};
twit.post("statuses/retweet/:id", params, (err, data) => {
if (err) {
return reject(err);
}
return resolve(data);
});
});
}
async function main() {
try {
const params = readParams();
const data = await getTweets(params.since_id);
const tweets = data.statuses;
console.log("We got the tweets", tweets.length);
for await (let tweet of tweets) {
try {
await postRetweet(tweet.id_str);
console.log("Successful retweet " + tweet.id_str);
} catch (e) {
console.log("Unsuccessful retweet " + tweet.id_str);
}
params.since_id = tweet.id_str;
}
writeParams(params);
} catch (e) {
console.error(e);
}
}
console.log("Starting the twitter bot ...");
setInterval(main, 10000);