Enhances Commands

main
Giò 2024-07-05 14:11:54 +02:00
parent b7661d8e01
commit 1b59017694
2 changed files with 61 additions and 10 deletions

View File

@ -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);
}
}
}

View File

@ -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();
$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' => $uri,
'region_id' => $regionId
'uri' => $url,
'region_id' => array_search($regionChoice, $regions)
]);
$this->info('New Seed created '.$seed);
$this->info('New Seed has been added: '.$seed);
}
}
}