Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
var DEBUG = false;

// URL to a google sheet you have permissions to
// Columns MUST be in this order:
// Columns MUST be in this order:
// feed_name {STRING)}
// feed_url (URL)
// feed_type (RSS or ATOM)
// excluded_cvs (comma separated list of keywords to exclude)
// feed_logo (URL)
// webhook_url (URL)
// status (STRING) - active | disabled
Expand All @@ -23,7 +24,7 @@ var GOOGLE_SHEET_TAB_NAME = "feed_data";
var all_sheet_rows = SpreadsheetApp.openByUrl(GOOGLE_SHEET_URL).getSheetByName(GOOGLE_SHEET_TAB_NAME).getDataRange().getValues();

var filteredRows = all_sheet_rows.filter(function(row){
if (row[5] === 'active') {
if(row[6] === 'active') {
return row;
}
});
Expand All @@ -32,58 +33,61 @@ var filteredRows = all_sheet_rows.filter(function(row){
function fetch_all_feeds() {

filteredRows.forEach(function(row, index) {
fetchNews(row[0], row[1], row[2], row[3], row[4]);

fetchNews(row[0], row[1], row[2], row[3], row[4], row[5]);

});

}

// fetch a feed, and send any new events through to the associated Chat room
function fetchNews(FEED_NAME, FEED_URL, FEED_TYPE, FEED_LOGO_URL, WEBHOOK_URL) {
function fetchNews(FEED_NAME, FEED_URL, FEED_TYPE, EXCLUDED_CVS, FEED_LOGO_URL, WEBHOOK_URL) {

var lastUpdate = new Date(parseFloat(PropertiesService.getScriptProperties().getProperty("lastUpdate")) || 0);

Logger.log("Last update: " + lastUpdate);

Logger.log("Fetching '" + FEED_NAME + "'...");
//Logger.log("URL '" + FEED_URL + "'...");
//Logger.log("LOGO '" + FEED_LOGO_URL + "'...");

var xml = UrlFetchApp.fetch(FEED_URL).getContentText();
var document = XmlService.parse(xml);

if(FEED_TYPE == "RSS") {

Logger.log("RSS Feed being parsed - " + FEED_NAME);

var items = document.getRootElement().getChild('channel').getChildren('item').reverse();

Logger.log(items.length + " entrie(s) found");

var count = 0;

for (var i = 0; i < items.length; i++) {

var pubDate = new Date(items[i].getChild('pubDate').getText());
var title = items[i].getChild("title").getText();
var description = items[i].getChild("description").getText();
var link = items[i].getChild("link").getText();
var eventDate = items[i].getChild("pubDate").getText();

if(DEBUG){
Logger.log("------ " + (i+1) + "/" + items.length + " ------");
Logger.log(pubDate);
Logger.log(title);
Logger.log(link);
if(excludedList.some(keyword => title.includes(keyword))){
Logger.log("Excluded, title matches keywords: "+excludedList);
}
// Logger.log(description);
Logger.log("--------------------");
}

// check to make sure the feed event is after the last time we ran the script
if(pubDate.getTime() > lastUpdate.getTime()) {
//Logger.log("Logging Event - Title: " + title + " | Date: " + eventDate + " | Link: " + link);
if(!DEBUG){
if((!DEBUG)&&(!excludedList.some(keyword => title.includes(keyword)))){
postTopicAsCard_(WEBHOOK_URL, FEED_NAME, FEED_URL, FEED_LOGO_URL, title, eventDate, link);
}
PropertiesService.getScriptProperties().setProperty("lastUpdate", pubDate.getTime());
Expand All @@ -105,6 +109,7 @@ function fetchNews(FEED_NAME, FEED_URL, FEED_TYPE, FEED_LOGO_URL, WEBHOOK_URL) {
var count = 0;
for (var i = 0; i < entries.length; i++) {
var title = entries[i].getChild('title', atom).getText();
var excludedList = EXCLUDED_CVS.toString().split(",");
var pubDate = new Date(entries[i].getChild('updated', atom).getText());
var link = entries[i].getChild("link", atom).getAttribute('href').getValue();
var eventDate = entries[i].getChild("updated", atom).getText();
Expand All @@ -114,14 +119,17 @@ function fetchNews(FEED_NAME, FEED_URL, FEED_TYPE, FEED_LOGO_URL, WEBHOOK_URL) {
Logger.log(pubDate);
Logger.log(title);
Logger.log(link);
if(excludedList.some(keyword => title.includes(keyword))){
Logger.log("Excluded, title matches keywords: "+excludedList);
}
// Logger.log(description);
Logger.log("--------------------");
}

// check to make sure the feed event is after the last time we ran the script
if(pubDate.getTime() > lastUpdate.getTime()) {
//Logger.log("Logging Event - Title: " + title + " | Date: " + eventDate + " | Link: " + link);
if(!DEBUG){
if((!DEBUG)&&(!excludedList.some(keyword => title.includes(keyword)))){
postTopicAsCard_(WEBHOOK_URL, FEED_NAME, FEED_URL, FEED_LOGO_URL, title, eventDate, link);
}
PropertiesService.getScriptProperties().setProperty("lastUpdate", pubDate.getTime());
Expand All @@ -133,12 +141,12 @@ function fetchNews(FEED_NAME, FEED_URL, FEED_TYPE, FEED_LOGO_URL, WEBHOOK_URL) {
Logger.log(entries.length + " entrie(s) found");
Logger.log("--> " + count + " item(s) posted");
}

}

// quick function to take the info, send it to create a card, and then post the card.
function postTopicAsCard_(webhook_url, feed_name, feed_url, feed_logo_url, card_title, card_subtitle, card_link) {

var card_json = createCardJson(feed_name, feed_url, feed_logo_url, card_title, card_subtitle, card_link);

// set options for what will be sent to Chat according to documentation
Expand All @@ -147,7 +155,7 @@ function postTopicAsCard_(webhook_url, feed_name, feed_url, feed_logo_url, card_
'contentType': 'application/json',
'payload' : JSON.stringify(card_json)
};

UrlFetchApp.fetch(webhook_url, options);
}

Expand Down Expand Up @@ -191,4 +199,4 @@ function createCardJson(feed_name, feed_url, feed_logo_url, card_title, card_sub
}]
}]
};
}
}