baptx

Twitter API 1.0 favorites/following/followers backup

Feb 1st, 2013
146
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Backup favorites from specified Twitter account with the first script, following and followers with the second one. Script can be executed on any HTML page with Firefox Scratchpad or any browser console. A textarea will appear so you can copy/paste retrieved data.
  2.  
  3. Replace Twitter screen_name to fit your needs.
  4. For the first script, replace API URL to choose between following and followers:
  5. Following (friends) API 1 URL: https://api.twitter.com/1/friends/ids.json
  6. Followers API 1 URL: https://api.twitter.com/1/followers/ids.json
  7. Those scripts are working with Twitter API 1.0, wich will be shut down on March 5, 2013.
  8. After that, only API 1.1 will be available and it will be impossible to do any request to Twitter API from client-side browser due to Twitter changes that require every API request to be authentified with a Twitter account.
  9. Yes, Twitter sucks, here you have independant scripts that you can execute directly from your browser and whenever you want to backup some Twitter data. You don't have to rely on third party, server-side solution that will be unavailable if servers are down or simply if the service close.
  10. So enjoy this for a few time and say thank you to @twitter for making this work useless.
  11.  
  12. Update: These scripts are deprecated and bugs have been fixed in the new version. You can access Twitter API 1.1 in browser http://pastebin.com/h8u3Fp9Q and backup favorites/following/followers http://pastebin.com/Zut93sr1
  13. */
  14.  
  15.  
  16. // Favorites backup using Twitter JSONP callback
  17.  
  18. var favs = [];
  19. var length = -1;
  20. var start = 0;
  21. var max_id = "";
  22. var res;
  23.  
  24. function getJSONP(data)
  25. {
  26.     res = data;
  27. }
  28.  
  29. (function loadAPI()
  30. {
  31.     if (length != 1)
  32.     {
  33.     var script = document.createElement("script");
  34.     script.setAttribute("src", "https://api.twitter.com/1/favorites.json?count=200&screen_name=baptx" + max_id + "&callback=getJSONP");
  35.     document.body.appendChild(script);
  36.     script.addEventListener("load", function() {
  37.         length = res.length;
  38.         for (var i = 0; i < length; i++)
  39.         favs[start + i] = res[i].created_at + "\t@" + res[i].user.screen_name + ": " + res[i].text + "\t" + "(tweet ID: " + res[i].id_str + ")";
  40.         max_id = "&max_id=" + res[i - 1].id_str;
  41.         start += length + 1;
  42.         loadAPI();
  43.     });
  44.     }
  45.     else
  46.     {
  47.     var box = document.createElement("textarea");
  48.     box.value = favs.join('\n');
  49.     document.body.appendChild(box);
  50.     }
  51. })();
  52.  
  53.  
  54. // Following/followers backup using Twitter JSONP callback
  55.  
  56. var res;
  57.  
  58. function getJSONP(data)
  59. {
  60.     res = data;
  61. }
  62.  
  63. var script = document.createElement("script");
  64. script.setAttribute("src", "https://api.twitter.com/1/friends/ids.json?screen_name=baptx&callback=getJSONP");
  65. document.body.appendChild(script);
  66. script.addEventListener("load", function() {
  67.     var ids = res.ids;
  68.     var length = ids.length;
  69.     var start = 0;
  70.  
  71.     (function loadAPI()
  72.      {
  73.      if (start < length)
  74.      {
  75.          var params = [];
  76.          if (length - start < 100)
  77.          var end = start + (length - start);
  78.          else
  79.          var end = start + 100;
  80.          for (var i = start; i < end; i++) {
  81.          params += ids[i];
  82.          if (i + 1 != end)
  83.              params += ',';
  84.          }
  85.          var script = document.createElement("script");
  86.          script.setAttribute("src", "https://api.twitter.com/1/users/lookup.json?user_id=" + params + "&callback=getJSONP");
  87.          document.body.appendChild(script);
  88.          script.addEventListener("load", function() {
  89.          var length = res.length;
  90.          for (var i = start; i < end; i++) {
  91.              for (var j = 0; j < length; j++) {
  92.              if (ids[i] == res[j].id)
  93.                  ids[i] = res[j].screen_name;
  94.              }
  95.          }
  96.          start = end;
  97.          loadAPI();
  98.          });
  99.      }
  100.      else
  101.      {
  102.          var box = document.createElement("textarea");
  103.          box.value = ids.join('\n');
  104.          document.body.appendChild(box);
  105.      }
  106.      })();
  107. });
  108.  
  109.  
  110. // Favorites backup using AJAX requests (limited by CORS)
  111.  
  112. var favs = [];
  113. var length = -1;
  114. var start = 0;
  115. var max_id = "";
  116. var xhr = new XMLHttpRequest();
  117. while (length != 1)
  118. {
  119.     xhr.open("GET", "https://api.twitter.com/1/favorites.json?count=200&screen_name=baptx" + max_id, false);
  120.     xhr.send();
  121.     if (xhr.status == 200)
  122.     var res = JSON.parse(xhr.responseText);
  123.         length = res.length;
  124.     for (var i = 0; i < length; i++)
  125.     favs[start + i] = res[i].created_at + "\t@" + res[i].user.screen_name + ": " + res[i].text + "\t" + "(tweet ID: " + res[i].id_str + ")";
  126.     max_id = "&max_id=" + res[i - 1].id_str;
  127.     start += length + 1;
  128. }
  129. var box = document.createElement("textarea");
  130. box.value = favs.join('\n');
  131. document.body.appendChild(box);
  132.  
  133.  
  134. // Following/followers backup using AJAX requests (limited by CORS))
  135.  
  136. var xhr = new XMLHttpRequest();
  137. xhr.open("GET", "https://api.twitter.com/1/friends/ids.json?screen_name=baptx", false);
  138. xhr.send();
  139. if (xhr.status == 200)
  140.     var res = JSON.parse(xhr.responseText);
  141. var ids = res.ids;
  142. var length = ids.length;
  143. var start = 0;
  144. while (start < length)
  145. {
  146.     var params = [];
  147.     if (length - start < 100)
  148.     var end = start + (length - start);
  149.     else
  150.     var end = start + 100;
  151.     for (var i = start; i < end; i++) {
  152.     params += ids[i];
  153.     if (i + 1 != end)
  154.         params += ',';
  155.     }
  156.     xhr.open("GET", "https://api.twitter.com/1/users/lookup.json?user_id=" + params, false);
  157.     xhr.send();
  158.     if (xhr.status == 200)
  159.     var res2 = JSON.parse(xhr.responseText);
  160.     var length2 = res2.length;
  161.     for (var i = start; i < end; i++) {
  162.     for (var j = 0; j < length2; j++) {
  163.         if (ids[i] == res2[j].id)
  164.         ids[i] = res2[j].screen_name;
  165.     }
  166.     }
  167.     start = end;
  168. }
  169. var box = document.createElement("textarea");
  170. box.value = ids.join('\n');
  171. document.body.appendChild(box);
RAW Paste Data