KiberInfinity

vk msg dump render

Sep 23rd, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. vkopt['messages'] = {
  3.    html_tpl:'<!DOCTYPE html>\
  4.   <html>\
  5.      <head><meta charset="utf-8"/><link rel="shortcut icon" href="http://vk.com/images/fav_chat.ico"/><link rel="stylesheet" type="text/css" href="http://vk.com/css/al/common.css" /><title>%title</title><style>\
  6.      body{text-align:center;font:12px/16px Verdana;margin:5px;}\
  7.      hr{border-color:#C3D1E0;}\
  8.      .messages{width:950px;margin:0 auto;text-align:left;} .msg_item {overflow:hidden} .from,.msg_body,.att_head,.attacments,.attacment,.fwd{margin-left:80px;}\
  9.      .upic{float:left} .upic img{vertical-align:top;width:70px;padding:5px;height:70px;}\
  10.      a,a:visited{text-decoration:none;color:#2B587A} a:hover{text-decoration:underline} .att_head{color:#777;}\
  11.      .att_ico{float:left;width:11px;height:11px;margin: 3px 3px 2px; background-image:url(\'http://vk.com/images/icons/mono_iconset.gif\');}\
  12.      .att_photo{background-position: 0 -30px;} .att_audio{background-position: 0 -222px;} .att_video{background-position: 0 -75px;}\
  13.      .att_doc{background-position: 0 -280px;} .att_wall,.att_fwd{background-position: 0 -194px;} .att_gift{background-position: 0 -105px;} \
  14.      .att_sticker{background-position: 0 -362px; width: 12px; height: 12px;}\
  15.      .att_link{background-position: 0 -237px;} .attb_link a span{color:#777777 !important;} .att_geo{background-position: 0 -165px;}\
  16.      .fwd{border:2px solid #C3D1E0;border-width: 0 0 0 2px;margin-left:85px;}\
  17.      </style></head>\
  18.      <body><div class="messages">%messages_body</div></body>\
  19.   </html>',
  20.    make_html: function(msg,user){
  21.       var html='';
  22.       var t2d = function(unix){
  23.          var time = new Date(unix*1000);
  24.          return time.getFullYear()+'.'+('0'+(time.getMonth()+1)).slice(-2)+'.'+('0'+time.getDate()).slice(-2)+' '+('0'+time.getHours()).slice(-2)+':'+('0'+time.getMinutes()).slice(-2)+':'+('0'+time.getSeconds()).slice(-2);
  25.       };
  26.       var t2m = function(inputText) {
  27.          var replacedText,replacePattern2,replacePattern3;
  28.          //add break
  29.          replacedText = replaceEntities(inputText).replace(/&/g,'&amp;').replace(/</g, '&lt;').replace(/>/g,'&gt;').replace(/\n/g,'<br />').replace(/"/g, '&quot;');
  30.  
  31.             /*
  32.                            replacedText.replace(/&#(\d\d+);/g,function(s, c) {
  33.                                   c=replaceEntities('&#'+c+';')
  34.                                   return c.replace(/</g, '&lt;').replace(/>/g,'&gt;').replace(/\n/g,'<br />').replace(/"/g, '&quot;');
  35.                            }
  36.             */
  37.           //URLs starting with http://, https://, or ftp://
  38.           replacePattern2 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\\/%?=~_|!:,.;\u0410-\u042f\u0430-\u044f\u0401\u0451]*[-A-Z0-9+&@#\/%=~_|\u0410-\u042f\u0430-\u044f\u0401\u0451])/gim; // /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;А-Яа-яЁё]*[-A-Z0-9+&@#\/%=~_|А-Яа-яЁё])/gim;
  39.           replacedText = replacedText.replace(replacePattern2, '<a href="$1" target="_blank">$1</a>');
  40.           //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
  41.           replacePattern3 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
  42.           replacedText = replacedText.replace(replacePattern3,'$1<a href="http://$2" target="_blank">$2</a>');
  43.  
  44.          if (window.Emoji && Emoji.emojiToHTML)
  45.             replacedText = Emoji.emojiToHTML(replacedText,true).replace(/"\/images\//g,'"http://vk.com/images/') || replacedText;
  46.  
  47.           return replacedText;
  48.       };
  49.         var doc2text=function(t){
  50.             // проверка < и > в именах документов
  51.             return t.replace(/</g, '&lt;').replace(/>/g,'&gt;').replace(/"/g, '&quot;').replace(/&/g,'&amp;');
  52.         };
  53.       var a2t = function(sec){
  54.          return Math.floor(sec/60)+':'+('0'+(sec%60)).slice(-2);
  55.       };
  56.       var chatAction=function(action_name){
  57.          switch(action_name){
  58.             case 'chat_photo_update':
  59.                html='<div style="color:#888888;">'+IDL('HistMsgChatAvaUpd')+'</div>';
  60.                break;
  61.             case 'chat_photo_remove':
  62.                html='<div style="color:#888888;">'+IDL('HistMsgChatAvaDel')+'</div>';
  63.                break;
  64.             default:
  65.                html='<div>action "<b>'+action_name+'</b>" is unknown</div>'
  66.          }
  67.          return html;
  68.       };
  69.       var make_attach=function(attach){
  70.          var html='';
  71.          if (!attach[attach.type]){
  72.             //console.log('Attach broken?', attach);
  73.             attach[attach.type]={};
  74.          }
  75.          switch (attach.type){
  76.             case 'photo':
  77.                var photolink=attach.photo.photo_2560 || attach.photo.photo_1280 || attach.photo.photo_807 || attach.photo.photo_604;
  78.                var photo_size=attach.photo.width ? ' ('+attach.photo.width+'x'+attach.photo.height+')' : '';
  79.                html+='<div class="attacment"> <div class="att_ico att_photo"></div> <a target="_blank" href="'+photolink+'">[photo'+attach.photo.owner_id+'_'+attach.photo.id+']'+photo_size+'</a> </div>';
  80.                break;
  81.             case 'audio':
  82.                var url = attach.audio.url;
  83.                if (!url){
  84.                   url = 'http://vk.com/audio?q='+encodeURIComponent(attach.audio.artist+' - '+attach.audio.title);
  85.                }
  86.                html+='<div class="attacment"> <div class="att_ico att_audio"></div> <a target="_blank" href="'+url+'">[audio'+attach.audio.owner_id+'_'+attach.audio.id+'] '+doc2text(attach.audio.artist)+' - '+doc2text(attach.audio.title)+' ('+a2t(attach.audio.duration)+')</a></div>';
  87.                break;
  88.             case 'video':
  89.                html+='<div class="attacment"> <div class="att_ico att_video"></div> <a href="http://vk.com/video'+attach.video.owner_id+'_'+attach.video.id+'" target="_blank">[video'+attach.video.owner_id+'_'+attach.video.id+'] '+doc2text(attach.video.title)+' ('+a2t(attach.video.duration)+')</a></div>';
  90.                break;
  91.             case 'doc':
  92.                html+='<div class="attacment"> <div class="att_ico att_doc"></div> <a target="_blank" href="'+attach.doc["url"].replace(/&/g,'&amp;')+'">'+doc2text(attach.doc.title)+'</a></div>';
  93.                break;
  94.             case 'wall':
  95.                html+='<div class="attacment"> <div class="att_ico att_wall"></div> <a target="_blank" href="http://vk.com/wall'+attach.wall.to_id+'_'+attach.wall.id+'">[wall'+attach.wall.to_id+'_'+attach.wall.id+']</a></div>';
  96.                break;
  97.             case 'link':
  98.                html+='<div class="attacment attb_link"> <div class="att_ico att_link"></div> <a href="'+attach.link.url+'" target="_blank"><span>'+IDL('HistMsgAttachLink')+'</span> '+doc2text(attach.link.title)+'</a></div>';
  99.                break;
  100.             case 'gift':
  101.                html+='<div class="attacment"> <div class="att_ico att_gift"></div> <a target="_blank" href="'+attach.gift.thumb_256+'">'+IDL('HistMsgAttachGift')+' #'+attach.gift.id+'</a></div>';
  102.                break;
  103.             case 'sticker':
  104.                html+='<div class="attacment"> <div class="att_ico att_sticker"></div> <a target="_blank" href="'+attach.sticker.photo_256+'">'+IDL('HistMsgAttachSticker')+' #'+attach.sticker.id+'</a></div>';
  105.                break;
  106.             default:
  107.                html+=JSON.stringify(attach);
  108.                //console.log(attach.type+' is unknown');
  109.          }
  110.          return html;
  111.       };
  112.         var make_geo=function(m){
  113.             var html='';
  114.          html+='<div class="attacment"> <div class="att_ico att_geo"></div> <a href="https://maps.google.ru/maps?q='+m.geo['coordinates']+'" target="_blank">'+IDL('HistMsgGeoAttach')+' '+(m.geo['place'] || {'title':'---'})['title']+'</a></div>';
  115.             return html;
  116.         };
  117.  
  118.       // write data
  119.       html+='<hr>';
  120.       html+='<div> '+IDL('HistMsgDates').replace(/%start/g,t2d(msg[0].date)).replace(/%end/g,t2d(msg[msg.length-1].date))+' </div>';
  121.       html+='<div> '+IDL('HistMsgCount').replace(/%count/g,msg.length)+' </div>';
  122.       html+='<hr>';
  123.  
  124.       // icons
  125.       // http://vk.com/images/icons/mono_iconset.gif
  126.  
  127.       // build
  128.       for(var i=0,j=msg.length;i<j;i++){
  129.          var u=(user[msg[i].user_id] || {
  130.                            id: msg[i].user_id,
  131.                            first_name: 'DELETED',
  132.                            last_name: '',
  133.                            photo_100: 'http://vk.com/images/deactivated_c.gif'
  134.                         } );
  135.          html+='<div id="msg'+msg[i].id+'" class="msg_item">';
  136.          html+='<div class="upic"><img src="'+
  137.          u.photo_100+
  138.          '" alt="[photo_100]"></div>';
  139.          html+='<div class="from"> <b> <a href="http://vk.com/id'+msg[i].user_id+'" target="_blank">'+u.first_name+' '+u.last_name+'</a></b> @ <a href="#msg'+msg[i].id+'">'+t2d(msg[i].date)+'</a></div>';
  140.          if(msg[i].body != ""){
  141.                html+='<div class="msg_body">'+t2m(msg[i].body)+'</div>';
  142.          }
  143.          if(msg[i].action){
  144.             html+=chatAction(msg[i].action);
  145.          }
  146.          if(msg[i].attachments !== undefined){
  147.             html+='<div class="attacments"> <b>'+IDL('HistMsgAttachments')+'</b> </div>';
  148.             var l=msg[i].attachments.length;
  149.             for(var k=0;k<l;k++){
  150.                html+=make_attach(msg[i].attachments[k]);
  151.             }
  152.          }
  153.          //геолокаци
  154.          if(msg[i].geo !== undefined)
  155.             html+=make_geo(msg[i]);
  156.  
  157.          if(msg[i].fwd_messages !== undefined){
  158.             initfwd(msg[i].fwd_messages);
  159.          }
  160.          html+='</div>';
  161.       }
  162.       html+='<hr>';
  163.  
  164.       function initfwd(msgfwd){
  165.          html+='<div class="att_head"> <div class="att_ico att_fwd"></div> '+IDL('HistMsgFwd')+' </div>';
  166.          html+='<div class="fwd">';
  167.          for(var k=0,l=msgfwd.length;k<l;k++){
  168.             var u = (user[msgfwd[k].user_id] || {
  169.                            id: msgfwd[k].user_id,
  170.                            first_name: 'DELETED',
  171.                            last_name: '',
  172.                            photo_100: 'http://vk.com/images/deactivated_c.gif'
  173.                         } );
  174.             html+='<div class="msg_item">';
  175.             html+='<div class="upic"><img src="'+
  176.             u.photo_100+
  177.             '" alt="[photo_100]"></div>';
  178.             html+='<div class="from"> <b> <a href="http://vk.com/id'+msgfwd[k].user_id+'" target="_blank">'+u.first_name+' '+u.last_name+'</a></b> @ '+t2d(msgfwd[k].date)+'</div>';
  179.             html+='<div class="msg_body"> '+t2m(msgfwd[k].body)+'</div>';
  180.             if(msgfwd[k].attachments !== undefined){
  181.                html+='<div class="attacments"> <b>'+IDL('HistMsgAttachments')+'</b> </div>';
  182.                var n=msgfwd[k].attachments.length;
  183.                for(var m=0;m<n;m++){
  184.                   html+=make_attach(msgfwd[k].attachments[m]);
  185.                }
  186.             }
  187.             if(msgfwd[k].geo !== undefined)
  188.                html+=make_geo(msgfwd[k]);
  189.  
  190.             if(msgfwd[k].fwd_messages !== undefined){
  191.                initfwd(msgfwd[k].fwd_messages);
  192.             }
  193.             html+='</div>';
  194.          }
  195.          html+='</div>';
  196.       }
  197.       return html;
  198.    },
  199.    export_data: function(messages){
  200.       var users_ids = [];
  201.       var history_uids={};
  202.       var collect_users=function(arr){
  203.          for (var i=0; i<arr.length; i++){
  204.             var msg=arr[i];
  205.             //console.log(msg)
  206.             if (msg.from_id) history_uids[msg.from_id]='1';
  207.             if (msg.user_id) history_uids[msg.user_id]='1';
  208.             if (msg.from_id && users_ids.indexOf(msg.from_id)==-1) users_ids.push(msg.from_id);
  209.             if (msg.user_id && users_ids.indexOf(msg.user_id)==-1) users_ids.push(msg.user_id);
  210.             if (msg.fwd_messages)
  211.                collect_users(msg.fwd_messages);
  212.                //for (var i=0; i<msg.fwd_messages.length; i++)
  213.          }
  214.       };
  215.       collect_users(messages);
  216.       var ldr = ge('saveldr');
  217.       if (ldr){
  218.          var w=getSize(ge('saveldr'),true)[0];
  219.          ldr.innerHTML=vkProgressBar(0,100,w,'Users data... %');
  220.       }
  221.       dApi.call('users.get',{user_ids:users_ids.join(','),fields:'photo_100',v:'5.5'},function(r){
  222.          ldr && (ldr.innerHTML = vkProgressBar(90,100,w,'Users data... %'));
  223.          var usrs=r.response;
  224.          var users={};
  225.          for (var i=0; i<usrs.length; i++)
  226.             users[usrs[i].id]=usrs[i];
  227.          for (var i=0; i<users_ids.length; i++)
  228.             if (!users[users_ids[i]])
  229.                users[users_ids[i]]={
  230.                   id: users_ids[i],
  231.                   first_name: 'DELETED',
  232.                   last_name: '',
  233.                   photo_100: 'http://vk.com/images/deactivated_c.gif'
  234.                };
  235.  
  236.          var html=vkopt.messages.make_html(messages, users);
  237.          html=vkopt.messages.html_tpl.replace(/%messages_body/g,html);
  238.          ldr && (ldr.innerHTML=vkProgressBar(100,100,w,'Users data... %'));
  239.          show('save_btn_text');
  240.          hide('saveldr');
  241.  
  242.          var file_name=[];
  243.          for (var key in users){
  244.             var uid=parseInt(key || '0');
  245.             if (history_uids[key] && !(window.vk && uid==vk.id)) file_name.push(users[key].first_name+" "+users[key].last_name+'('+uid+')');
  246.          }
  247.  
  248.          html=html.replace(/%title/g,'VK Messages: '+file_name.join(','));
  249.          vkopt.messages.save_file(html,"messages_"+vkCleanFileName(file_name.join(',')).substr(0,250)+".html");
  250.          //vkSaveText();
  251.       });
  252.    },
  253.    save_file: function(data, filename){
  254.       vkLdr.show();
  255.       FileSaverConnect(function() {
  256.          var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
  257.          vkLdr.hide();
  258.          saveAs(blob, filename);
  259.       });
  260.    },
  261.    load_dump: function(callback){
  262.         var inp_el;
  263.         var load_file = function(){
  264.            var file = inp_el.files[0];
  265.            if (!file) {
  266.              return;
  267.            }
  268.  
  269.            var reader = new FileReader();
  270.            reader.onload = function(e) {
  271.              var contents = JSON.parse(e.target.result);
  272.              callback(contents);
  273.            };
  274.            reader.readAsText(file);
  275.         }
  276.         vkAlertBox(IDL('LoadDump'), IDL('SelectDumpFile')+'<input type="file" id="vk_load_dump_input">', true);
  277.         inp_el = ge('vk_load_dump_input');
  278.         inp_el.addEventListener('change', load_file, false);
  279.    },
  280.    get_history:function(uid){
  281.       if (!uid) uid=cur.thread.id;
  282.       var PER_REQ=100;
  283.       var offset=0;
  284.       var messages = [];
  285.       var scan=function(){
  286.          hide('save_btn_text');
  287.          show('saveldr');
  288.          //document.title='offset:'+offset;
  289.          var w=getSize(ge('saveldr'),true)[0];
  290.          if (offset==0) ge('saveldr').innerHTML=vkProgressBar(offset,10,w);
  291.  
  292.          var code=[];
  293.          for (var i=0; i<10; i++){
  294.             code.push('API.messages.getHistory({user_id:'+uid+', count:'+PER_REQ+', offset:'+offset+', rev:1}).items');//
  295.             offset+=PER_REQ;
  296.          }
  297.          dApi.call('execute',{code:'return {count:API.messages.getHistory({user_id:'+uid+', count:0, offset:0}).count, items:'+code.join('+')+'};',v:'5.5'},function(r){
  298.             var msgs = r.response.items;
  299.             var count = r.response.count;
  300.             ge('saveldr').innerHTML=vkProgressBar(offset,count,w);
  301.  
  302.             messages = messages.concat(msgs);
  303.             if (msgs.length>0){
  304.                setTimeout(scan,350);
  305.             } else {
  306.                vkopt.messages.export_data(messages);
  307.             }
  308.          });
  309.       };
  310.       scan();
  311.    }
  312. };
  313.  
  314. vkopt.messages.load_dump(function(data){
  315.    vkopt.messages.export_data(data);
  316. })
Add Comment
Please, Sign In to add comment