Export Tweets from Twitter with Google Apps Script

The Twitter Archiver app will archive tweets for any hashtag to a Google Spreadsheet using the Twitter API and Google Apps Script.

You can set a time-based trigger to run downloadTweets() every 5 minutes or even 1 minute for #hashtags that are extremely popular and generate thousands of tweets. The code has been updated to using the OAuth1 library instead of the OAuthConfig service which has since been deprecated.


function downloadTweets(searchTerm) {

    var twitterService = getTwitterService_();

    var props = PropertiesService.getUserProperties();
    var sinceID = props.getProperty("SINCEID") || "";

    var api = "https://api.twitter.com/1.1/search/tweets.json?count=100&include_entities=false";
    api += "&result_type=recent&q=" + encodeString_(searchTerm) + "&since_id=" + sinceID;

    var result = twitterService.fetch(api);

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

        var json = JSON.parse(result.getContentText());

        var tweets = json.statuses;

        // SINCEID will store the ID of the last processed tweet

        for (var i = tweets.length - 1; i >= 0; i--) {
            logTweet_(tweets[i]);
            if (i == 0) {
                props.setProperty("SINCEID", tweets[0].id_str);
            }
        }
    }
}

/* Add the tweet details into the sheet */
function logTweet_(tweet) {

    var log = [];

    log.push(new Date(tweet.created_at));
    log.push('=HYPERLINK("https://twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id_str + '","' + tweet.user.name + '")');
    log.push(tweet.user.followers_count);
    log.push(tweet.user.friends_count);
    log.push(tweet.retweet_count);
    log.push(tweet.favorite_count);
    log.push(tweet.text.replace(/\n|\r/g, " "));

    SpreadsheetApp.getActiveSheet().appendRow(log)
}


function getTwitterService_() {

    var props = PropertiesService.getUserProperties();

    return OAuth1.createService('twitter')
        .setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
        .setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
        .setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
        .setConsumerKey(props.getProperty("consumer_key"))
        .setConsumerSecret(props.getProperty("consumer_secret"))
        .setProjectKey(ScriptApp.getProjectKey())
        .setCallbackFunction('twitter')
        .setPropertyStore(props);

}


/* Properly encode the Twitter search query */
function encodeString_(q) {
    var str = encodeURIComponent(q);
    str = str.replace(/!/g, '%21');
    str = str.replace(/\*/g, '%2A');
    str = str.replace(/\(/g, '%28');
    str = str.replace(/\)/g, '%29');
    str = str.replace(/'/g, '%27');
    return str;
}