[AuoJMBridge] Follow Website change (#1527)

* [AuoJMBridge] Follow Website change
This commit is contained in:
sysadminstory 2020-05-17 20:05:04 +02:00 committed by GitHub
parent 63a4db7e86
commit f4affe1833
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -77,110 +77,69 @@ class AutoJMBridge extends BridgeAbstract {
$model_url = self::URI . $this->getInput('url'); $model_url = self::URI . $this->getInput('url');
// Get the session cookies and the form token // Build the GET data
$this->getInitialParameters($model_url); $get_data = 'form[energy]=' . $this->getInput('energy') .
'&form[transmission]=' . $this->getInput('transmission') .
'&form[priceMin]=' . $this->getInput('priceMin') .
'&form[priceMin]=' . $this->getInput('priceMin');
// Build the form // Set the header 'X-Requested-With' like the website does it
$post_data = array(
'form[energy]' => $this->getInput('energy'),
'form[transmission]' => $this->getInput('transmission'),
'form[priceMin]' => $this->getInput('priceMin'),
'form[priceMin]' => $this->getInput('priceMin'),
'form[_token]' => $this->token
);
// Set the Form request content type
$header = array( $header = array(
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With: XMLHttpRequest'
);
// Set the curl options (POST query and content, and session cookies
$curl_opts = array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($post_data),
CURLOPT_COOKIE => $this->cookies
); );
// Get the JSON content of the form // Get the JSON content of the form
$json = getContents($model_url, $header, $curl_opts) $json = getContents($model_url . '?' . $get_data, $header)
or returnServerError('Could not request AutoJM.'); or returnServerError('Could not request AutoJM.');
// Extract the HTML content from the JSON result // Extract the HTML content from the JSON result
$data = json_decode($json); $data = json_decode($json);
$html = str_get_html($data->content); $html = str_get_html($data->results);
// Go through every finisha of the model // Go through every car of the model
$list = $html->find('h3'); $list = $html->find('div[class=car-card]');
foreach ($list as $finish) { foreach ($list as $car) {
$finish_name = $finish->plaintext;
$motorizations = $finish->next_sibling()->find('li');
foreach ($motorizations as $element) {
$image = $element->find('div[class=block-product-image]', 0)->{'data-ga-banner'};
$serie = $element->find('span[class=model]', 0)->plaintext;
$url = self::URI . substr($element->find('a', 0)->href, 1);
if ($element->find('span[class*=block-product-nbModel]', 0) != null) {
$availability = 'En Stock';
} else {
$availability = 'Sur commande';
}
$discount_html = $element->find('span[class*=tag--promo]', 0);
if ($discount_html != null) {
$discount = $discount_html->plaintext;
} else {
$discount = 'inconnue';
}
$price = $element->find('span[class=price red h1]', 0)->plaintext;
$item = array();
$item['title'] = $finish_name . ' ' . $serie;
$item['content'] = '<p><img style="vertical-align:middle ; padding: 10px" src="' . $image . '" />'
. $finish_name . ' ' . $serie . '</p>';
$item['content'] .= '<ul><li>Disponibilité : ' . $availability . '</li>';
$item['content'] .= '<li>Série : ' . $serie . '</li>';
$item['content'] .= '<li>Remise : ' . $discount . '</li>';
$item['content'] .= '<li>Prix : ' . $price . '</li></ul>';
// Add a fictionnal anchor to the RSS element URL, based on the item content ; // Get the Finish name if this car is the first of a new finish
// As the URL could be identical even if the price change, some RSS reader will not show those offers as new items $prev_tag = $car->prev_sibling();
$item['uri'] = $url . '#' . md5($item['content']); if($prev_tag->tag == 'div' && $prev_tag->class == 'results-title') {
$finish_name = $prev_tag->plaintext;
$this->items[] = $item;
} }
}
}
/** // Get the info about the car offer
* Gets the session cookie and the form token $image = $car->find('div[class=car-card__visual]', 0)->find('img', 0)->src;
* $serie = $car->find('div[class=car-card__title]', 0)->plaintext;
* @param string $pageURL The URL from which to get the values $url = $car->find('a', 0)->href;
*/ // Check if the car model is in stock or available only on order
private function getInitialParameters($pageURL) { if($car->find('span[class*=tag--dispo]', 0) != null) {
$ch = curl_init(); $availability = 'En Stock';
curl_setopt($ch, CURLOPT_URL, $pageURL); } else {
curl_setopt($ch, CURLOPT_HEADER, true); $availability = 'Sur commande';
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
// Separate the response header and the content
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($data, 0, $headerSize);
$content = substr($data, $headerSize);
curl_close($ch);
// Extract the cookies from the headers
$cookies = '';
$http_response_header = explode("\r\n", $header);
foreach ($http_response_header as $hdr) {
if (strpos($hdr, 'Set-Cookie') !== false) {
$cLine = explode(':', $hdr)[1];
$cLine = explode(';', $cLine)[0];
$cookies .= ';' . $cLine;
} }
} $discount_html = $car->find('span[class=promo]', 0);
$this->cookies = trim(substr($cookies, 1)); // Check if there is any discount dsiplayed
if ($discount_html != null) {
$discount = $discount_html->plaintext;
} else {
$discount = 'inconnue';
}
$price = $car->find('span[class=price]', 0)->plaintext;
// Get the token from the content // Construct the new item
$html = str_get_html($content); $item = array();
$token = $html->find('input[type=hidden][id=form__token]', 0); $item['title'] = $finish_name . ' ' . $serie;
$this->token = $token->value; $item['content'] = '<p><img style="vertical-align:middle ; padding: 10px" src="' . $image . '" />'
. $finish_name . ' ' . $serie . '</p>';
$item['content'] .= '<ul><li>Disponibilité : ' . $availability . '</li>';
$item['content'] .= '<li>Série : ' . $serie . '</li>';
$item['content'] .= '<li>Remise : ' . $discount . '</li>';
$item['content'] .= '<li>Prix : ' . $price . '</li></ul>';
// Add a fictionnal anchor to the RSS element URL, based on the item content ;
// As the URL could be identical even if the price change, some RSS reader will not show those offers as new items
$item['uri'] = $url . '#' . md5($item['content']);
$this->items[] = $item;
}
} }
} }