Parse RSS Feeds with Google Apps Script

This snippet explains how you can read and parse RSS feeds with Google Apps Script. The script reads the feed using URLFetchApp, translates the RSS Feed and then serves it as an RSS feed using the ContentService (the mime type is set to RSS).

Also, the feed content is cached for an hour to reduce the number of URL fetch requests.

function doGet() {
  var fromLang = 'en';
  var toLang = 'fr';
  var rssFeed = 'http://feeds.labnol.org/';

  var feed = parseRSS(rssFeed, fromLang, toLang);
  return ContentService.createTextOutput(feed).setMimeType(ContentService.MimeType.RSS);
}

function parseRSS(feed, fromLang, toLang) {
  var id = Utilities.base64Encode(feed + fromLang + toLang);

  // Cache the RSS feeds for an hour
  var cache = CacheService.getPublicCache();
  var rss = cache.get(id);

  if (rss != null) {
    return rss;
  }

  var item, date, title, link, desc, guid;

  var txt = UrlFetchApp.fetch(feed).getContentText();
  var doc = Xml.parse(txt, false);

  title = doc.getElement().getElement('channel').getElement('title').getText();

  // The RSS Feed is translated using Google Translate
  rss = '';
  rss += '';
  rss += LanguageApp.translate(title, fromLang, toLang);
  rss += ' (' + title + ')';

  var items = doc.getElement().getElement('channel').getElements('item');

  // Parsing single items in the RSS Feed
  for (var i in items) {
    try {
      item = items[i];

      title = item.getElement('title').getText();
      link = item.getElement('link').getText();
      date = item.getElement('pubDate').getText();
      desc = item.getElement('description').getText();

      guid = Utilities.base64Encode(link + fromLang + toLang);

      title = LanguageApp.translate(title, fromLang, toLang);
      desc = LanguageApp.translate(desc, fromLang, toLang, { contentType: 'html' });

      rss += '';
      rss += '  ' + title + '';
      rss += '  ' + link + '';
      rss += '  ' + date + '';
      rss += '  ' + guid + '';
      rss += '  ';
      rss += '';
    } catch (e) {
      Logger.log(e);
    }
  }

  rss += '';

  cache.put(id, rss, 3600);
  return rss;
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻