Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $connection = mysqli_connect("127.0.0.1", "root", "Dantheman", "cardiffremap") or die('error');
- function getPowerAndTorque($url, $varientId)
- {
- libxml_use_internal_errors(true);
- $input = file_get_contents($url);
- $matches = array();
- $dom = new DOMDocument;
- $dom->loadHTML($input);
- foreach($dom->getElementsByTagName('td') as $td) {
- if ( ! $td->hasAttribute('class')) {
- continue;
- }
- $class = $td->getAttribute('class');
- if ($class == 'ctvc_stage_td') {
- $matches[] = $td->textContent;
- }
- }
- return array(
- 'power_original' => (int) filter_var($matches[0], FILTER_SANITIZE_NUMBER_INT),
- 'power_modified' => (int) filter_var($matches[1], FILTER_SANITIZE_NUMBER_INT),
- 'power_difference' => (int) filter_var($matches[2], FILTER_SANITIZE_NUMBER_INT),
- 'torque_original' => (int) filter_var($matches[3], FILTER_SANITIZE_NUMBER_INT),
- 'torque_modified' => (int) filter_var($matches[4], FILTER_SANITIZE_NUMBER_INT),
- 'torque_difference' => (int) filter_var($matches[5], FILTER_SANITIZE_NUMBER_INT),
- );
- }
- function postAndGet($url, $fields=array())
- {
- $fields_string = '';
- //url-ify the data for the POST
- foreach($fields as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
- rtrim($fields_string, '&');
- //open connection
- $ch = curl_init();
- //set the url, number of POST vars, POST data
- curl_setopt($ch,CURLOPT_URL, $url);
- curl_setopt($ch,CURLOPT_POST, count($fields));
- curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //execute post
- $result = curl_exec($ch);
- //close connection
- curl_close($ch);
- return $result;
- }
- function handleVarient($varient, $modelId, $fuelType)
- {
- global $connection;
- $varient_url = $varient->Url;
- $varient_id = mysqli_escape_string($connection, $varient->Value);
- $varient_value = mysqli_escape_string($connection, $varient->Text);
- $data = getPowerAndTorque("https://www.celtictuning.co.uk/" . $varient_url, $varient_id);
- $power_original = $data['power_original'];
- $power_modified = $data['power_modified'];
- $power_difference = $data['power_difference'];
- $torque_original = $data['torque_original'];
- $torque_modified = $data['torque_modified'];
- $torque_difference = $data['torque_difference'];
- 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');");
- }
- function handleVarients($makeId, $modelId, $fuelType)
- {
- $json = json_decode(postAndGet("https://www.celtictuning.co.uk/?option=com_ajax&plugin=ctajaxcore&format=json&ct_method=variant", array(
- 'makeId' => $makeId,
- 'modelId' => $modelId,
- 'fuelId' => $fuelType
- )
- ));
- foreach($json->data[0]->Items as $varient)
- {
- if (is_object($varient))
- {
- if ($varient->Value != -1)
- {
- handleVarient($varient, $modelId, $fuelType);
- }
- }
- }
- }
- function handleModel($model, $makeId)
- {
- global $connection;
- // Insert the model into the database
- $modelId = mysqli_escape_string($connection, $model->Value);
- $model_name = mysqli_escape_string($connection, $model->Text);
- $imageUrl = mysqli_escape_string($connection, $model->ImageUrl);
- mysqli_query($connection, "INSERT INTO `vehicle_models` (`vehicle_model_id`, `model`, `make_id`, `image_url`) VALUES('$modelId', '$model_name', '$makeId', '$imageUrl');");
- // Handle the varients
- handleVarients($makeId, $modelId, 0);
- handleVarients($makeId, $modelId, 1);
- }
- function handleModels($makeId)
- {
- $json = json_decode(postAndGet('https://www.celtictuning.co.uk/?option=com_ajax&plugin=ctajaxcore&format=json&ct_method=model', array(
- 'makeId' => $makeId
- )));
- foreach($json->data[0]->Models as $model)
- {
- if (is_object($model))
- {
- if ($model->Value != -1)
- {
- handleModel($model, $makeId);
- }
- }
- }
- }
- function handleMake($make)
- {
- global $connection;
- // Insert the make into the database
- $makeId = mysqli_escape_string($connection, $make->Value);
- $make = mysqli_escape_string($connection, $make->Text);
- mysqli_query($connection, "INSERT INTO `vehicle_makes` (`vehicle_make_id`, `make`) VALUES('$makeId', '$make');");
- echo "Make: " . $make . "<br />";
- // Handle the make's models
- handleModels($makeId);
- echo "END OF MAKE" . "<br />";
- }
- function handleMakes()
- {
- $json = json_decode(postAndGet('https://www.celtictuning.co.uk/about-us?option=com_ajax&plugin=ctajaxcore&format=json&ct_method=make'));
- foreach($json->data[0]->Items as $make)
- {
- if (is_object($make))
- {
- if ($make->Value != -1)
- {
- handleMake($make);
- }
- }
- }
- }
- handleMakes();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement