Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. <?php
  2.  
  3. $connection = mysqli_connect("127.0.0.1", "root", "Dantheman", "cardiffremap") or die('error');
  4. function getPowerAndTorque($url, $varientId)
  5. {
  6. libxml_use_internal_errors(true);
  7. $input = file_get_contents($url);
  8.  
  9. $matches = array();
  10.  
  11. $dom = new DOMDocument;
  12.  
  13. $dom->loadHTML($input);
  14.  
  15. foreach($dom->getElementsByTagName('td') as $td) {
  16. if ( ! $td->hasAttribute('class')) {
  17. continue;
  18. }
  19.  
  20. $class = $td->getAttribute('class');
  21.  
  22. if ($class == 'ctvc_stage_td') {
  23. $matches[] = $td->textContent;
  24. }
  25.  
  26. }
  27.  
  28. return array(
  29. 'power_original' => (int) filter_var($matches[0], FILTER_SANITIZE_NUMBER_INT),
  30. 'power_modified' => (int) filter_var($matches[1], FILTER_SANITIZE_NUMBER_INT),
  31. 'power_difference' => (int) filter_var($matches[2], FILTER_SANITIZE_NUMBER_INT),
  32. 'torque_original' => (int) filter_var($matches[3], FILTER_SANITIZE_NUMBER_INT),
  33. 'torque_modified' => (int) filter_var($matches[4], FILTER_SANITIZE_NUMBER_INT),
  34. 'torque_difference' => (int) filter_var($matches[5], FILTER_SANITIZE_NUMBER_INT),
  35. );
  36.  
  37. }
  38.  
  39. function postAndGet($url, $fields=array())
  40. {
  41. $fields_string = '';
  42. //url-ify the data for the POST
  43. foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
  44. rtrim($fields_string, '&');
  45.  
  46. //open connection
  47. $ch = curl_init();
  48.  
  49. //set the url, number of POST vars, POST data
  50. curl_setopt($ch,CURLOPT_URL, $url);
  51. curl_setopt($ch,CURLOPT_POST, count($fields));
  52. curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54.  
  55. //execute post
  56. $result = curl_exec($ch);
  57.  
  58. //close connection
  59. curl_close($ch);
  60. return $result;
  61. }
  62.  
  63. function handleVarient($varient, $modelId, $fuelType)
  64. {
  65. global $connection;
  66. $varient_url = $varient->Url;
  67. $varient_id = mysqli_escape_string($connection, $varient->Value);
  68. $varient_value = mysqli_escape_string($connection, $varient->Text);
  69. $data = getPowerAndTorque("https://www.celtictuning.co.uk/" . $varient_url, $varient_id);
  70. $power_original = $data['power_original'];
  71. $power_modified = $data['power_modified'];
  72. $power_difference = $data['power_difference'];
  73. $torque_original = $data['torque_original'];
  74. $torque_modified = $data['torque_modified'];
  75. $torque_difference = $data['torque_difference'];
  76. mysqli_query($connection, "INSERT INTO `vehicle_varient` (vehicle_varient_id, varient, fuel_type, model_id, power_original, power_modified, power_difference, torque_original, torque_modified, torque_difference) VALUES('$varient_id', '$varient_value', '$fuelType', '$modelId', '$power_original', '$power_modified', '$power_difference', '$torque_original', '$torque_modified', '$torque_difference');");
  77. }
  78.  
  79.  
  80. function handleVarients($makeId, $modelId, $fuelType)
  81. {
  82. $json = json_decode(postAndGet("https://www.celtictuning.co.uk/?option=com_ajax&plugin=ctajaxcore&format=json&ct_method=variant", array(
  83. 'makeId' => $makeId,
  84. 'modelId' => $modelId,
  85. 'fuelId' => $fuelType
  86. )
  87. ));
  88.  
  89. foreach($json->data[0]->Items as $varient)
  90. {
  91. if (is_object($varient))
  92. {
  93. if ($varient->Value != -1)
  94. {
  95. handleVarient($varient, $modelId, $fuelType);
  96. }
  97. }
  98. }
  99.  
  100. }
  101.  
  102. function handleModel($model, $makeId)
  103. {
  104. global $connection;
  105. // Insert the model into the database
  106. $modelId = mysqli_escape_string($connection, $model->Value);
  107. $model_name = mysqli_escape_string($connection, $model->Text);
  108. $imageUrl = mysqli_escape_string($connection, $model->ImageUrl);
  109.  
  110.  
  111. mysqli_query($connection, "INSERT INTO `vehicle_models` (`vehicle_model_id`, `model`, `make_id`, `image_url`) VALUES('$modelId', '$model_name', '$makeId', '$imageUrl');");
  112.  
  113. // Handle the varients
  114. handleVarients($makeId, $modelId, 0);
  115. handleVarients($makeId, $modelId, 1);
  116. }
  117.  
  118. function handleModels($makeId)
  119. {
  120. $json = json_decode(postAndGet('https://www.celtictuning.co.uk/?option=com_ajax&plugin=ctajaxcore&format=json&ct_method=model', array(
  121. 'makeId' => $makeId
  122. )));
  123.  
  124. foreach($json->data[0]->Models as $model)
  125. {
  126. if (is_object($model))
  127. {
  128. if ($model->Value != -1)
  129. {
  130. handleModel($model, $makeId);
  131. }
  132. }
  133. }
  134. }
  135.  
  136. function handleMake($make)
  137. {
  138. global $connection;
  139. // Insert the make into the database
  140. $makeId = mysqli_escape_string($connection, $make->Value);
  141. $make = mysqli_escape_string($connection, $make->Text);
  142.  
  143. mysqli_query($connection, "INSERT INTO `vehicle_makes` (`vehicle_make_id`, `make`) VALUES('$makeId', '$make');");
  144. echo "Make: " . $make . "<br />";
  145. // Handle the make's models
  146. handleModels($makeId);
  147.  
  148. echo "END OF MAKE" . "<br />";
  149. }
  150.  
  151. function handleMakes()
  152. {
  153. $json = json_decode(postAndGet('https://www.celtictuning.co.uk/about-us?option=com_ajax&plugin=ctajaxcore&format=json&ct_method=make'));
  154. foreach($json->data[0]->Items as $make)
  155. {
  156. if (is_object($make))
  157. {
  158. if ($make->Value != -1)
  159. {
  160. handleMake($make);
  161. }
  162. }
  163. }
  164. }
  165.  
  166. handleMakes();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement