MyHomeCollection/app/Parser/Pap.php
2020-07-24 11:15:45 +02:00

63 lines
2.0 KiB
PHP

<?php
namespace App\Parser;
use App\ParsedHome;
use App\Parser;
class Pap extends Parser
{
/**
* @inheritDoc
*/
public function parse(): ParsedHome
{
$idAnnonce = $this->getIdAnnonceFromUrl($this->url);
$url = sprintf('https://ws.pap.fr/immobilier/annonces/%s', (int)$idAnnonce);
$request = $this->client->get(
$url,
[
'headers' => [
'X-Device-Gsf' => '36049adaf18ade77',
'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();
$parsedHome->city = $annonce['_embedded']['place'][0]['title'];
//ex: Vente maison 110 m² Tréguier (22220)
$parsedHome->title = sprintf(
'%s %s %s m² %s',
ucfirst($annonce['produit']),
$annonce['typebien'],
$annonce['surface'],
$parsedHome->city
);
$parsedHome->price = $annonce['prix'];
$parsedHome->surface = $annonce['surface'];
$parsedHome->rooms = $annonce['nb_pieces'];
$parsedHome->energy = $annonce['classe_energie'];
$parsedHome->description = $annonce['texte'];
$parsedHome->pictures = collect(
$annonce['_embedded']['photo']
)->map(static function ($photo) {
return $photo['_links']['self']['href'];
});
$location = $annonce['marker'];
$parsedHome->map = ['lat' => $location['lat'], 'lng' => $location['lng']];
return $parsedHome;
}
private function getIdAnnonceFromUrl($url)
{
//ex: https://www.pap.fr/annonces/maison-treguier-22220-r432301732
return substr(collect(explode('-', collect(array_filter(explode('/', $url)))->last()))->last(), 1);
}
}