SHARE
TWEET

instagram-api_direct_messages_backup

baptx Sep 14th, 2019 (edited) 1,853 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Instagram API: view and backup direct messages from a web browser
  3.  
  4. 1) Log in on Instagram web version and go to your profile page
  5. (the home page will not work because it loads data when scrolling down and the direct messages will be displayed at the bottom of the page)
  6.  
  7. 2) Modify HTTP headers with a browser addon like Header Editor (https://addons.mozilla.org/en-US/firefox/addon/header-editor/)
  8.  
  9. Content-Security-Policy (response header on domain www.instagram.com): original data with https://i.instagram.com/ added to connect-src
  10. report-uri https://www.instagram.com/security/csp_report/; default-src 'self' https://www.instagram.com; img-src https: data: blob:; font-src https: data:; media-src 'self' blob: https://www.instagram.com https://*.cdninstagram.com https://*.fbcdn.net; manifest-src 'self' https://www.instagram.com; script-src 'self' https://instagram.com https://www.instagram.com https://*.www.instagram.com https://*.cdninstagram.com wss://www.instagram.com https://*.facebook.com https://*.fbcdn.net https://*.facebook.net 'unsafe-inline' 'unsafe-eval' blob:; style-src 'self' https://*.www.instagram.com https://www.instagram.com 'unsafe-inline'; connect-src 'self' https://instagram.com https://www.instagram.com https://i.instagram.com https://*.www.instagram.com https://graph.instagram.com https://*.graph.instagram.com https://*.cdninstagram.com https://api.instagram.com wss://www.instagram.com wss://edge-chat.instagram.com https://*.facebook.com https://*.fbcdn.net https://*.facebook.net chrome-extension://boadgeojelhgndaghljhdicfkmllpafd blob:; worker-src 'self' blob: https://www.instagram.com; frame-src 'self' https://instagram.com https://www.instagram.com https://staticxx.facebook.com https://www.facebook.com https://web.facebook.com https://connect.facebook.net https://m.facebook.com; object-src 'none'; upgrade-insecure-requests
  11.  
  12. User-Agent (request header on domain i.instagram.com)
  13. Instagram 85.0.0.21.100 Android (23/6.0.1; 538dpi; 1440x2560; LGE; LG-E425f; vee3e; en_US)
  14.  
  15. 3) Run the following JavaScript code in the web console of your browser or in a tool like Firefox scratchpad.
  16. The function InstagramDMthreads should be used first, then you have to comment the call to it and uncoment the call to InstagramDMmessages,
  17. which takes a thread ID as parameter (found with the previous function InstagramDMthreads).
  18. A textarea will appear to the bottom of the page so you can copy-paste to backup data (if you don't see it, you have to close the web console or scroll down).
  19.  
  20. If you want to send direct messages without using Android / iOS and the official proprietary app, you can use open source desktop apps like IG:dm, do cURL requests to the API or use my web browser script instagram-api_send_message.js.
  21. */
  22.  
  23. /* Usage */
  24. //InstagramDMthreads();
  25. //InstagramDMmessages("XXX"); // parameter needs to be a string since it is too big for an integer
  26.  
  27. /* CONFIG */
  28. var backup = true; // if backup is set to false, only the last threads and messages are displayed, in browser console instead of textarea (number can be configured in limit variable)
  29. var limit = 100; // 20 threads and messages by default, per API request
  30. /* END CONFIG */
  31.  
  32. var start;
  33. var data;
  34. var usernames;
  35.  
  36. // backup will return: thread ID, date + time + timezone, userid, username, last message
  37. function InstagramDMthreads()
  38. {
  39.     initVariables(); // clear variables to allow reusing functions several time
  40.    
  41.     var xhr = new XMLHttpRequest();
  42.     xhr.open("GET", "https://i.instagram.com/api/v1/direct_v2/inbox/" + "?limit=" + limit);
  43.     xhr.withCredentials = true;
  44.     xhr.addEventListener("load", function() {
  45.         var response = JSON.parse(xhr.responseText);
  46.         if (backup) {
  47.             scrollInbox(response);
  48.         }
  49.         else {
  50.             parseInbox(response);
  51.             console.log(data);
  52.         }
  53.     });
  54.     xhr.send();
  55. }
  56.  
  57. function parseInbox(response)
  58. {
  59.     var length = response.inbox.threads.length;
  60.     for (var i = 0; i < length; ++i) {
  61.         // length is 0 for conversations with yourself
  62.         var userid = response.inbox.threads[i].users.length != 0 ? response.inbox.threads[i].users[0].pk : response.viewer.pk;
  63.         var username = response.inbox.threads[i].users.length != 0 ? response.inbox.threads[i].users[0].username : response.viewer.username;
  64.         data[start + i] = '"""' + response.inbox.threads[i].thread_id + '"""\t'
  65.             + new Date(response.inbox.threads[i].last_activity_at / 1000).toString() + '\t'
  66.             + userid + '\t'
  67.             + username + '\t'
  68.             + '"' + getMessageText(response.inbox.threads[i].items[0]).replace(/"/g, '""') + '"';
  69.             // allow multiline CSV string with tab separator and escape double quote string delimiter
  70.     }
  71.     start += length;
  72. }
  73.  
  74. function scrollInbox(response)
  75. {
  76.     parseInbox(response);
  77.    
  78.     var cursor = response.inbox.oldest_cursor;
  79.    
  80.     if (cursor) {
  81.         var xhr = new XMLHttpRequest();
  82.         xhr.open("GET", "https://i.instagram.com/api/v1/direct_v2/inbox/?cursor=" + cursor + "&limit=" + limit);
  83.         xhr.withCredentials = true;
  84.         xhr.addEventListener("load", function() {
  85.             var response = JSON.parse(xhr.responseText);
  86.             scrollInbox(response);
  87.         });
  88.         xhr.send();
  89.     }
  90.     else {
  91.         displayData();
  92.     }
  93. }
  94.  
  95. // backup will return: date + time + timezone, username, message
  96. function InstagramDMmessages(thread)
  97. {
  98.     initVariables(); // clear variables to allow reusing functions several time
  99.    
  100.     var xhr = new XMLHttpRequest();
  101.     xhr.open("GET", "https://i.instagram.com/api/v1/direct_v2/threads/" + thread + "/" + "?limit=" + limit);
  102.     xhr.withCredentials = true;
  103.     xhr.addEventListener("load", function() {
  104.         var response = JSON.parse(xhr.responseText);
  105.        
  106.         var xhr2 = new XMLHttpRequest();
  107.         xhr2.open("GET", "https://i.instagram.com/api/v1/users/" + response.thread.viewer_id + "/info/");
  108.         xhr2.withCredentials = true;
  109.         xhr2.addEventListener("load", function() {
  110.             var response2 = JSON.parse(xhr2.responseText);
  111.            
  112.             usernames[response.thread.viewer_id] = response2.user.username;
  113.             usernames[response.thread.users[0].pk] = response.thread.users[0].username;
  114.            
  115.             if (backup) {
  116.                 scrollThread(thread, response);
  117.             }
  118.             else {
  119.                 parseThread(thread, response);
  120.                 console.log(data);
  121.             }
  122.         });
  123.         xhr2.send();
  124.     });
  125.     xhr.send();
  126. }
  127.  
  128. function parseThread(thread, response)
  129. {
  130.     var length = response.thread.items.length;
  131.     for (var i = 0; i < length; ++i) {
  132.         data[start + i] = new Date(response.thread.items[i].timestamp  / 1000).toString() + '\t'
  133.             + usernames[response.thread.items[i].user_id] + '\t'
  134.             + '"' + getMessageText(response.thread.items[i]) + '"';
  135.     }
  136.     start += length;
  137. }
  138.  
  139. function scrollThread(thread, response)
  140. {
  141.     parseThread(thread, response);
  142.    
  143.     if (response.thread.prev_cursor != "MINCURSOR") {
  144.         var cursor = response.thread.oldest_cursor;
  145.        
  146.         var xhr = new XMLHttpRequest();
  147.         xhr.open("GET", "https://i.instagram.com/api/v1/direct_v2/threads/" + thread + "/?cursor=" + cursor + "&limit=" + limit);
  148.         xhr.withCredentials = true;
  149.         xhr.addEventListener("load", function() {
  150.             var response = JSON.parse(xhr.responseText);
  151.             scrollThread(thread, response);
  152.         });
  153.         xhr.send();
  154.     }
  155.     else {
  156.         displayData();
  157.     }
  158. }
  159.  
  160. function getMessageText(item)
  161. {
  162.     var type = item.item_type;
  163.     var text;
  164.    
  165.     switch (type) {
  166.         case "text":
  167.             text = item.text;
  168.             break;
  169.         case "link": // used if the message contains a link
  170.             text = item.link.text;
  171.             break;
  172.         case "action_log": // used when someone likes a message
  173.             text = item.action_log.description;
  174.             break;
  175.         case "like": // used when someone sends a like
  176.             text = item.like;
  177.             break;
  178.         case "media":
  179.             if (item.media.media_type == 1) {
  180.                 text = item.media.image_versions2.candidates[0].url;
  181.             }
  182.             else { // media_type is 1 for images and 2 for videos
  183.                 text = item.media.video_versions[0].url;
  184.             }
  185.             break;
  186.         case "animated_media": // for GIF files
  187.             text = item.animated_media.images.fixed_height.url;
  188.             break;
  189.         case "voice_media": // for audio recordings
  190.             text = item.voice_media.media.audio.audio_src;
  191.             break;
  192.     }
  193.    
  194.     return text;
  195. }
  196.  
  197. function initVariables()
  198. {
  199.     start = 0;
  200.     data = [];
  201.     usernames = [];
  202. }
  203.  
  204. function displayData()
  205. {
  206.     var box = document.createElement("textarea");
  207.     box.value = data.join("\n");
  208.     document.body.appendChild(box);
  209. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top