Added youtube mode toggle with full url -> share url conversion

This commit is contained in:
benji7425 2016-12-02 21:53:13 +00:00
parent 5a5c9dd9eb
commit 0766a8351b
2 changed files with 18 additions and 2 deletions

View File

@ -2,5 +2,6 @@
"feedUrl": "https://www.youtube.com/feeds/videos.xml?user=EthosLab",
"channelID": "241238530376990722",
"pollingInterval": 5000,
"numLinksToCache": 10
"numLinksToCache": 10,
"youtubeMode": true
}

View File

@ -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