From 56fd13c62770ca0c1eff270b4bcfd74b56280a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Sat, 18 May 2024 20:58:16 +0200 Subject: [PATCH] Use progress bar to track progress --- src/Command/ScrapeWebsite.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index 069d31e..1860ade 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -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; } } \ No newline at end of file