Convert Twitter JSON to RSS with Google Apps Script

C
Published in: Google Apps Script - Twitter

This Google Script converts the JSON response from the Twitter API to standard RSS feeds. The results are cached for 60 minutes and all requests to Twitter are authenticated with OAuth, an important requirement of the Twitter API v1.1.

 ```js function JSONtoRSS(json, type, key) {

oAuth();

var options = { “method”: “get”, “oAuthServiceName”:“twitter”, “oAuthUseToken”:“always” };

try {

var result = UrlFetchApp.fetch(json, options);

if (result.getResponseCode() === 200) {

  var tweets = Utilities.jsonParse(result.getContentText());

  if (tweets) {

    var len = tweets.length;

    var rss = "";

    if (len) {

      rss  = '<?xml version="1.0"?><rss version="2.0">';
      rss += ' <channel><title>Twitter ' + type + ': ' + key + '</title>';
      rss += ' <link>' + htmlentities ( json ) + '</link>';
      rss += ' <pubDate>' + new Date() + '</pubDate>';

      for (var i=0; i<len; i++) {

        var sender = tweets[i].user.screen_name;
        var tweet  = htmlentities ( tweets[i].text );

        rss += "<item><title>" + sender + ": " + tweet + "</title>";
        rss += " <author>" + tweets[i].user.name
                        + " (@" + sender + ")</author>";
        rss += " <pubDate>" + tweets[i].created_at + "</pubDate>";
        rss += " <guid isPermaLink='false'>" + tweets[i].id_str + "</guid>";
        rss += " <link>https://twitter.com/" + sender
                        + "/statuses/" + tweets[i].id_str + "</link>";
        rss += " <description>" + tweet + "</description>";
        rss += "</item>";
      }

      rss += "</channel></rss>";

      return rss;
    }
  }
}

} catch (e) { Logger.log(e.toString()); } }

function doGet(e) {

var a = e.parameter.action; var q = e.parameter.q;

var feed = ”https://api.twitter.com/1.1/”;

switch (a) { case “timeline”: feed += “statuses/user_timeline.json?screen_name=” + q; break; case “search”: feed += “search/tweets.json?q=” + encodeURIComponent(q); break; case “favorites”: feed += “favorites/list.json?screen_name=” + q; break; case “list”: var i = q.split(”/”); feed += “lists/statuses.json?slug=” + i[1] + “&owner_screen_name=” + i[0]; break; default: feed += “statuses/user_timeline.json”; break; }

var id = Utilities.base64Encode(feed);

var cache = CacheService.getPublicCache(); var rss = cache.get(id);

if ( ! rss ) { rss = JSONtoRSS ( feed, a, q ); cache.put(id, rss, 3600); }

return ContentService.createTextOutput(rss) .setMimeType(ContentService.MimeType.RSS); }



export const _frontmatter = {"title":"Convert Twitter JSON to RSS with Google Apps Script","date":"2017-03-19T18:35:22.000Z","slug":"/code/19433-twitter-json-to-rss","template":"post","category":"Code","tags":["Google Apps Script","Twitter"]}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Twitter

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
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
G

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