diff --git a/config.json b/config.json index 596b3ea..2822763 100644 --- a/config.json +++ b/config.json @@ -2,5 +2,6 @@ "feedUrl": "https://www.youtube.com/feeds/videos.xml?user=EthosLab", "channelID": "241238530376990722", "pollingInterval": 5000, - "numLinksToCache": 10 + "numLinksToCache": 10, + "youtubeMode": true } \ No newline at end of file diff --git a/feed-bot.js b/feed-bot.js index 6d80268..ddfe1cd 100644 --- a/feed-bot.js +++ b/feed-bot.js @@ -17,6 +17,9 @@ var latestFeedLink = ""; var linkRegExp = new RegExp(["http", "https", "www"].join("|")); var cachedLinks = []; +var youtubeShareUrl = "http://youtu.be/"; +var youtubeFullUrl = "http://www.youtube.com/watch?v="; + //caches a link so we can check again later function cacheLink(link) { //cheaty way to get around http and https not matching @@ -31,6 +34,18 @@ function cacheLink(link) { cachedLinks.shift(); } +function checkCache(link) { + if (Config.youtubeMode && link.includes(youtubeFullUrl)) { + return cachedLinks.includes(convertToYoutubeShareUrl(link)); + } + return cachedLinks.includes(link); +} + +function convertToYoutubeShareUrl(fullUrl){ + var shareUrl = fullUrl.replace(youtubeFullUrl, youtubeShareUrl); + shareUrl.splice(0, shareUrl.indexOf("&")); +} + //check if we can connect to discordapp.com to authenticate the bot Dns.resolve("discordapp.com", function (err) { if (err) Log.error("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot (you are probably not connected to the internet).", err); @@ -96,7 +111,7 @@ function checkLinkAndPost(err, articles) { var latestLink = articles[0].link.replace("https", "http"); //check whether the latest link out the feed exists in our cache - if (!cachedLinks.includes(latestLink)) { + if (!checkCache(latestLink)) { Log.info("Attempting to post new link: " + latestLink); //send a messsage containing the new feed link to our discord channel