Add country option to scrape command. #61
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user