forked from kakomarina/twitterBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
104 lines (91 loc) · 2.85 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
console.log('Ola!');
var Twit = require('twit');
var fs = require('fs');
var config = require('./config');
var T = new Twit(config);
setInterval(retweetRecycle, 1000*60*60*24);
setInterval(tweetIt, 1000*60);
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
// find latest tweet according the query 'q' in params
function retweetRecycle() {
var params = {
q: '#recycle', // REQUIRED
result_type: 'popular',
lan: 'en',
count: 5
}
T.get('search/tweets', params, function(err, data) {
if(data.statuses[0] === undefined) console.log('Nenhum tweet encontrado');
// if there no errors
else if (!err) {
for(var i = 0; i < 5; i++){
// grab ID of tweet to retweet
var retweetId = data.statuses[i].id_str;
// Tell TWITTER to retweet
T.post('statuses/retweet/:id', {
id: retweetId
}, function(err, response) {
if (err) {
console.log('Hmmm, tweet duplicado?');
}
else if (response) {
console.log('Retweeted!!!');
}
});
sleep(1000*60*5);
}
}
// if unable to Search a tweet
else {
console.log('Algo deu errado com a busca');
}
});
}
var tweetCount = 0;
function tweetIt(){
fs.readFile('tweets.json', (err, data) => {
if (err)
console.log(err);
else {
var json = JSON.parse(data);
var tweet = {
status: json.status[tweetCount%5],
}
console.log(tweet.status);
T.post('statuses/update', tweet, tweeted);
function tweeted(err, data, response){
if(!err) console.log("Funcionou!");
else console.log("Nao deu certo");
tweetCount++;
}
}
})
}
//testing how to tweet media, still incoporating it to the bot
function tweetMedia(){
var image = fs.readFileSync('meme.jpg', { encoding: 'base64' })
// posting the media to twitter
T.post('media/upload', { media_data: image }, function (err, data, response) {
// assingning alttext to media (accessability)
var mediaIdStr = data.media_id_string;
var altText = "Me explaining to my mom meme";
var meta_params = { media_id: mediaIdStr, alt_text: { text: altText } };
T.post('media/metadata/create', meta_params, function (err, data, response) {
if (!err) {
// reference the media and post a tweet
var params = { status: 'me explaining to my friends that reusing is important', media_ids: [mediaIdStr] }
T.post('statuses/update', params, function (err, data, response) {
if(!err) console.log("Funcionou");
else console.log("Nao deu certo");
})
}
})
})
}