forked from shantismurf/ficfeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
287 lines (281 loc) · 9.63 KB
/
node.js
File metadata and controls
287 lines (281 loc) · 9.63 KB
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
import dotenv from 'dotenv';
dotenv.config({ path: './.env' });
var channels = [];
let sendchannel = '';
const msgurl = `https://discord.com/api/v10/channels/{}/messages?limit=30`;
const cache = {};
const getchurl = `https://discord.com/api/v10/guilds/${process.env.GUILD}/channels`;
const seen = new Set();
const chnames = (process.env.CHANNELS ?? '').split(',');
const toamt = process.env.TIMEOUT ?? 60;
async function fetchData() {
if (cache[getchurl]) {
return cache[getchurl];
}
try {
const response = await fetch(getchurl, {
headers: {
'Authorization': `Bot ${process.env.TOKEN}`,
'Content-Type': 'application/json',
},
});
const data = await response.json();
cache[getchurl] = data;
return data;
} catch (error) {
console.error(error);
}
}
async function getmsg(c, i) {
const now = new Date();
const timestampString = now.toISOString();
console.log('Fetching channel ' + (i + 1) + '/' + channels.length + '...' + timestampString);
var x = null;
try {
const response = await fetch(msgurl.replace('{}', c), {
method: 'GET',
headers: {
'Authorization': `Bot ${process.env.TOKEN}`,
'Content-Type': 'application/json',
},
});
x = await response.json();
} catch (e) {
console.log('Error:', e.toString() + ' - ' + timestampString);
}
x = x instanceof Array ? x : [];
return x.map(x => [x, c]);
}
async function fetchMessages(start) {
try {
var res = [].concat(...(await Promise.all(channels.map((x, i) =>
new Promise(y => setTimeout(async () => y(await getmsg(x, i)), i * 7e3))
))));
var newmsg = res.filter(msg => !seen.has(msg[0].id)).map(msg => {
return msg.concat([(msg[0].content.match(/archiveofourown.org\/works\/\d{1,12}/g) ?? [])[0]]);
}).filter(x => x[2] != undefined).sort((a, b) => new Date(b[0].timestamp).getTime() - new Date(a[0].timestamp).getTime());
var to = -1;
newmsg.forEach(async msg => {
seen.add(msg[0].id);
if (!start) {
var ao3 = await ao3api('https://' + msg[2] + '/');
to++;
console.log('New Message:', msg[0].content);
//set limits on summary and tag string lengths and append elipsis if truncated
var summarystr = (ao3.summary ?? 'None').substring(0, 400);
summarystr = summarystr.length == 400 ? summarystr + ' ...' : summarystr;
var tagstr = (ao3.freeform ?? 'None').substring(0, 400);
tagstr = tagstr.length == 400 ? tagstr + ' ...' : tagstr;
setTimeout(() =>
fetch(msgurl.replace('{}', sendchannel), {
method: 'POST',
headers: {
'Authorization': `Bot ${process.env.TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(
ao3.error ? {
embeds: [{
title: 'Preview not available. Click here to see work.',
url: `https://${msg[2]}/`,
description:
`Posted by <@${msg[0].author.id}> in https://discord.com/channels/${process.env.GUILD}/${msg[1]}/${msg[0].id}`,
color: 0x0000FF,
}]
} :
{
embeds: [{
title: ao3.title,
url: `https://${msg[2]}/`,
description:
`Posted by <@${msg[0].author.id}> in https://discord.com/channels/${process.env.GUILD}/${msg[1]}/${msg[0].id}`,
color: ({
"Not Rated": 0x808080, "General Audiences": 0x0000FF,
"Teen And Up Audiences": 0x008000, "Mature": 0xFFA500, "Explicit": 0xFF0000
})[ao3.rating],
author: {
name: 'A work by ' + ao3.author,
url: ao3.authorlink,
},
fields: [
{
name: 'Published' + (ao3.status ? ' | Updated' : ''),
value: (ao3.published + (ao3.status ? ' | ' + ao3.status : '')).substring(0, 1024),
inline: true
},
{
name: 'Words | Chapters',
value: (ao3.words + ' | ' + ao3.chapters).substring(0, 1024),
inline: true,
},
{
name: 'Rating | Warning',
value: (ao3.rating + ' | ' + ao3.warning).substring(0, 1024),
// inline: true,
},
{
name: 'Tags',
value: tagstr,
inline: true,
},
{
name: 'Summary',
value: summarystr,
// inline: true,
},
],
footer: {
text: ('Hits: ' + ao3.hits + ' | Kudos: ' + (ao3.kudos ?? 0) +
' | Comments: ' + (ao3.comments ?? 0)).substring(0, 1024)
}
}]
}
),
}).then(e => e.json()).then(e => e.code ? console.log(JSON.stringify(e)) : ''),
to * 5e3);
}
});
} catch (e) {
console.log('Error:', e.toString() + ' - ' + timestampString);
async function getmsg(c, i) {
try {
const now = new Date();
const timestampString = now.toISOString();
console.log('Fetching channel ' + (i + 1) + '/' + channels.length + '...' + timestampString);
const response = await fetch(msgurl.replace('{}', c), {
method: 'GET',
headers: {
'Authorization': `Bot ${process.env.TOKEN}`,
'Content-Type': 'application/json',
},
});
const x = await response.json();
return x.map(x => [x, c]);
} catch (e) {
console.log('Error:', e.toString() + ' - ' + timestampString);
}
}
}
if (start)
console.log('Ready!');
setTimeout(fetchMessages, toamt * 1);//e3);
return res;
}
async function getch() {
console.log('Fetching channels...');
var res = await fetch(getchurl, {
method: 'GET',
headers: {
'Authorization': `Bot ${process.env.TOKEN}`,
'Content-Type': 'application/json'
}
}).then(x => x.json());
res = res.filter(x => x.type == 0);
sendchannel = res.splice(res.findIndex(x => x.name == process.env.FEED), 1)[0].id;
channels = res.filter(x => chnames[0] == '' ? true : chnames.includes(x.name)).map(x => x.id);
return;
}
async function ao3api(link) {
var errorCount = 0;
try {
var res = await fetch(link).then(e => e.text());
if (!res)
return {error: true};
var cur;
var v = {};
var rx = /<dd class="(.*?)(?<!stats)( tags)?">(.*?)<\/dd>/gs;
while (cur = rx.exec(res)) {
var ry = /(?<=<a class="tag" href=".*?">).*?(?=<\/a>)/gs;
var ml = [];
var m = '';
var i = 0;
while ((m = ry.exec(cur[3])) && i++ < 1000)
ml.push(m);
if (ml.length == 0)
ml = [cur[3]];
v[cur[1]] = ml.join(', ').replaceAll(''', "'");
};
const titleMatch = res.match(/<h2 class="title heading">(.*?)<\/h2>/s);
if (titleMatch) {
v.title = titleMatch[1].replace(/<[^>]*>/g, '').trim();
} else {
console.error('Failed to match title');
v.title = '';
errorCount++;
}
const authorMatch = res.match(/(?<=<a rel="author" href=".*?">).*?(?=<\/a>)/s);
if (authorMatch) {
v.author = authorMatch[0];
} else {
console.error('Failed to match author');
v.author = '';
errorCount++;
}
const authorLinkMatch = res.match(/(?<=<a rel="author" href=").*?(?=">)/s);
if (authorLinkMatch) {
v.authorlink = 'https://archiveofourown.org' + authorLinkMatch[0];
} else {
console.error('Failed to match author link');
v.authorlink = '';
errorCount++;
}
const summaryMatch = res.match(/<h3 class="heading">Summary:<\/h3>\s*<blockquote class="userstuff">([\s\S]*?)<\/blockquote>/s);
if (summaryMatch) {
const summary = summaryMatch[0]
.replace(/<(p|i|b)>/gs, (m, tag) => {
switch (tag) {
case 'p':
return '\n' + m + '\n';
case 'i':
return '*$&*';
case 'b':
return '**$&**';
default:
return '';
}
})
.replace(/<\/(p|i|b)>/gs, '')
.replace(/<br\s*\/?>/gs, '\n')
.replace(/Summary:/gs, '')
.replace(/<[^>]*>/g, '').trim()
.trim();
v.summary = summary;
} else {
console.error('Failed to match summary');
v.summary = '';
errorCount++;
}
const publishedMatch = res.match(/<dd class="published">.*?<\/dd>/);
if (publishedMatch) {
v.published = publishedMatch[0].replace(/<dd class="published">|<\/dd>/g, '');
} else {
console.error('Failed to match published date');
v.publishedDate = '';
errorCount++;
}
const updatedDateMatch = res.match(/<dd class="status">(.*?)<\/dd>/);
if (updatedDateMatch) {
v.updatedDate = updatedDateMatch[1];
} else {
console.error('Failed to match updated date');
v.updatedDate = '';
errorCount++;
}
if (errorCount > 5) {
return {error: true};
}
return v;
} catch (e) {
console.error(e)
return { error: e }
}
}
getch().then(() => {
fetchMessages(true);
});
setInterval(getch, 3600e3 * 12)
process.on('uncaughtException', (e) => {
console.error(e);
}).on('unhandledRejection', (e) => {
console.error(e);
});