MyHomeCollection/app/Parser/ImmobilierNotaires.php

72 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Parser;
use App\ParsedHome;
use App\Parser;
use Symfony\Component\DomCrawler\Crawler;
class ImmobilierNotaires extends Parser
{
/**
* @inheritDoc
*/
public function parse(): ParsedHome
{
$idAnnonce = $this->getIdAnnonceFromUrl($this->url);
$url = sprintf('https://www.immobilier.notaires.fr/pub-services/inotr-www-annonces/v1/annonces/%s', (int)$idAnnonce);
$request = $this->client->get(
$url,
[
'headers' => [
'Referer' => $this->url,
'User-Agent' => 'Dalvik/2.1.0 (Linux; U; Android 6.0.1; D5803 Build/MOB30M.Z1)',
'Connection' => 'Keep-Alive',
'Accept-Encoding' => 'gzip'
],
]
);
$annonce = json_decode($request->getBody()->getContents(), true);
$parsedHome = new ParsedHome();
$transaction = $annonce[strtolower($annonce['typeTransaction'])];
$images = $transaction['multimedias'];
$parsedHome->pictures = [];
foreach ($images as $image) {
$parsedHome->pictures[] = $image['vga'];
}
$parsedHome->description = $transaction['descriptions'][0]['descLongue'];
if (array_key_exists('prixTotal', $transaction)) {
$parsedHome->price = $transaction['prixTotal'];
} elseif ($transaction['redevableEmoluments'] === 'ACQUEREUR') {
$parsedHome->price = $transaction['prix'] + $transaction['emoluments'];
} else {
$parsedHome->price = $transaction['prix'];
}
$bien = $annonce['bien'];
$maison = $bien['maison'];
$parsedHome->city = $maison['communeNom'];
$parsedHome->surface = $maison['surfaceHabitable'];
$parsedHome->garden_surface = $maison['surfaceTerrain'] ?? null;
$parsedHome->rooms = $maison['nbPieces'];
$parsedHome->title = $transaction['descriptions'][0]['descCourte'];
$parsedHome->energy = $maison['consommationClasse'] === 'Y' ? null : $maison['consommationClasse'];
$parsedHome->ges = $maison['emissionGesClasse'] === 'Y' ? null : $maison['emissionGesClasse'];
if (array_key_exists('coordonneesExactesW84', $maison)) {
$parsedHome->map = [
'lat' => $maison['coordonneesExactesW84']['coordonneeY'],
'lng' => $maison['coordonneesExactesW84']['coordonneeX']
];
}
return $parsedHome;
}
private function getIdAnnonceFromUrl($url)
{
// ex: https://www.immobilier.notaires.fr/fr/annonce-immo/immo-interactif/maison/rognac-13/1263723
$url = parse_url($url, PHP_URL_PATH);
return collect(explode('/', $url))->last();
}
}