2017-09-09 22:26:04 +03:00
|
|
|
//my imports
|
2017-09-20 02:04:04 +03:00
|
|
|
const DiscordUtil = require("../../discord-bot-core").util;
|
2017-09-09 22:26:04 +03:00
|
|
|
|
|
|
|
//external lib imports
|
|
|
|
const Dns = require("dns"); //for host resolution checking
|
|
|
|
const Url = require("url"); //for url parsing
|
|
|
|
const FeedRead = require("feed-read"); //for extracing new links from RSS feeds
|
|
|
|
const GetUrls = require("get-urls"); //for extracting urls from messages
|
|
|
|
const ShortID = require("shortid"); //to provide ids for each feed, allowing guilds to remove them
|
|
|
|
|
|
|
|
module.exports = class FeedData {
|
2017-10-02 01:19:10 +03:00
|
|
|
constructor({ id = null, url, channelID, roleID, cachedLinks = null, maxCacheSize }) {
|
2017-09-09 22:26:04 +03:00
|
|
|
this.id = id || ShortID.generate();
|
|
|
|
this.url = url;
|
2017-10-02 01:19:10 +03:00
|
|
|
this.channelID = channelID;
|
|
|
|
this.roleID = roleID;
|
2017-09-09 22:26:04 +03:00
|
|
|
this.cachedLinks = cachedLinks || [];
|
|
|
|
this.maxCacheSize = maxCacheSize || 10;
|
|
|
|
|
|
|
|
this.cachedLinks.push = (...elements) => {
|
2017-10-02 01:19:10 +03:00
|
|
|
Array.prototype.push.apply(
|
|
|
|
this.cachedLinks,
|
|
|
|
elements
|
|
|
|
.map(el => normaliseUrl(el))
|
|
|
|
.filter(el => !this.cachedLinks.includes(el))
|
|
|
|
);
|
2017-09-09 22:26:04 +03:00
|
|
|
|
2017-10-02 01:19:10 +03:00
|
|
|
//seeing as new links come in at the end of the array, we need to remove the old links from the beginning
|
|
|
|
this.cachedLinks.splice(0, this.cachedLinks.length - this.maxCacheSize);
|
2017-09-09 22:26:04 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-24 23:31:30 +03:00
|
|
|
/**@param param*/
|
2017-09-09 22:26:04 +03:00
|
|
|
updatePastPostedLinks(guild) {
|
2017-10-02 01:19:10 +03:00
|
|
|
const channel = guild.channels.get(this.channelID);
|
|
|
|
|
|
|
|
if (!channel)
|
|
|
|
return Promise.reject("Channel not found!");
|
|
|
|
|
2017-09-09 22:26:04 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
channel.fetchMessages({ limit: 100 })
|
|
|
|
.then(messages => {
|
2017-10-02 01:19:10 +03:00
|
|
|
/* we want to push the links in oldest first, but discord.js returns messages newest first, so we need to reverse them
|
|
|
|
* discord.js returns a map, and maps don't have .reverse methods, hence needing to spread the elements into an array first */
|
|
|
|
[...messages.values()].reverse().forEach(m => this.cachedLinks.push(...GetUrls(m.content)));
|
|
|
|
resolve();
|
2017-09-09 22:26:04 +03:00
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-24 23:31:30 +03:00
|
|
|
/**@param param */
|
2017-10-02 01:19:10 +03:00
|
|
|
fetchLatest(guild) {
|
|
|
|
Dns.resolve(Url.parse(this.url).host || "", err => {
|
2017-09-09 22:26:04 +03:00
|
|
|
if (err)
|
2017-10-02 01:19:10 +03:00
|
|
|
DiscordUtil.dateError("Connection Error: Can't resolve host", err.message || err);
|
2017-09-09 22:26:04 +03:00
|
|
|
else
|
2017-10-02 01:19:10 +03:00
|
|
|
this._doFetchRSS(guild);
|
2017-09-09 22:26:04 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
const blacklist = ["cachedLinks", "maxCacheSize"];
|
2017-10-02 01:19:10 +03:00
|
|
|
return `\`\`\`JavaScript\n ${JSON.stringify(this, (k, v) => !blacklist.find(x => x === k) ? v : undefined, "\t")} \`\`\``;
|
|
|
|
}
|
|
|
|
|
|
|
|
_doFetchRSS(guild) {
|
|
|
|
FeedRead(this.url, (err, articles) => {
|
|
|
|
if (err)
|
|
|
|
return DiscordUtil.dateError(err.message || err);
|
|
|
|
|
|
|
|
const latest = normaliseUrl(articles[0].link);
|
|
|
|
|
|
|
|
if (!this.cachedLinks.includes(latest)) {
|
|
|
|
this.cachedLinks.push(latest);
|
|
|
|
|
|
|
|
const channel = guild.channels.get(this.channelID),
|
|
|
|
role = guild.roles.get(this.roleID);
|
|
|
|
|
|
|
|
channel.send((role ? role + " " : "") + latest)
|
|
|
|
.catch(err => DiscordUtil.dateError(`Error posting in ${channel.id}: ${err.message || err}`));
|
|
|
|
}
|
|
|
|
});
|
2017-09-09 22:26:04 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function normaliseUrl(url) {
|
2017-09-13 01:50:03 +03:00
|
|
|
url = url.replace("https://", "http://"); //hacky way to treat http and https the same
|
2017-09-09 22:26:04 +03:00
|
|
|
|
2017-09-20 02:43:59 +03:00
|
|
|
const parsedUrl = Url.parse(url);
|
|
|
|
if (parsedUrl.host.includes("youtube.com")) {
|
2017-09-20 02:55:23 +03:00
|
|
|
const videoIDParam = (parsedUrl.query || "").split("&").find(x => x.startsWith("v="));
|
2017-09-20 02:43:59 +03:00
|
|
|
if (videoIDParam) {
|
|
|
|
const videoID = videoIDParam.substring(videoIDParam.indexOf("=") + 1, videoIDParam.length);
|
|
|
|
url = "http://youtu.be/" + videoID;
|
|
|
|
}
|
|
|
|
}
|
2017-09-09 22:26:04 +03:00
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|