-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
354 lines (260 loc) · 9.44 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const process = require('process');
const fs = require("fs");
const rssParser = new (require("rss-parser"))();
const htmlParser = require("node-html-parser");
const md5 = require("md5");
const fetch = require("node-fetch");
const needle = require('needle');
const feedsFilename = "feeds.json";
const hashesFilename = "hashes.json";
const UpdateTimeout = process.env.RSS_UPDATE_TIMEOUT || 60 * 1000 * 5;
const twitterCred = {
bearer_token: process.env.TWITTER_BEARER_TOKEN
};
function LoadFeeds() {
return JSON.parse(fs.readFileSync(__dirname+"/"+feedsFilename, "utf8"));
}
function LoadHashes() {
try {
return JSON.parse(fs.readFileSync(__dirname+"/"+hashesFilename, "utf8")) || {};
} catch (error) {
return {};
}
}
function SaveHashes(hashes) {
return fs.writeFileSync(__dirname+"/"+hashesFilename, JSON.stringify(hashes), "utf8");
}
async function GetRSSEntries(feedItem) {
let feed = await rssParser.parseURL(feedItem.url);
let hash = md5(feedItem.url);
let entries = [];
feed.items.forEach(function(entry) {
let entryTitle = entry.title || "";
let entryUrl = entry.link || "";
let entryAuthor = entry.creator || "";
let entryContent = entry.content || "";
let entryPublished = entry.pubDate || "";
let entryImageUrl;
try {
entryImageUrl = entry.enclosure.url;
} catch (error) { }
if (!entryImageUrl) {
try {
entryImageUrl = htmlParser
.parse(entry.content)
.querySelector("img")
.getAttribute("src");
} catch (error) {
entryImageUrl = "";
}
}
entries.push({
EntryTitle: entryTitle,
EntryUrl: entryUrl,
EntryAuthor: entryAuthor,
EntryContent: entryContent,
EntryPublished: entryPublished,
EntryImageUrl: entryImageUrl
});
});
return entries;
}
async function SendWebhook(webhookOptions, postBody, embed) {
let fetchBody = JSON.stringify({
username: webhookOptions.username,
avatar_url: webhookOptions.userimage,
embeds: postBody
});
if(!embed) {
fetchBody = JSON.stringify({
username: webhookOptions.username,
avatar_url: webhookOptions.userimage,
content: postBody
});
}
fetch(webhookOptions.url,{
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: fetchBody
}
);
let date = new Date();
console.log("Webhook sent from " + webhookOptions.username + " @ " + date.toISOString());
}
async function getTwitterID(username) {
const endpointURL = "https://api.twitter.com/2/users/by?usernames="
// These are the parameters for the API request
// specify User names to fetch, and any additional fields that are required
// by default, only the User ID, name and user name are returned
const params = {
usernames: username, // Edit usernames to look up
"user.fields": "created_at,description", // Edit optional query parameters here
"expansions": "pinned_tweet_id"
}
// this is the HTTP header that adds bearer token authentication
const res = await needle('get', endpointURL, params, {
headers: {
"User-Agent": "v2UserLookupJS",
"authorization": `Bearer ${twitterCred.bearer_token}`
}
})
if (res.body) {
return res.body.data[0].id;
} else {
throw new Error('Unsuccessful request')
}
}
const getUserTweets = async (userID) => {
const url = `https://api.twitter.com/2/users/${userID}/tweets`;
let userTweets = [];
// we request the author_id expansion so that we can print out the user name later
let params = {
"max_results": 5,
"tweet.fields": "created_at",
"expansions": "author_id"
}
const options = {
headers: {
"User-Agent": "v2UserTweetsJS",
"authorization": `Bearer ${twitterCred.bearer_token}`
}
}
let nextToken = null;
let userName;
let resp = await getTwitterPage(params, options, nextToken, url);
if (resp && resp.meta && resp.meta.result_count && resp.meta.result_count > 0) {
userName = resp.includes.users[0].username;
if (resp.data) {
resp.data.sort(function(a,b){
return new Date(a.created_at) - new Date(b.created_at);
});
userTweets.push.apply(userTweets, resp.data);
}
}
return userTweets;
}
const getTwitterPage = async (params, options, nextToken, url) => {
if (nextToken) {
params.pagination_token = nextToken;
}
try {
const resp = await needle('get', url, params, options);
if (resp.statusCode != 200) {
//console.log(`${resp.statusCode} ${resp.statusMessage}:\n${resp.body}`);
return;
}
return resp.body;
} catch (err) {
throw new Error(`Request failed: ${err}`);
}
}
async function ProcessTwitterFeed(feedItem, hashes) {
let feedHash = md5(feedItem.url);
let feedURL = new URL(feedItem.url);
let feedUser = feedURL.pathname.split('/')[1];
let feedID = await getTwitterID(feedUser);
let entries = await getUserTweets(feedID);
let newItems = 0;
let postBody = [];
if(typeof hashes[feedHash] == 'undefined') hashes[feedHash] = [];
let newHashes = [];
entries.forEach(function(entry) {
let entryHash = md5(entry.id);
let hashEntry = {'hash': entryHash, 'date': entry.created_at};
let entryFound = false;
hashes[feedHash].forEach(function(row) {
if (row.hash == entryHash) entryFound=true;
});
if(!entryFound) {
var todaysDate = new Date();
var tweetDate = new Date(entry.created_at);
if(tweetDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
let tweetlink = `https://twitter.com/${feedUser}/status/${entry.id}`
postBody.push(tweetlink);
newItems++;
}
}
newHashes.push(hashEntry);
});
if(postBody.length > 0) {
postBody.forEach(function(body) {
feedItem.webhooks.forEach(function(webhook) {
let webhookoptions = {'username':feedItem.username, 'userimage':feedItem.userimage, 'url':webhook};
SendWebhook(webhookoptions, body, false);
});
});
}
console.log(feedItem.username + " - Found " + newItems + " new of " + entries.length + " total." );
return newHashes;
}
async function ProcessRSSFeed(feedItem, hashes) {
let feedHash = md5(feedItem.url);
let entries = await GetRSSEntries(feedItem);
let newItems = 0;
let postBody = [];
if(typeof hashes[feedHash] == 'undefined') hashes[feedHash] = [];
let newHashes = [];
entries.forEach(function(entry) {
let entryHash = md5(entry.EntryUrl);
let hashEntry = {'hash': entryHash, 'date': entry.EntryPublished};
let entryFound = false;
hashes[feedHash].forEach(function(row) {
if (row.hash == entryHash) entryFound=true;
});
if(!entryFound) {
postBody.push({
color: 11730954,
title: entry.EntryTitle,
url: entry.EntryUrl
});
newItems++;
}
newHashes.push(hashEntry);
});
if(postBody.length > 0) {
// Discord has rate limiting for embeds so we break them up into groups of 10.
var i,j,bodyChunk,chunk = 10;
for (i=0,j=postBody.length; i<j; i+=chunk) {
bodyChunk = postBody.slice(i,i+chunk);
feedItem.webhooks.forEach(function(webhook) {
let webhookoptions = {'username':feedItem.username, 'userimage':feedItem.userimage, 'url':webhook};
SendWebhook(webhookoptions, bodyChunk, true);
});
}
}
console.log(feedItem.username + " - Found " + newItems + " new of " + entries.length + " total." );
return newHashes;
}
async function ProcessAllFeeds(feeds, hashes) {
var processStart = new Date();
console.log("Processing feeds at:", processStart.toUTCString());
for (let index = 0; index < feeds.length; index++) {
let feedHash = md5(feeds[index].url);
let feedURL = new URL(feeds[index].url);
if(feedURL.hostname.toLowerCase() == "twitter.com"){
if(twitterCred.bearer_token){
hashes[feedHash] = await ProcessTwitterFeed(feeds[index], hashes);
} else {
console.log("Twitter Token not found!");
}
}
else
{
hashes[feedHash] = await ProcessRSSFeed(feeds[index], hashes);
}
}
SaveHashes(hashes);
}
// Entry point
(async () => {
let feeds = LoadFeeds();
let hashes = LoadHashes();
if(twitterCred.bearer_token) console.log("Twitter Token found!");
await ProcessAllFeeds(feeds, hashes); // Force first check
let timerId = setInterval(
async () => await ProcessAllFeeds(feeds, hashes),
UpdateTimeout
);
})();