Export Tweets from Twitter with Google Apps Script

E
Published in: Google Apps Script - Twitter

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;
}
📮  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