-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (89 loc) · 2.43 KB
/
index.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
require('dotenv').config();
const fetch = require('node-fetch');
const Twitter = require('twitter');
const client = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_SECRET
});
const arr = [];
async function getPosts() {
const url = `https://www.reddit.com/r/${process.env.SUBREDDITS}/rising/.json?limit=${process.env.LIMIT}`;
try {
const data = await fetch(url);
const posts = await data.json();
return posts.data.children;
} catch (err) {
console.log('Error fetching reddit content', err);
return [];
}
}
function filterContent(data) {
const images = data.filter(cur => cur.data.url.endsWith('.jpg') || cur.data.url.endsWith('.png'));
const posts = images.map(cur => ({
title: cur.data.title,
url: cur.data.url,
sub: cur.data.subreddit_name_prefixed,
id: cur.data.id
}));
return posts;
}
function getStatus(title, sub) {
if (title.length < 100) {
return `${title} via ${sub}`;
}
const lastSpace = title.slice(0, 100).lastIndexOf(' ');
return `${title.slice(0, lastSpace)}... via ${sub}`;
}
async function postTweet(post, buf) {
const { title, sub, id } = post;
try {
const media = await client.post('media/upload', { media: buf });
if (media) {
const status = {
status: getStatus(title, sub),
media_ids: media.media_id_string
};
try {
const tweet = await client.post('statuses/update', status);
if (tweet) {
arr.push(id);
}
} catch (err) {
console.log('Error posting tweet', err);
}
}
} catch (err) {
console.log('Error adding image', err);
}
}
async function getImage(post) {
const { url } = post;
try {
const data = await fetch(url);
const buffer = await data.arrayBuffer();
postTweet(post, Buffer.from(buffer));
} catch (err) {
console.log('Error getting image', err);
}
}
function removeOldPosts(posts) {
return posts.filter(cur => arr.indexOf(cur.id) === -1);
}
async function start() {
const posts = await getPosts(); // todo: add filter for reposts
const content = filterContent(posts);
const newPosts = removeOldPosts(content);
newPosts.forEach((cur) => {
getImage(cur);
});
}
function init() {
console.log('running...');
start();
return setInterval(() => {
start();
}, 1000 * 60 * 60 * 4);
}
init();