Export Twitter Followers and Friends using a Google Spreadsheet

Published on March 8, 2011 at 1:57 pm in Google, Google Apps, Google Apps Script, Google Spreadsheet and Twitter. 142 Comments By Martin Hawksey
Share this post on:
| Facebook | Twitter | LinkedIn

Note: Twitter recently wrote to me reminding me that

methods of obtaining and exporting Twitter content obtained via the Twitter API is prohibited by section I.4.A of our API Terms of Service (https://dev.twitter.com/terms/api-terms ), specifically:

You will not attempt or encourage others to sell, rent, lease, sublicense, redistribute, or syndicate access to the Twitter API or Twitter Content to any third party without prior written approval from Twitter. If you provide an API that returns Twitter data, you may only return IDs (including tweet IDs and user IDs). You may export or extract non-programmatic, GUI-driven Twitter Content as a PDF or spreadsheet by using “save as” or similar functionality. Exporting Twitter Content to a datastore as a service or other cloud based service, however, is not permitted.

Following clarification from Twitter I making this template available again but please note.

By using this template you agree it is for personal use only and the data is not made publicly available.

Recently I’ve noticed a growing number of people arrive at this blog having searched for ‘export twitter followers’. Rather than them leaving disappointed here’s a Google Spreadsheet I threw together which allows you to grab a copy of your friends/followers:

*** Google Spreadsheet to Export Twitter Friends and Followers ***

Benefits of using Google Spreadsheet

  • Control – You register for your own API key with Twitter so you have full control of the account
  • Playing with the data – as you are importing straight into a spreadsheet you can do all of your own data manipulation like sorting, filtering and creating your own formula for things like follow/follower ratios
  • Backup – Google Spreadsheets allow you to download copies of spreadsheets in different formats
  • Share – You can make your lists of friends/followers easily viewable

Where’s this all going?

Having already done other things with the Twitter API and Google Spreadsheets (See Populating a Twitter List via Google Spreadsheet … Automatically!, Collect/backup tweets in a Google Spreadsheet, Google Apps Script, Spreadsheets, Twitter and Gadgets) the Twitter/Google Spreadsheet back is well and truly broken. You’ll probably see fewer posts one this area with new stuff instead I’ll probably start properly documenting the little code snippets I use (but if you have any interesting ideas you want help with get in touch).

This doesn’t mean I’ll be walking away from Google Spreadsheets. As recent posts like Turning Google Spreadsheets into a personal or group bookmarking service, show there is huge scope in using Spreadsheets as a very flexible rapid development platform.

Below are some bits of the code used in my new spreadsheet (all the code is viewable via the Script Editor in the Spreadsheet):

function tw_request(method, api_request){
  // general purpose function to interact with twitter API
  // for method and api_request doc see dev.twitter.com/doc/
  // retuns object
  var oauthConfig = UrlFetchApp.addOAuthService("twitter");
  oauthConfig.setAccessTokenUrl(
      "https://api.twitter.com/oauth/access_token");
  oauthConfig.setRequestTokenUrl(
      "https://api.twitter.com/oauth/request_token");
  oauthConfig.setAuthorizationUrl(
      "https://api.twitter.com/oauth/authorize");
  oauthConfig.setConsumerKey(getConsumerKey());
  oauthConfig.setConsumerSecret(getConsumerSecret());
  var requestData = {
        "method": method,
        "oAuthServiceName": "twitter",
        "oAuthUseToken": "always"
      };
   try {
      var result = UrlFetchApp.fetch(
          "https://api.twitter.com/1/"+api_request,
          requestData);
      var o  = Utilities.jsonParse(result.getContentText());
    } catch (e) {
      Logger.log(e);
    }
   return o;
}

function getFriendAndFo(sheetName){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetName);
  sheet.getRange(2, 1, sheet.getLastRow(), sheet.getMaxColumns()).clear({contentsOnly:true}); //clear sheet
  var cursor = "-1";
  while(cursor != "none"){ // while twitter returns data loop
    try {
      var o = tw_request("GET", "statuses/"+sheetName+".json?cursor="+cursor); // note using sheetname to build api request
      var data = o.users;
      for (i in data){ // extracting some subobjects to top level (makes it easier to setRowsData)
        if (data[i].status){
          for (j in data[i].status){
            data[i]["status_"+j] = data[i].status[j];
          }
        }
        if (data[i].screen_name){ // also build url to jump to profile page
          data[i]["profile_link"] = "twitter.com/"+data[i].screen_name;
        }
      }
      var headRange = sheet.getRange(1, 1, 1, sheet.getMaxColumns());
      var rowIndex = sheet.getLastRow()+1;
      setRowsData(sheet, data, headRange, rowIndex); // dump data for this loop to sheet
      if (o.next_cursor!="0"){
        cursor = o.next_cursor; // get next cursor
      } else {
        cursor = "none"; // break
      }
    }  catch (e) {
      Logger.log(e);
    }

  }
}

Last updated by Martin Hawksey at .

Share this post on:
| Facebook | Twitter | LinkedIn

142 Responses to “Export Twitter Followers and Friends using a Google Spreadsheet”

Feed for this Entry Trackback Address

« Previous 1 2
  • spacer Winton
    February 27, 2012 at 3:33 am

    Amazing! saved me a whole lot of time. Thanks!

    Reply
  • How to map an influencer's network? - ident{een}ityident{een}ity
    Pingback on Mar 1st, 2012 at 11:30 am
  • spacer Aaron S
    March 7, 2012 at 10:30 pm

    Jon Thomas (@Story_Jon) You rock! The call back URL was the fix to help me authorize the app.

    Reply
  • 6 maneras de saber lo que internet dice de ti | Marketing y Consumo
    Pingback on Mar 12th, 2012 at 9:32 am
  • 6 maneras de saber lo que internet dice de ti | Marketing y Consumo
    Pingback on Mar 12th, 2012 at 9:32 am
  • spacer Morten
    March 13, 2012 at 9:03 pm

    Hey, did you ever fix so you can download more than 5000 tweets? Mine isn’t working either, but great job making this, tho :)

    Reply
    • spacer Martin Hawksey
      March 15, 2012 at 9:57 pm

      @Morten it should be able to work with over 5000 but you need to configure the getMoreFriendOrFo() function in the script editor – there’s documentation on my site somewhere ;-s

      Reply
  • Notes on extracting the JISC CETIS twitter follower network – MASHe
    Pingback on Mar 30th, 2012 at 5:03 pm
  • Linklove Boston 2012 Recap | distilled
    Pingback on Apr 10th, 2012 at 11:02 am
  • My Not-So Successful Attempts To Visualise a Twitter User | Cast Digital Sandbox Process
    Pingback on Apr 11th, 2012 at 6:10 pm
  • spacer John
    April 12, 2012 at 1:38 pm

    This is a very helpful tool.

    For my study, I need to have not only the friends and followers of a specific screen name but also the ‘friend’ and ‘follower’ connections between those. Does anyone know of a script that will download this type of data from Twitter? I know that Is there a NodeXL provides this option but, unfortunately, it is extremely slow…

    Any help or guidance with this will be greatly appreciated.

    Thanks.

    John

    Reply
    • spacer Martin Hawksey
      April 12, 2012 at 4:13 pm

      Hi John – unfortunately because of the volume of data required it’s always going to be slow. Tony Hirst has a python script for this soft of thing but he’s whitelisted so can get 10k+ api calls an hour. If you don’t want to leave your PC on all day an alternative might be using scraperwiki https://scraperwiki.com/scrapers/username_twitter_followers_id_manual_row_w_batc_10/
      Martin

      Reply
  • Optimize Your Link Building with Twitter
    Pingback on Apr 18th, 2012 at 2:01 pm
  • spacer Steve
    April 20, 2012 at 6:34 pm

    Hi! When I try to load the Twitter menu, I get an error message Script function onOpen could not be found. Any suggestions? Thanks.

    Reply
    • spacer Martin Hawksey
      April 20, 2012 at 7:51 pm

      Try making a fresh copy

      Reply
      • spacer Steve
        April 20, 2012 at 10:59 pm

        Yep, did that. More than once. Tried it in IE and Opera using different google accounts and get the same message.

        Reply
        • spacer Martin Hawksey
          April 21, 2012 at 9:07 am

          There appeared to be temporary server issues with Apps Script in the last 24 hours

          Reply
  • Devious Media – Devel » 5 Essential Spreadsheets for Social Media Analytics
    Pingback on Apr 20th, 2012 at 7:34 pm
  • spacer abdul
    April 24, 2012 at 7:46 am

    Thanks for this great tool
    it really helped me a lot in colecting data. However, I’ve tried get other person follower but the max it could get is 5000 then i tried to do it again for the same person but i couldn’t. Plus, i’m afraid even if it did, it’ll get the same first 5000 again which wont be very helpful for me.
    could you plz let me know if there’s another way around it so i can get all the followers of another person
    again thank you for this powerful tool

    Reply