Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. <?php
  2. // 569512
  3. include('simple_html_dom.php');
  4.  
  5. ini_set('memory_limit', '-1');
  6. ini_set('max_execution_time', 3600000);
  7.  
  8. $page = 'http://www.getrandomthings.com/';
  9. $html = new simple_html_dom();
  10. $html->load_file($page);
  11.  
  12. $items = $html->find('[class=app_cat_ul] a');
  13.  
  14. //db
  15.  
  16. $servername = "localhost";
  17. $username = "root";
  18. $password = "";
  19. $dbname = "getrandomthings";
  20.  
  21. // Create connection
  22. $conn = new mysqli($servername, $username, $password, $dbname);
  23. // Check connection
  24. if ($conn->connect_error) {
  25. die("Connection failed: " . $conn->connect_error);
  26. }
  27.  
  28. /*
  29. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  30. $result = $conn->query($sql);
  31.  
  32. if ($result->num_rows > 0) {
  33. echo "<table><tr><th>ID</th><th>Name</th></tr>";
  34. // output data of each row
  35. while($row = $result->fetch_assoc()) {
  36. echo "<tr><td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td></tr>";
  37. }
  38. echo "</table>";
  39. } else {
  40. echo "0 results";
  41. }*/
  42.  
  43.  
  44. // curl
  45. $ch = curl_init();
  46.  
  47. foreach ($items as $k => $item){
  48. echo $k.'<br>';
  49. $category = str_replace(' ','_',$item->innertext);//$item->innertext
  50. $sql_count = 'SELECT count(*) total FROM all_things ats
  51. WHERE ats.category = "' . $category . '"';
  52. $_result = $conn->query($sql_count);
  53. $count = 0;
  54. while($row = $_result->fetch_assoc()) {
  55. $count = intval($row['total']);
  56. }
  57.  
  58. // 545
  59. if($count < 10000 && $k > 260) {
  60. curl_setopt($ch, CURLOPT_URL,$page.'data/'.$item->href);//.$item->href
  61. curl_setopt($ch, CURLOPT_POST, 1);
  62. curl_setopt($ch, CURLOPT_POSTFIELDS,
  63. "num=100&add=address&unique=true");
  64.  
  65.  
  66. // in real life you should use something like:
  67. // curl_setopt($ch, CURLOPT_POSTFIELDS,
  68. // http_build_query(array('postvar1' => 'value1')));
  69.  
  70. // receive server response ...
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  72.  
  73. $max = $count;
  74.  
  75. $server_output = curl_exec($ch);
  76.  
  77. while (1) {
  78.  
  79. $_check = true;
  80.  
  81. $server_output = curl_exec($ch);
  82.  
  83. $html_child = str_get_html($server_output);
  84. $f_child = $html_child->firstChild();
  85. if (!empty($f_child)) {
  86. $data = $f_child->children();
  87. foreach ($data as $_item) {
  88. // $value = $_item->plaintext;
  89. // echo 'value: '.$value.'<br>';
  90. if ($_item->find('[class=sdes]', 0)) {
  91. $sub_value = $_item->find('[class=sdes]', 0)->innertext;
  92. } else {
  93. $sub_value = '';
  94. }
  95. // echo 'sub_value: '.$sub_value.'<br>';
  96. $value = trim(str_replace($sub_value, '', $_item->plaintext));
  97. // echo 'value: '.$value.'<br>';
  98. if ($_item->find('img', 0)) {
  99. $image = $_item->find('img', 0)->src;
  100. } else {
  101. $image = '';
  102. }
  103. // echo 'image: '.$image.'<br>';
  104.  
  105. $sql_exist = 'SELECT 1 FROM all_things ats
  106. WHERE ats.value = "' . $value . '"
  107. AND ats.category = "' . $category . '"
  108. LIMIT 1';
  109. $result = $conn->query($sql_exist);
  110.  
  111. if (empty($result) || $result->num_rows == 0) {
  112. $_check = false;
  113. }
  114.  
  115. $value = mysqli_real_escape_string($conn, $value);
  116. $sub_value = mysqli_real_escape_string($conn, $sub_value);
  117.  
  118. if (empty($result) || $result->num_rows == 0) {
  119. $sql = 'INSERT INTO all_things (value, category , sub_value, image, origin_image)
  120. VALUES ("' . $value . '", "' . $category . '","' . $sub_value . '","' . $image . '","' . $image . '")';
  121. $h = $conn->query($sql);
  122. }
  123. }
  124. if ($_check || $max >= 10000) {
  125. break;
  126. }
  127.  
  128. $max += count($data);
  129. }
  130. }
  131. }
  132. // break;
  133. }
  134.  
  135. echo 'done';
  136.  
  137. curl_close ($ch);
  138.  
  139. $conn->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement