-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (78 loc) · 2.34 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
const axios = require("axios");
const fs = require("fs").promises;
const path = require("path");
const parser = require("xml2json");
const TurndownService = require("turndown");
const turndownService = new TurndownService();
const parse = async (url) => {
const { data } = await axios.get(url);
return JSON.parse(await parser.toJson(data));
};
const getLastItem = (rss) => {
const item = rss.rss.channel.item;
console.log(item);
if (Array.isArray(item))
// Get the latest post
return item.reduce((prev, v) => {
if (new Date(prev.pubDate) < new Date(v.pubDate)) return v;
return prev;
});
return item;
};
(async () => {
const feedCache = JSON.parse(
await fs
.readFile(path.join(__dirname, ".cache.json"), "utf-8")
.catch(() => "{}") // File does not exist
);
const feedConfig = JSON.parse(
await fs
.readFile(path.join(__dirname, "feeds.json"), "utf-8")
.catch(() => "[]") // File does not exist
);
const feed = await Promise.all(
feedConfig.map(async (v) => ({
...v,
data: getLastItem(await parse(v.url)),
}))
);
try {
await Promise.all(
feed.map(async (channel) => {
const date = new Date(channel.data.pubDate);
if (date.getTime() > (feedCache[channel.url] || 0)) {
feedCache[channel.url] = date.getTime();
await axios.post(channel.webhookUrl, {
content: `Novo anúncio de ${channel.name}${
channel.roleId ? ` <@&${channel.roleId}>` : ``
}`,
embeds: [
{
title: channel.data.title.substring(0, 256),
description: turndownService
.turndown(channel.data.description)
.substring(0, 2048),
url: channel.data.link,
color: parseInt(channel.color.substring(1), 16),
author: {
name:
channel.data.author.match(/\((.+)\)/)?.[1] ||
channel.data.author,
},
footer: {
text: channel.name,
},
timestamp: date.toISOString(),
},
],
});
}
})
);
} catch (ignore) {}
fs.writeFile(
path.join(__dirname, ".cache.json"),
JSON.stringify(feedCache),
"utf-8"
);
})();