-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.js
316 lines (278 loc) · 8.89 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// =======================================================
// Overview
// -----------------------------------------------------
// Reddibot mirrors top image-based posts from various
// subreddits and tweets them.
// -----------------------------------------------------
// @author: Matthew Salcido
// @github: https://github.com/salcido
// @source: https://github.com/salcido/reddibot
// @bot-url: https://www.twitter.com/reddibot
// =======================================================
// ========================================================
// Module Dependencies
// ========================================================
require('dotenv').config();
const fetch = require('node-fetch');
const sharp = require('sharp');
const Twit = require('twit');
// ========================================================
// Assets / Utilities
// ========================================================
const { colors } = require('./assets/colors');
const { logo } = require('./assets/logo');
const { subreddits: { subs } } = require('./assets/subreddits');
const { utils: { alphabetize,
filterImages,
filterImgur,
filterJpgs,
filterPngs,
filterTexts,
generateImgurUrl,
generateShortLinks,
meta,
minutes,
sanitizeTitle,
timestamp
}} = require('./assets/utils');
// ========================================================
// Auth values
// ========================================================
const secret = {
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
};
// ========================================================
// Global vars
// ========================================================
const Twitter = new Twit(secret);
// Number of minutes between posts and updates;
const interval = minutes(30);
// Number of posts to return from each subreddit
const limit = 50;
// Bot's twitter handle for timeline data
const screenName = 'reddibot';
// Timezone offset (for logging fetches and tweets)
const utcOffset = -7;
// ========================================================
// Post queue and twitter timeline arrays
// ========================================================
let queue = [];
let timeline = [];
// ========================================================
// Functions (alphabetical)
// ========================================================
/**
* Converts raw buffer data to base64 string
* @param {string} buffer Raw image data
* @returns {string}
*/
function base64Encode(buffer) {
if ( buffer.byteLength > 5000000 ) {
return resize(Buffer.from(buffer, 'base64'));
}
return new Buffer(buffer).toString('base64');
}
/**
* Filters the posts for images and texts that meet
* the posting criteria
* @param {array.<object>} posts An array of various subreddit posts
* @returns {array}
*/
function filterPosts(posts) {
let images,
imgur,
jpgs,
pngs,
texts;
// Text only posts
texts = filterTexts(posts);
texts = meta(texts, 'text');
// Image-based posts
images = filterImages(posts);
images = meta(images, 'image');
// Gather up the image-based posts
pngs = filterPngs(images);
jpgs = filterJpgs(images);
imgur = generateImgurUrl(filterImgur(images));
// Update the queue with new posts
queue.push(...pngs, ...jpgs, ...imgur, ...texts);
return queue;
}
/**
* Gets the next post in the queue
* @returns {method}
*/
function getNextPost() {
if ( queue.length ) {
let post = queue.shift(),
title = post.data.title;
console.log(' ');
console.log(colors.reset, 'Attempting to post...');
console.log(title);
console.log('queue length: ', queue.length);
if ( !timeline.some(t => t.text.includes(title.substring(0, 25))) ) {
// Reset the queue after tweeting so that we're only tweeting
// the most upvoted, untweeted post every interval
queue = [];
return tweet(post);
}
console.log('Seen it. NEXT!!!');
return getNextPost();
}
return;
}
/**
* Gets the top posts from the subreddits
* and replaces any problematic characters
* in the posts' title
* @returns {method}
*/
function getPosts() {
let url = `https://www.reddit.com/r/${subs.join('+')}/top.json?limit=${limit}`;
// List subs in query
console.log(colors.yellow, 'Gathering new posts...');
return fetch(url, {cache: 'no-cache'})
.then(res => res.json())
.then(json => {
let posts = json.data.children;
posts.forEach(p => p.data.title = sanitizeTitle(p.data.title));
return posts;
})
.catch(err => console.log(colors.red, 'Error getPosts() ', err));
}
/**
* Gathers post data, mutates it, then
* gets Twitter timeline data
* @returns {promise}
*/
function getPostsAndTimeline() {
// Show logo on startup
printLogo();
// Grab our data
return new Promise((resolve, reject) => {
getPosts()
.then(filterPosts)
.then(posts => {
// Process our post data
queue = generateShortLinks(posts);
queue = queue.sort(alphabetize).reverse();
})
.then(getTimeline)
.then(resolve)
.catch(err => console.log(colors.red, 'Error getPostsAndTimeline() ', err));
});
}
/**
* Returns the 200 most recent tweets from the bot account
* @returns {array.<object>}
*/
function getTimeline() {
return new Promise((resolve, reject) => {
let params = { screen_name: screenName, count: 200 };
return Twitter.get('statuses/user_timeline', params, (err, data, res) => {
timeline = data;
return resolve();
});
});
}
/**
* Logs the `reddibot` logo in the output
* @returns {undefined}
*/
function printLogo() {
console.log(colors.cyan, `${logo}`);
console.log(colors.cyan, 'Next post: ', timestamp(utcOffset, interval));
}
/**
* Resizes an image to 1000px wide so that
* it will be under the 5mb limit Twitter requires
* @param {string} buffer Raw image data
* @returns {string}
*/
function resize(buffer) {
return sharp(buffer)
.resize(1000).toBuffer()
.then(data => new Buffer(data).toString('base64'))
.catch(err => console.log(colors.red, 'Error resize() ', err));
}
/**
* Tweets a post based on it's `meta` prop
* @param {object} post A single post from a subreddit
* @returns {method}
*/
function tweet(post) {
switch (post.data.meta) {
case 'text':
return tweetText(post);
case 'image':
return tweetImage(post);
}
}
/**
* Tweets an update to Twitter with an image
* @param {object} post A post from a subreddit
* @returns {undefined}
*/
function tweetImage(post) {
fetch(post.data.url)
.then(res => res.arrayBuffer())
.then(base64Encode)
.then(res => {
let title = post.data.title;
Twitter.post('media/upload', { media_data: res }, (err, data, res) => {
let mediaIdStr = data.media_id_string,
meta_params = {
media_id: mediaIdStr,
alt_text: { text: title }
};
Twitter.post('media/metadata/create', meta_params, (err, data, res) => {
if ( !err ) {
let params = {
status: `${title} \n${post.data.shorty} \n#${post.data.subreddit}`,
media_ids: [mediaIdStr]
};
Twitter.post('statuses/update', params, (err, data, res) => {
console.log(colors.green, 'Post successfully tweeted!');
console.log(colors.green, timestamp(utcOffset));
console.log(colors.cyan, 'Next post: ', timestamp(utcOffset, interval));
console.log(' ');
if ( data.errors ) console.log(colors.red, data);
});
} else {
console.log(' ');
console.log(colors.red, 'There was an error when attempting to post...');
console.error(err);
console.log(' ');
}
});
});
})
.catch(err => console.log(colors.red, 'Error tweet() ', err));
}
/**
* Tweets a text-only update to Twitter
* @param {object} post A post from a subreddit
* @returns {undefined}
*/
function tweetText(post) {
let title = post.data.title,
params = {
status: `${title} \n#${post.data.subreddit} \n${post.data.shorty}`
};
Twitter.post('statuses/update', params, (err, data, response) => {
console.log(colors.green, 'Post successfully tweeted!');
console.log(colors.green, timestamp(utcOffset));
console.log(colors.cyan, 'Next post: ', timestamp(utcOffset, interval));
console.log(' ');
if ( err ) console.log(colors.red, err);
if ( data.errors ) console.log(colors.red, data);
});
}
// ========================================================
// Init
// ========================================================
printLogo();
setInterval(() => getPostsAndTimeline().then(() => getNextPost()), interval);