ConsultancyProject1_Auslast.../app/Console/Commands/scraperAddRegion.php

77 lines
1.7 KiB
PHP

<?php
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
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scraper:add-region';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add new region to database.';
/**
* Execute the console command.
*/
public function handle()
{
$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
]);
$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);
}
}
}