ConsultancyProject1_Auslast.../scraper/app/Scraper/Edomizil.php

171 lines
3.8 KiB
PHP
Raw Normal View History

2024-03-23 10:05:14 +01:00
<?php
namespace App\Scraper;
use App\Models\Seed;
use App\Models\Property;
2024-04-13 00:34:40 +02:00
use App\Models\Extraction;
2024-04-09 22:36:40 +02:00
use App\Models\Exception;
use App\Jobs\ScrapeProperty;
use App\Jobs\ScrapePropertyData;
2024-03-23 10:05:14 +01:00
use Illuminate\Support\Facades\Http;
class Edomizil{
2024-04-13 00:34:40 +02:00
public static function saveHttpException($response, $type, $entityId){
$exception = [];
$exception['status'] = $response->status();
$exception['headers'] = $response->headers();
$exception['body'] = $response->body();
$exceptionJSON = json_encode($exception);
Exception::create([
'exception' => $exceptionJSON,
'type' => $type,
'entity_id' => $entityId
]);
}
2024-04-09 22:36:40 +02:00
public static function getAllSeeds()
{
2024-04-13 00:34:40 +02:00
// get all seeds from model in random order.
2024-04-09 22:36:40 +02:00
return Seed::select('id','uri')->inRandomOrder()->get();
}
2024-03-23 10:05:14 +01:00
2024-04-09 22:36:40 +02:00
public static function getAllProperties()
{
// get all properties from model in random order.
return Property::select('id','property_platform_id')->inRandomOrder()->get();
}
2024-03-23 10:05:14 +01:00
2024-04-09 22:36:40 +02:00
public static function dispatchPropertyJobs()
{
$seeds = self::getAllSeeds();
foreach($seeds as $seed){
2024-04-13 00:34:40 +02:00
ScrapeProperty::dispatch($seed);
2024-04-09 22:36:40 +02:00
}
}
2024-03-23 10:05:14 +01:00
2024-04-09 22:36:40 +02:00
public static function dispatchPropertyDataJobs()
{
$properties = self::getAllProperties();
foreach($properties as $property){
ScrapePropertyData::dispatch($property);
2024-04-09 22:36:40 +02:00
}
}
2024-03-23 10:05:14 +01:00
2024-04-13 00:34:40 +02:00
public static function scrapeProperty($seed)
2024-04-09 22:36:40 +02:00
{
2024-04-13 00:34:40 +02:00
$response = Http::get($seed->uri);
if($response->successful()){
2024-04-09 22:36:40 +02:00
2024-04-13 00:34:40 +02:00
$json = $response->json();
foreach($json['offers'] as $offer){
$property = Property::firstWhere('property_platform_id', $offer['id']);
$geoLocation = implode(',', $offer['geoLocation']);
if($property){
$property->last_found = now();
$property->save();
// check if geoLocation is the same as last crawl
if($property->check_data !== $geoLocation){
Exception::create([
'exception' => 'geoLocation was different: '.$geoLocation,
'entity_type' => 'property',
2024-04-22 12:00:57 +02:00
'entity_id' => $property->id
2024-04-13 00:34:40 +02:00
]);
}
}else{
Property::create([
'property_platform_id' => $offer['id'],
'seed_id' => $seed->id,
'check_data' => $geoLocation,
'last_found' => now()
2024-03-23 10:05:14 +01:00
]);
}
}
2024-06-24 18:49:43 +02:00
return count($json['offers']);
2024-04-13 00:34:40 +02:00
}else{
self::saveHttpException($response,'property', $seed->id);
2024-06-24 18:49:43 +02:00
return 0;
2024-03-23 10:05:14 +01:00
}
}
public static function scrapePropertyData($property){
2024-04-13 00:34:40 +02:00
2024-06-24 18:49:43 +02:00
$result = [];
2024-04-13 00:34:40 +02:00
// scrape offer details such as name etc.
$offer = Http::get('https://www.e-domizil.ch/rental/offer/'.$property->property_platform_id);
2024-04-13 00:34:40 +02:00
if($offer->successful()){
Extraction::create([
'property_id' => $property->id,
2024-04-13 00:34:40 +02:00
'type' => 'offer',
'body' => $offer->body(),
'header' => json_encode($offer->headers())
]);
}else{
self::saveHttpException($offer,'offer',$property->id);
2024-04-13 00:34:40 +02:00
}
2024-06-24 18:49:43 +02:00
$result['offer'] = $offer->body();
2024-04-13 00:34:40 +02:00
// scrape price of property
$price = Http::get('https://www.e-domizil.ch/booking/checkout/priceDetails/'.$property->property_platform_id);
2024-04-13 00:34:40 +02:00
if($price->successful()){
Extraction::create([
'property_id' => $property->id,
2024-04-13 00:34:40 +02:00
'type' => 'price',
'body' => $price->body(),
'header' => json_encode($price->headers())
]);
}else{
self::saveHttpException($price,'price',$property->id);
2024-04-13 00:34:40 +02:00
}
2024-06-24 18:49:43 +02:00
$result['price'] = $price->body();
2024-04-13 00:34:40 +02:00
// scrape calendar which contains occupancies
$calendar = Http::get('https://www.e-domizil.ch/api/v2/calendar/'.$property->property_platform_id, [
2024-04-09 22:36:40 +02:00
'year' => date("Y"),
'month' => date("m")
]);
2024-04-13 00:34:40 +02:00
if($calendar->successful()){
2024-04-09 22:36:40 +02:00
2024-04-13 00:34:40 +02:00
Extraction::create([
'property_id' => $property->id,
2024-04-13 00:34:40 +02:00
'type' => 'calendar',
'body' => $calendar->body(),
'header' => json_encode($calendar->headers())
]);
2024-04-09 22:36:40 +02:00
2024-04-13 00:34:40 +02:00
}else{
2024-06-24 18:49:43 +02:00
self::saveHttpException($calendar,'calendar',$property->id);
2024-04-13 00:34:40 +02:00
}
2024-06-24 18:49:43 +02:00
$result['calendar'] = $calendar->body();
return json_encode($result);
2024-04-09 22:36:40 +02:00
2024-03-23 10:05:14 +01:00
}
}