baptx

twitter_api_1.1_backup

Oct 5th, 2014 (edited)
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Twitter API 1.1 tweets / favorites (likes) / following / followers backup in web browser
  2.  * Get your access keys to use Twitter API 1.1: https://dev.twitter.com/docs/auth/tokens-devtwittercom
  3.  * You can change Twitter API URL and Twitter screen_name, then execute script from a trusted web page without CSP protection like about:blank in a web browser console (F12 or Ctrl+Shift+K shortcut)
  4.  * A textarea will appear so you can copy/paste to save data as a CSV file or search tweets / users in your web browser (Ctrl+F shortcut)
  5.  * You can then view your backup in a spreadsheet editor like LibreOffice Calc
  6.  * You can also compare the backup with another one to see who unfollowed you, who changed their Twitter username by looking at the user ID or which tweet you retweeted / favorited was deleted (e.g. with the Linux diff command)
  7.  *
  8.  * Note about the tweets backup:
  9.  * Usually you will search tweets that you retweeted using Twitter web version (https://twitter.com/search) with a search like "from:your_username filter:nativeretweets keyword"
  10.  * But it is limited to the retweets of the last 7 days, like for the free version of the search API (https://developer.twitter.com/en/docs/tweets/search/overview/standard)
  11.  * An alternative is to search tweets in your user timeline with this script but it is limited to your last 3200 tweets (including retweets and replies)
  12.  * This script can be combined with the Twitter feature to backup data, which is not limited to your last 3200 tweets but you can only request a backup every 30 days
  13.  * To find tweets that you retweeted or favorited / liked from a specific person, you can open the CSV file with LibreOffice Calc, click on the column you want to search and press Ctrl+H to search a username
  14. */
  15.  
  16.  
  17. var url = "https://api.twitter.com/1.1/statuses/user_timeline.json"
  18. //var url = "https://api.twitter.com/1.1/favorites/list.json";
  19. //var url = "https://api.twitter.com/1.1/friends/list.json";
  20. //var url = "https://api.twitter.com/1.1/followers/list.json";
  21.  
  22. var accessor = {
  23.     token: "YYY",
  24.     tokenSecret: "YYY",
  25.     consumerKey : "YYY",
  26.     consumerSecret: "YYY"
  27. };
  28. var message = {
  29.     action: url,
  30.     method: "GET",
  31.     parameters: {
  32.         screen_name: "jack",
  33.         count: 200,
  34.         callback: "getJSONP"
  35.     }
  36. };
  37.  
  38. var out = [];
  39. var length = -1;
  40. var start = 0;
  41. var next = -1;
  42. var res;
  43.  
  44. function getJSONP(data)
  45. {
  46.     res = data;
  47. }
  48.  
  49. function loadAPI_tweets()
  50. {
  51.     if (length != 1)
  52.     {
  53.         if (next != -1) {
  54.             message.parameters.max_id = next;
  55.         }
  56.         OAuth.completeRequest(message, accessor);
  57.         OAuth.SignatureMethod.sign(message, accessor);
  58.  
  59.         var script3 = document.createElement("script");
  60.         script3.setAttribute("src", url + '?' + OAuth.formEncode(message.parameters));
  61.         document.body.appendChild(script3);
  62.         script3.addEventListener("load", function() {
  63.             length = res.length;
  64.             for (var i = 0; i < length; ++i) {
  65.                 var text, name, id;
  66.                
  67.                 if (res[i].retweeted_status) {
  68.                     text = res[i].retweeted_status.full_text;
  69.                     name = res[i].retweeted_status.user.screen_name;
  70.                     id = res[i].retweeted_status.user.id;
  71.                 } else {
  72.                     text = res[i].full_text;
  73.                     name = res[i].user.screen_name;
  74.                     id = res[i].user.id;
  75.                 }
  76.                
  77.                 out[start + i] = res[i].created_at + '\t'
  78.                     + name + '\t"""'
  79.                     + id + '"""\t"' // backup user ID to track a user who deleted a tweet you retweeted / favorited (even if the user changed his username)
  80.                     + text.replace(/"/g, '""') + '"\t"""'
  81.                     + res[i].id_str + '"""';
  82.                 // CSV with tab separator: quote to allow multiline string; escape quote string delimiter with double quote; display large numbers correctly as string with triple quote
  83.             }
  84.             next = res[length - 1].id_str;
  85.             start += length - 1;
  86.             loadAPI_tweets();
  87.         });
  88.     }
  89.     else
  90.     {
  91.         displayData();
  92.     }
  93. }
  94.  
  95. function loadAPI_users()
  96. {
  97.     if (length == -1 || res.next_cursor_str != 0) {
  98.         message.parameters.cursor = next;
  99.         OAuth.completeRequest(message, accessor);
  100.         OAuth.SignatureMethod.sign(message, accessor);
  101.  
  102.         var script3 = document.createElement("script");
  103.         script3.setAttribute("src", url + '?' + OAuth.formEncode(message.parameters));
  104.         document.body.appendChild(script3);
  105.         script3.addEventListener("load", function() {
  106.             length = res.users.length;
  107.             for (var i = 0; i < length; ++i) {
  108.                 out[start + i] = res.users[i].screen_name + '\t'
  109.                     + res.users[i].name.replace(/"/g, '""') + '\t"""'
  110.                     + res.users[i].id + '"""';
  111.             }
  112.             next = res.next_cursor_str;
  113.             start += length;
  114.             loadAPI_users();
  115.         });
  116.     }
  117.     else {
  118.         displayData();
  119.     }
  120. }
  121.  
  122. function displayData()
  123. {
  124.     var box = document.createElement("textarea");
  125.     box.value = out.join('\n');
  126.     document.body.appendChild(box);
  127. }
  128.  
  129. var script = document.createElement("script");
  130. script.setAttribute("src", "https://pastebin.com/raw/HFjqYLdG"); // http://oauth.googlecode.com/svn/code/javascript/oauth.js (down)
  131. document.body.appendChild(script);
  132. script.addEventListener("load", function() {
  133. var script2 = document.createElement("script");
  134.     script2.setAttribute("src", "https://pastebin.com/raw/M0N8JKwf"); // http://pajhome.org.uk/crypt/md5/sha1.js
  135.     document.head.appendChild(script2);
  136.     script2.addEventListener("load", function() {
  137.         if (url == "https://api.twitter.com/1.1/statuses/user_timeline.json" || url == "https://api.twitter.com/1.1/favorites/list.json") {
  138.             message.parameters.include_rts = true; // include retweets
  139.             message.parameters.tweet_mode = "extended"; // needed to get full tweet after API change
  140.             loadAPI_tweets();
  141.         }
  142.         else {
  143.             loadAPI_users();
  144.         }
  145.     });
  146. });
  147.  
Add Comment
Please, Sign In to add comment