From 1b5901769485c7e2607448ab2b1c6682927908df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gi=C3=B2?= Date: Fri, 5 Jul 2024 14:11:54 +0200 Subject: [PATCH] Enhances Commands --- .../app/Console/Commands/scraperAddRegion.php | 41 ++++++++++++++++++- .../app/Console/Commands/scraperAddSeed.php | 30 ++++++++++---- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/scraper/app/Console/Commands/scraperAddRegion.php b/scraper/app/Console/Commands/scraperAddRegion.php index e3ba6cb..cec2ebe 100644 --- a/scraper/app/Console/Commands/scraperAddRegion.php +++ b/scraper/app/Console/Commands/scraperAddRegion.php @@ -3,7 +3,9 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +use Illuminate\Support\Facades\Http; use App\Models\Regions; +use App\Models\Seed; class scraperAddRegion extends Command { @@ -12,7 +14,7 @@ class scraperAddRegion extends Command * * @var string */ - protected $signature = 'scraper:add-region {name}'; + protected $signature = 'scraper:add-region'; /** * The console command description. @@ -27,7 +29,30 @@ class scraperAddRegion extends Command public function handle() { - $name = $this->argument('name'); + $name = $this->ask('Type in desired region'); + + $suggestions = Http::get('https://www.e-domizil.ch/api/v2/autocomplete?q='.$name.'&limit=6'); + $options = []; + + if($suggestions->successful()){ + + $suggestionsArr = json_decode($suggestions->body(), 1); + + if(count($suggestionsArr['suggestions']) > 0){ + + foreach ($suggestionsArr['suggestions'] as $suggestion) { + $options[$suggestion['id']] = $suggestion['fullTitle']; + } + + $choice = $this->choice( + 'Choose desired region', + $options + ); + + $id = $choice; + $name = $options[$choice]; + } + } $region = Regions::create([ 'name' => $name @@ -35,5 +60,17 @@ class scraperAddRegion extends Command $this->info('New Region created '.$region); + if(count($options) > 0){ + + $seed = Seed::create([ + 'uri' => 'https://www.e-domizil.ch/search/'.$id.'?_format=json', + 'region_id' => $region['id'] + ]); + + $this->info('New Seed added '.$seed); + + } + + } } diff --git a/scraper/app/Console/Commands/scraperAddSeed.php b/scraper/app/Console/Commands/scraperAddSeed.php index 41accfc..30df27b 100644 --- a/scraper/app/Console/Commands/scraperAddSeed.php +++ b/scraper/app/Console/Commands/scraperAddSeed.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +use App\Models\Regions; use App\Models\Seed; class scraperAddSeed extends Command @@ -12,7 +13,7 @@ class scraperAddSeed extends Command * * @var string */ - protected $signature = 'scraper:add-seed {regionId} {uri}'; + protected $signature = 'scraper:add-seed'; /** * The console command description. @@ -27,15 +28,28 @@ class scraperAddSeed extends Command public function handle() { - $regionId = $this->argument('regionId'); - $uri = $this->argument('uri'); + $regions = Regions::all()->pluck('name', 'id')->all(); - $seed = Seed::create([ - 'uri' => $uri, - 'region_id' => $regionId - ]); - $this->info('New Seed created '.$seed); + $regionChoice = $this->choice( + 'For which region do you want to add the seed?', + $regions + ); + + $url = $this->ask('Please input the seed url'); + + if(!str_contains($url, 'e-domizil.ch')){ + $this->error('Given seed url is not valid.'); + }else{ + + $seed = Seed::create([ + 'uri' => $url, + 'region_id' => array_search($regionChoice, $regions) + ]); + + $this->info('New Seed has been added: '.$seed); + + } } }