From f02602955a6bc15f34a4fec9f0221842ef0f4b02 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 12 Aug 2017 01:55:58 +0100 Subject: [PATCH] Add feed checking logic, set to run at 30 sec intervals --- app/config.json | 1 + app/index.js | 14 +++++++++--- app/models/feed-data.js | 46 ++++++++++++++++++++++++++++++++++++++++ app/models/guild-data.js | 6 +++++- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/app/config.json b/app/config.json index 26971c6..ff568bc 100644 --- a/app/config.json +++ b/app/config.json @@ -1,5 +1,6 @@ { "saveIntervalSec": 60, + "feedCheckIntervalSec": 30, "commands": { "version": "version", "addFeed": "add-feed" diff --git a/app/index.js b/app/index.js index 68168c4..b02d198 100644 --- a/app/index.js +++ b/app/index.js @@ -20,8 +20,13 @@ module.exports = (client) => { const guildsData = FileSystem.existsSync(SAVE_FILE) ? fromJSON(JsonFile.readFileSync(SAVE_FILE)) : {}; setInterval(() => writeFile(guildsData), config.saveIntervalSec * 1000); - parseLinksInAllGuilds(client.guilds, guildsData).then(writeFile(guildsData)); + parseLinksInGuilds(client.guilds, guildsData).then(writeFile(guildsData)); + //set up an interval to check all the feeds + checkFeedsInGuilds(guildsData); + setInterval(() => checkFeedsInGuilds(guildsData), config.feedCheckIntervalSec * 1000); + + //set up an on message handler to detect when links are posted client.on("message", message => { if (message.author.id !== client.user.id) { //check the bot isn't triggering itself if (message.channel.type === "dm") @@ -69,11 +74,14 @@ function addFeed(client, guildsData, message) { } else responseMessage.reply("Your feed has not been saved, please add it again with the correct details"); - }); } -function parseLinksInAllGuilds(guilds, guildsData) { +function checkFeedsInGuilds(guildsData) { + guildsData.forEach(guild => guild.checkFeeds()); +} + +function parseLinksInGuilds(guilds, guildsData) { const promises = []; for (let guild of guilds) { const guildData = guildsData[guild.id]; diff --git a/app/models/feed-data.js b/app/models/feed-data.js index f4aae1c..7f47450 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -1,3 +1,11 @@ +//my imports +const DiscordUtil = require("discordjs-util"); + +//external lib imports +const Dns = require("dns"); +const Url = require("url"); +const FeedRead = require("feed-read"); + module.exports = class FeedData { constructor({ link, channelName, roleName, cachedLinks }) { this.link = link; @@ -23,8 +31,46 @@ module.exports = class FeedData { .catch(reject); }); } + + check(guild) { + Dns.resolve(Url.parse(this.link).host, err => { //check we can resolve the host, so we can throw an appropriate error if it fails + if (err) + DiscordUtil.dateError("Connection Error: Can't resolve host", err); //log our error if we can't resolve the host + else + FeedRead(this.link, (err, articles) => { //check the feed + if (err) + DiscordUtil.dateError(err); + else { + let latest = articles[0]; //extract the latest link + latest = normaliseUrl(latest); //standardise it a bit + + //if we don't have it cached already, cache it and callback + if (!this.cachedLinks.includes(latest)) { + this.cachedLinks.push(latest); + post(guild, latest); + } + } + }); + }); + } }; +function post(guild, link){ + const channel = guild.channels.first(ch => ch.type === "text" && ch.name.toLower() === this.channelName.toLower()); + channel.send(link); +} + +function normaliseUrl(url) { + url = url.replace("https://", "http://"); //cheaty way to get around http and https not matching + + if (Url.parse(url).host.includes("youtu")) //detect youtu.be and youtube.com - yes I know it's hacky + url = url.split("&")[0]; //quick way to chop off stuff like &feature=youtube + + url = url.replace("http://www.youtube.com/watch?v=", "http://youtu.be/"); //turn full url into share url + + return url; +} + function getUrls(str) { return str.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig); } \ No newline at end of file diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 82de0f9..1264809 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -9,11 +9,15 @@ module.exports = class GuildData { cachePastPostedLinks() { const promises = []; - + this.feeds.forEach(feed => { promises.push(feed.cachePastPostedLinks(this).catch(Util.dateError)); }); return Promise.all(promises); } + + checkFeeds() { + this.feeds.forEach(feed => feed.check()); + } }; \ No newline at end of file