Start working on handling multiple countries at once

This commit is contained in:
2026-01-16 11:20:34 +01:00
parent 4983f868df
commit e1340d45ed
4 changed files with 45 additions and 11 deletions

View File

@@ -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(),
]

View File

@@ -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]);

22
src/Models/Country.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
namespace Krzysiej\RyobiCrawler\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* @property string $countryName
* @property string $productUrl
* @property string $cultureCode
*/
class Country extends Model
{
public $timestamps = true;
public function products(): HasMany
{
return $this->hasMany(Product::class);
}
}

View File

@@ -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);