MyHomeCollection/app/Parser/Pap.php

63 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2020-07-23 12:26:10 +02:00
<?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)
2020-07-24 11:15:45 +02:00
$parsedHome->title = sprintf(
'%s %s %s m² %s',
2020-07-23 12:26:10 +02:00
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']
2020-07-24 11:15:45 +02:00
)->map(static function ($photo) {
2020-07-23 12:26:10 +02:00
return $photo['_links']['self']['href'];
});
$location = $annonce['marker'];
$parsedHome->map = ['lat' => $location['lat'], 'lng' => $location['lng']];
return $parsedHome;
}
2020-07-24 11:15:45 +02:00
private function getIdAnnonceFromUrl($url)
2020-07-23 12:26:10 +02:00
{
//ex: https://www.pap.fr/annonces/maison-treguier-22220-r432301732
return substr(collect(explode('-', collect(array_filter(explode('/', $url)))->last()))->last(), 1);
}
}