From e1340d45ed726562688966b810b1fafd26ec9e21 Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Fri, 16 Jan 2026 11:20:34 +0100 Subject: [PATCH] Start working on handling multiple countries at once --- src/Command/Migrate.php | 3 +++ src/Command/ScrapeWebsite.php | 25 ++++++++++++++----------- src/Models/Country.php | 22 ++++++++++++++++++++++ src/Models/Product.php | 6 ++++++ 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 src/Models/Country.php diff --git a/src/Command/Migrate.php b/src/Command/Migrate.php index 07be8ca..3c9c77f 100644 --- a/src/Command/Migrate.php +++ b/src/Command/Migrate.php @@ -86,6 +86,7 @@ class Migrate extends Command $table->increments('id'); $table->text('countryName'); $table->text('productsUrl'); + $table->text('cultureCode'); $table->timestamps(); }); } @@ -93,12 +94,14 @@ class Migrate extends Command [ 'countryName' => 'Poland', 'productsUrl' => 'https://pl.ryobitools.eu/api/product-listing/get-products', + 'cultureCode' => 'pl-PL', 'created_at' => now(), 'updated_at' => now(), ]); Capsule::table('countries')->insert([ 'countryName' => 'UK', 'productsUrl' => 'https://uk.ryobitools.eu/api/product-listing/get-products', + 'cultureCode' => 'en-GB', 'created_at' => now(), 'updated_at' => now(), ] diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index 6b07b5c..b74ce09 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -7,7 +7,7 @@ namespace Krzysiej\RyobiCrawler\Command; use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; use Illuminate\Database\Capsule\Manager as Capsule; -use Illuminate\Support\Facades\Date; +use Krzysiej\RyobiCrawler\Models\Country; use Krzysiej\RyobiCrawler\Models\Price; use Krzysiej\RyobiCrawler\Models\Product; use Krzysiej\RyobiCrawler\Models\Stock; @@ -37,11 +37,15 @@ class ScrapeWebsite extends Command $output->writeln('Scrape products'); $progress = new ProgressBar($output); $progress->start(); - $products = $this->getProducts(); - $progress->setMaxSteps(count($products)); - foreach ($products as $product) { - $this->saveProduct($product); - $progress->advance(); + $countries = Country::all(); + foreach($countries as $country) { + $output->writeln('Country name: ' . $country->countryName); + $products = $this->getProducts($country); + $progress->setMaxSteps(count($products)); + foreach ($products as $product) { + $this->saveProduct($product, $country); + $progress->advance(); + } } $progress->finish(); $output->writeln('Scrape products - DONE'); @@ -69,19 +73,18 @@ class ScrapeWebsite extends Command return Command::SUCCESS; } - private function getProducts(): array + private function getProducts(Country $country): array { $products = []; $page = 0; do { try { -// $res = $this->client->request('POST', 'https://uk.ryobitools.eu/api/product-listing/get-products', [ - $res = $this->client->request('POST', 'https://pl.ryobitools.eu/api/product-listing/get-products', [ + $res = $this->client->request('POST', $country->productUrl, [ 'form_params' => [ "includePreviousPages" => false, "pageIndex" => $page, "pageSize" => 100, - "cultureCode" => "pl-PL", + "cultureCode" => $country->cultureCode, ] ]); $responseObject = json_decode($res->getBody()->getContents()); @@ -96,7 +99,7 @@ class ScrapeWebsite extends Command return $products; } - private function saveProduct(\stdClass $product): void + private function saveProduct(\stdClass $product, Country $country): void { /** @var Product $productModel */ $productModel = Product::firstOrNew(['skuID' => $product->skuID]); diff --git a/src/Models/Country.php b/src/Models/Country.php new file mode 100644 index 0000000..2f61beb --- /dev/null +++ b/src/Models/Country.php @@ -0,0 +1,22 @@ +hasMany(Product::class); + } +} \ No newline at end of file diff --git a/src/Models/Product.php b/src/Models/Product.php index a44f846..139ddf9 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -5,6 +5,7 @@ namespace Krzysiej\RyobiCrawler\Models; use Carbon\Traits\Date; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; @@ -32,6 +33,11 @@ class Product extends Model public $timestamps = true; public $fillable = ['skuID']; + public function country(): BelongsTo + { + return $this->belongsTo(Country::class); + } + public function price(): HasMany { return $this->hasMany(Price::class);