-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
133 lines (115 loc) · 3.72 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
console.log('Ola!');
require('dotenv').config()
var Twit = require('twit');
var fs = require('fs');
var parser = require('./utils.js');
var config = {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
timeout_ms: 60*1000, //optional HTTP request timeout to apply to all requests.
}
var T = new Twit(config);
tweetLyrics();
setInterval(tweetLyrics, 60*60*1000);
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 retweet() {
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('No tweet found');
// 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('Error retweeting: ' + err);
}
else if (response) {
console.log('Retweeted!');
}
});
sleep(1000*60*5);
}
}
// if unable to Search a tweet
else {
console.log('Something went wrong with the search: ' + err);
}
});
}
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("Tweet posted");
else console.log("Something went wrong: " + err);
tweetCount++;
}
}
})
}
async function tweetLyrics() {
// choosing random lyric file
filename = 'lyrics2/lyrics' + Math.floor((Math.random() * 100000) % 504 + 1)
const lyrics = await parser.parseLyrics(filename);
// random lyric verse
var rand = (Math.random() * 1000) % lyrics.length;
var tweet = {
status: lyrics[Math.floor(rand)]
}
console.log(tweet.status)
T.post('statuses/update', tweet, tweeted);
function tweeted(err, data, response) {
if(!err) console.log("Tweet posted");
else console.log("Something went wrong: " + err);
}
}
//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("Tweet posted");
else console.log("Something went wrong: " + err);
})
}
})
})
}