Last active
March 23, 2022 07:48
-
-
Save aorborc/20b851eedf311fb8dfe9fbb078677693 to your computer and use it in GitHub Desktop.
Get the latest posts from Zoho Blogs posted in the last hour and send an email via Google Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function readAndSend() { | |
let url = 'https://www.zoho.com/blog/feed'; | |
let xml = UrlFetchApp.fetch(url).getContentText(); | |
let document = XmlService.parse(xml.trim()); | |
let root = document.getRootElement(); | |
let channel = root.getChild('channel'); | |
let items = channel.getChildren('item'); | |
for(i =0; i< items.length; i++){ | |
let item = items[i]; | |
let pubDateStr = item.getChild('pubDate').getText(); | |
let pubDate = new Date(pubDateStr); | |
let now =new Date(); | |
let lastHour = now.getTime() - (1000*60*60); | |
let postedInTheLastHour = pubDate.getTime() > lastHour; | |
if(!postedInTheLastHour) { | |
continue; | |
} | |
let title = item.getChild('title').getText(); | |
let categories = item.getChildren('category'); | |
let label = categories[0].getText(); | |
let link = item.getChild('link').getText(); | |
let description = item.getChild('description').getText(); | |
console.log(pubDate,lastHour , postedInTheLastHour) | |
let subject = title + " - " + label; | |
let desInd = description.lastIndexOf('<p>The post '); | |
description = description.substring(0,desInd); | |
description = '<h4><a rel="nofollow" href="' + link +'">'+ subject + '</a></h4>' + description; | |
GmailApp.sendEmail('[email protected]', subject, description, { | |
htmlBody: description, | |
from: "[email protected]", | |
name: "your name" | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment