From 49248fb16b4cc1898dc6250ad29bd5ec0bf0bfb5 Mon Sep 17 00:00:00 2001 From: Clement Date: Thu, 23 Jul 2020 16:20:27 +0200 Subject: [PATCH] :sparkles: Ajoute le parseur SeLoger Fix #5 --- app/Parser/SeLoger.php | 72 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/app/Parser/SeLoger.php b/app/Parser/SeLoger.php index 0874b5b..9c86124 100644 --- a/app/Parser/SeLoger.php +++ b/app/Parser/SeLoger.php @@ -5,30 +5,82 @@ namespace App\Parser; use App\ParsedHome; use App\Parser; +/** + * Thanks to https://github.com/axeleroy/untoitpourcaramel/issues/3 + * Class SeLoger + * @package App\Parser + */ class SeLoger extends Parser { - protected $params = [ - 'idtt' => 1, - 'idtypebien' => '1,2', - 'getDtCreationMax' => 1, - ]; + public const TOKEN_URL = 'https://api-seloger.svc.groupe-seloger.com/api/v1/security/authenticate'; + public const LISTING_URL = 'https://api-seloger.svc.groupe-seloger.com/api/v1/listings/'; + public const SELOGER_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJTZUxvZ2VyLW1vYmlsZSIsImFwcCI6IjYzZWU3MTRkLWE2MmEtNGEyNy05ZmJlLTQwYjdhMmMzMThlNCIsImlhdCI6MTUzOTQ0MjMyMCwianRpIjoiNWYzODRhNDYtZjk5MS00ODZkLWFkN2YtMmE2M2YxZTdkM2UwIiwiYXVkIjoiU2VMb2dlci1Nb2JpbGUtNi4wIn0.Qib13Y4395f_bi5Df0IhMQRvIxQZEfpf8Ac66oTfyiA'; + public const SELOGER_GUID = '63ee714d-a62a-4a27-9fbe-40b7a2c318e4'; /** * @inheritDoc */ public function parse(): ParsedHome { + $token = $this->retrieveToken(); $idAnnonce = $this->getIdAnnonceFromUrl($this->url); - var_dump($idAnnonce); - $payload = $this->params + ['noAudiotel' => 1, 'idAnnonce' => $idAnnonce]; - $request = $this->client->request('GET', "http://ws.seloger.com/annonceDetail_4.0.xml", $payload); - var_dump($request); - exit; + $url = sprintf('%s%s', self::LISTING_URL, $idAnnonce); + $request = $this->client->request( + 'GET', + $url, + [ + 'headers' => [ + 'AppToken' => $token, + ], + ] + ); + $annonce = json_decode($request->getBody()->getContents(), true); + + $parsedHome = new ParsedHome(); + $parsedHome->city = $annonce['city']; + //ex: Vente maison 110 m² Tréguier (22220) + $parsedHome->title = $annonce['title']; + $parsedHome->price = $annonce['price']; + $parsedHome->surface = $annonce['livingArea']; + $parsedHome->rooms = $annonce['rooms']; + $parsedHome->energy = $annonce['energy']['grade']; + $parsedHome->description = $annonce['description']; + $parsedHome->pictures = $annonce['photos']; + $location = $annonce['coordinates']; + $parsedHome->map = ['lat' => $location['latitude'], 'lng' => $location['longitude']]; + + return $parsedHome; } + /** + * @param string $url + * + * @return string + */ private function getIdAnnonceFromUrl($url) { // ex : https://www.seloger.com/annonces/achat/maison/franconville-95/vieux-marche-mare-des-noues/159100953.htm + $url = parse_url($url, PHP_URL_PATH); return collect(explode('.', collect(explode('/', $url))->last()))->first(); } + + /** + * @return string + * @throws \GuzzleHttp\Exception\GuzzleException + */ + private function retrieveToken() + { + $request = $this->client->request( + 'GET', + self::TOKEN_URL, + [ + 'headers' => [ + 'AppToken' => self::SELOGER_TOKEN, + 'AppGuid' => self::SELOGER_GUID, + ], + ] + ); + + return trim($request->getBody()->getContents(), '"'); + } }