Last active
May 16, 2024 21:36
-
-
Save gmcabrita/5178cfd53fa32037eb84bb9748c8b4e7 to your computer and use it in GitHub Desktop.
gmail gapps script rss feed reader
This file contains hidden or 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 fetchAndEmailRSSFeeds() { | |
const rssUrls = [ | |
"https://brandur.org/atoms.atom", | |
"https://sirupsen.com/atom.xml", | |
"https://ferd.ca/feed.rss", | |
"https://andrewkelley.me/rss.xml", | |
"https://apenwarr.ca/log/rss.php", | |
"https://www.evanjones.ca/index.rss", | |
"https://danluu.com/atom.xml", | |
"https://thume.ca/atom.xml", | |
"https://www.scattered-thoughts.net/atom.xml", | |
"https://jvns.ca/atom.xml", | |
"https://mitchellh.com/feed.xml", | |
"https://davidgomes.com/rss/", | |
"https://bcantrill.dtrace.org/feed/", | |
]; | |
const properties = PropertiesService.getUserProperties(); | |
const now = new Date(); | |
const oneDayAgo = new Date(now.getTime() - (24 * 60 * 60 * 1000)); | |
rssUrls.forEach(url => { | |
const response = UrlFetchApp.fetch(url); | |
const document = XmlService.parse(response.getContentText()); | |
const root = document.getRootElement(); | |
const ns = XmlService.getNamespace('http://www.w3.org/2005/Atom'); | |
const isAtom = root.getName() === 'feed'; | |
const entries = isAtom ? root.getChildren('entry', ns) : root.getChild('channel').getChildren('item'); | |
entries.forEach(entry => { | |
const title = entry.getChildText('title', ns) ? entry.getChildText('title', ns) : entry.getChildText('title'); | |
const link = entry.getChild('link', ns) ? entry.getChild('link', ns).getAttribute('href').getValue() : entry.getChildText('link'); | |
const pubDateText = entry.getChildText('pubDate') || entry.getChildText('published', ns) || entry.getChildText('updated', ns); | |
const pubDate = pubDateText ? new Date(pubDateText) : new Date(); | |
const id = entry.getChildText('guid') || entry.getChildText('id', ns) || link; | |
const uniqueId = `${url}-${id}`; | |
if (pubDate > oneDayAgo && !properties.getProperty(uniqueId)) { | |
const subject = `[${url}] New Entry: ${title}`; | |
const content = entry.getChildText('description') || entry.getChildText('content', ns) || ''; | |
const htmlBody = ` | |
<h1>${title}</h1> | |
<p>${content}</p> | |
<a href="${link}">Read more</a> | |
`; | |
// Logger.log({ | |
// to: Session.getActiveUser().getEmail(), | |
// subject: subject, | |
// htmlBody: htmlBody | |
// }) | |
MailApp.sendEmail({ | |
to: Session.getActiveUser().getEmail(), | |
subject: subject, | |
htmlBody: htmlBody | |
}); | |
properties.setProperty(uniqueId, new Date().toISOString()); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment