Use progress bar to track progress

This commit is contained in:
Krzysztof Płaczek
2024-05-18 20:58:16 +02:00
parent 4f0f0a5f00
commit 56fd13c627

View File

@@ -17,24 +17,27 @@ use Symfony\Component\Console\Output\OutputInterface;
class ScrapeWebsite extends Command
{
private Client $client;
private Capsule $capsule;
protected function configure()
{
$this->capsule = new Capsule;
$this->capsule->addConnection([
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'sqlite',
'database' => __DIR__ .'/../../database.sqlite',
]);
$this->capsule->setAsGlobal();
$this->capsule->bootEloquent();
$capsule->setAsGlobal();
$capsule->bootEloquent();
$this->client = new Client();
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$progress = new ProgressBar($output);
$progress->start();
$page = 0;
do {
$progress->advance();
$res = $this->client->request('POST', 'https://pl.ryobitools.eu/api/product-listing/get-products', [
'form_params' => [
"includePreviousPages" => false,
@@ -46,6 +49,7 @@ class ScrapeWebsite extends Command
$page++;
$responseObject = json_decode($res->getBody()->getContents());
$products = $responseObject->products;
$progress->setMaxSteps($progress->getMaxSteps()+count($products));
foreach ($products as $product) {
/** @var Product $productModel */
$productModel = Product::firstOrNew(['skuID' => $product->skuID]);
@@ -68,10 +72,13 @@ class ScrapeWebsite extends Command
$price->lowestProductPrice30Days = $product->lowestProductPrice30Days;
$productModel->price()->save($price);
}
echo ".";
$progress->advance();
}
echo "\n";
} while ((bool)$responseObject->canLoadMore);
$progress->finish();
$output->writeln('');
$output->writeln('DONE');
return Command::SUCCESS;
}
}