From 1303b4ad8aaa51f4f8275d5f9f65abe6c46e49fe Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Fri, 6 Feb 2026 08:34:31 +0100 Subject: [PATCH] Add country option to scrape command. --- src/Command/ScrapeWebsite.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index 4701deb..e34b4ca 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -15,11 +15,13 @@ use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; #[AsCommand(name: 'app:scrape', description: 'Scrape all products from Ryobi website')] class ScrapeWebsite extends Command { + const COUNTRY_ID = 'country'; private Client $client; public function __construct(protected Capsule $database) @@ -30,17 +32,19 @@ class ScrapeWebsite extends Command protected function configure(): void { $this->client = new Client(); + $this->addOption(self::COUNTRY_ID, 'c', InputOption::VALUE_OPTIONAL, 'Country id'); } public function execute(InputInterface $input, OutputInterface $output): int { + $countryId = intval($input->getOption(self::COUNTRY_ID)); $output->writeln('Scrape products'); - $progress = new ProgressBar($output); - $countries = Country::all(); + $countries = Country::all()->when($countryId, fn ($query) => $query->where('id', $countryId)); foreach($countries as $country) { $output->writeln('Country name: ' . $country->countryName); - $progress->start(); $products = $this->getProducts($country); + $progress = new ProgressBar($output); + $progress->start(); $progress->setMaxSteps(count($products)); foreach ($products as $product) { $this->saveProduct($product, $country); @@ -54,6 +58,7 @@ class ScrapeWebsite extends Command $output->writeln('Update prices'); $products = Product::all(); + $progress = new ProgressBar($output); $progress->setMaxSteps(count($products)); $progress->start(); foreach($products as $product) { @@ -87,7 +92,7 @@ class ScrapeWebsite extends Command 'form_params' => [ "includePreviousPages" => false, "pageIndex" => $page, - "pageSize" => 100, + "pageSize" => 1000, "cultureCode" => $country->cultureCode, ] ]); @@ -95,7 +100,7 @@ class ScrapeWebsite extends Command $products = array_merge($products, $responseObject->products); $page++; $canLoadMore = $responseObject->canLoadMore; - } catch (GuzzleException) { + } catch (GuzzleException $e) { return $products; } } while ($canLoadMore);