Parse Stack Exchange RSS Feeds

P
Published in: Google Apps Script - RSS

This Google Script will fetch and parse the XML RSS Feeds of any Stack Exchange website. You can then use the MailApp service to automatically push the content of the RSS feed to another email address on a regular basis. Or you can build an RSS to email service using Google Scripts.

function parseXML() {

  var feed = "http://stackexchange.com/feeds";

  var xml = UrlFetchApp.fetch(feed).getContentText();
  var root = XmlService.parse(xml).getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');

  var entries = root.getChildren('entry', atom);

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

    var title = entries[i].getChild('title', atom).getText();
    var categoryElements = entries[i].getChildren('category', atom);

    var updated = entries[i].getChild('updated', atom).getValue();
    var url = entries[i].getChild('id', atom).getValue();
    var summary = entries[i].getChild('summary', atom).getText();
    var user = entries[i].getChildren('author', atom)[0].getChild('name', atom).getValue();
    var userURL = entries[i].getChildren('author', atom)[0].getChild('uri', atom).getValue();

    var regexSubSite = /http:\/\/(.*?).stackexchange\.com/.exec(url);
    var site, siteURL;

    if (!regexSubSite) // If it's not a "subsite"...
    {
      site = /http:\/\/(.*)\.com/.exec(url);
      site = site ? site[1] : "noneFound";
      siteURL = site ? "http://www." + site + ".com" : "noneFound";

    } else {
      site = regexSubSite[1];
      siteURL = regexSubSite[0];
    }

    // url     - Question URL
    // title   - Question Title
    // user    - Question Author
    // userURL - Author Profile URL
    // site    - Stack Exchange Site Name
    // siteURL - SE Site URL

  }
}
Published in: Google Apps Script - RSS

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch