Advertisement
AyrA

4chan filter

Jul 12th, 2016
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var webPage=require('webpage');
  2. var page=webPage.create();
  3. var fs=require("fs");
  4.  
  5. phantom.onError = function(msg, trace) {
  6.     var msgStack = ['PHANTOM ERROR: ' + msg];
  7.     if (trace && trace.length)
  8.     {
  9.         msgStack.push('TRACE:');
  10.         trace.forEach(function(t)
  11.         {
  12.             msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
  13.         });
  14.     }
  15.     console.error(msgStack.join('\n'));
  16.     phantom.exit(1);
  17. };
  18.  
  19. //Disable web security
  20. page.settings.webSecurityEnabled = false;
  21. page.settings.XSSAuditingEnabled = false;
  22.  
  23. page.onConsoleMessage=function(msg){
  24.     console.log(msg);
  25. };
  26.  
  27. page.open('http://boards.4chan.org/trash/catalog', function(status) {
  28.     console.log("evaluating site specific js");
  29.     var ret=page.evaluate(function() {
  30.         var getImg=function(x)
  31.         {
  32.             var v=document.querySelector("#thumb-"+x);
  33.             if(v)
  34.             {
  35.                 console.log("converting image "+v.src);
  36.                 var canvas=document.createElement("canvas");
  37.                 canvas.width=v.width;
  38.                 canvas.height=v.height;
  39.                 var ctx=canvas.getContext("2d");
  40.                 ctx.drawImage(v,0,0,v.width,v.height);
  41.                 return canvas.toDataURL("image/png").split(",")[1];
  42.             }
  43.         }
  44.        
  45.         for(var x in catalog.threads)
  46.         {
  47.             catalog.threads[x].teaser=escape(catalog.threads[x].teaser);
  48.             catalog.threads[x].img=getImg(x);
  49.         }
  50.         return { catalog:catalog.threads,sort:catalog.order.alt/*, img:img*/};
  51.     });
  52.    
  53.     var entries=ret.sort.map(function(v,i,a)
  54.     {
  55.         ret.catalog[v].id=v;
  56.         return ret.catalog[v];
  57.     });
  58.    
  59.     console.log("Rendering images. Got "+entries.length);
  60.    
  61.     entries.forEach(function(v,i,a)
  62.     {
  63.         console.log("rendering "+v.id);
  64.         fs.write("img/"+v.id+".png",atob(v.img),"wb");
  65.     });
  66.     console.log("Processing text content");
  67.     var s=[];
  68.    
  69.     entries.forEach(function(v,i,a)
  70.     {
  71.         s.push([
  72.             v.id,
  73.             v.date,
  74.             v.file,
  75.             v.lr.date,
  76.             v.i,
  77.             v.r,
  78.             v.teaser
  79.         ].join("\t"));
  80.     });
  81.     fs.write("threads.txt", s.join("\r\n"), "w");
  82.     console.log("All done");
  83.     phantom.exit();
  84. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement