Parse RSS Feeds with Google Apps Script

Published in: 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  = "";

  var feed = parseRSS(rssFeed, fromLang, toLang);
  return ContentService.createTextOutput(feed)

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) {

  rss += "";

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

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube