feature/handle-multiple-countries #45
@@ -86,6 +86,7 @@ class Migrate extends Command
|
|||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->text('countryName');
|
$table->text('countryName');
|
||||||
$table->text('productsUrl');
|
$table->text('productsUrl');
|
||||||
|
$table->text('cultureCode');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -93,12 +94,14 @@ class Migrate extends Command
|
|||||||
[
|
[
|
||||||
'countryName' => 'Poland',
|
'countryName' => 'Poland',
|
||||||
'productsUrl' => 'https://pl.ryobitools.eu/api/product-listing/get-products',
|
'productsUrl' => 'https://pl.ryobitools.eu/api/product-listing/get-products',
|
||||||
|
'cultureCode' => 'pl-PL',
|
||||||
'created_at' => now(),
|
'created_at' => now(),
|
||||||
'updated_at' => now(),
|
'updated_at' => now(),
|
||||||
]);
|
]);
|
||||||
Capsule::table('countries')->insert([
|
Capsule::table('countries')->insert([
|
||||||
'countryName' => 'UK',
|
'countryName' => 'UK',
|
||||||
'productsUrl' => 'https://uk.ryobitools.eu/api/product-listing/get-products',
|
'productsUrl' => 'https://uk.ryobitools.eu/api/product-listing/get-products',
|
||||||
|
'cultureCode' => 'en-GB',
|
||||||
'created_at' => now(),
|
'created_at' => now(),
|
||||||
'updated_at' => now(),
|
'updated_at' => now(),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace Krzysiej\RyobiCrawler\Command;
|
|||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
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\Price;
|
||||||
use Krzysiej\RyobiCrawler\Models\Product;
|
use Krzysiej\RyobiCrawler\Models\Product;
|
||||||
use Krzysiej\RyobiCrawler\Models\Stock;
|
use Krzysiej\RyobiCrawler\Models\Stock;
|
||||||
@@ -37,11 +37,15 @@ class ScrapeWebsite extends Command
|
|||||||
$output->writeln('Scrape products');
|
$output->writeln('Scrape products');
|
||||||
$progress = new ProgressBar($output);
|
$progress = new ProgressBar($output);
|
||||||
$progress->start();
|
$progress->start();
|
||||||
$products = $this->getProducts();
|
$countries = Country::all();
|
||||||
$progress->setMaxSteps(count($products));
|
foreach($countries as $country) {
|
||||||
foreach ($products as $product) {
|
$output->writeln('Country name: ' . $country->countryName);
|
||||||
$this->saveProduct($product);
|
$products = $this->getProducts($country);
|
||||||
$progress->advance();
|
$progress->setMaxSteps(count($products));
|
||||||
|
foreach ($products as $product) {
|
||||||
|
$this->saveProduct($product, $country);
|
||||||
|
$progress->advance();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$progress->finish();
|
$progress->finish();
|
||||||
$output->writeln('Scrape products - DONE');
|
$output->writeln('Scrape products - DONE');
|
||||||
@@ -69,19 +73,18 @@ class ScrapeWebsite extends Command
|
|||||||
return Command::SUCCESS;
|
return Command::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getProducts(): array
|
private function getProducts(Country $country): array
|
||||||
{
|
{
|
||||||
$products = [];
|
$products = [];
|
||||||
$page = 0;
|
$page = 0;
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
// $res = $this->client->request('POST', 'https://uk.ryobitools.eu/api/product-listing/get-products', [
|
$res = $this->client->request('POST', $country->productUrl, [
|
||||||
$res = $this->client->request('POST', 'https://pl.ryobitools.eu/api/product-listing/get-products', [
|
|
||||||
'form_params' => [
|
'form_params' => [
|
||||||
"includePreviousPages" => false,
|
"includePreviousPages" => false,
|
||||||
"pageIndex" => $page,
|
"pageIndex" => $page,
|
||||||
"pageSize" => 100,
|
"pageSize" => 100,
|
||||||
"cultureCode" => "pl-PL",
|
"cultureCode" => $country->cultureCode,
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
$responseObject = json_decode($res->getBody()->getContents());
|
$responseObject = json_decode($res->getBody()->getContents());
|
||||||
@@ -96,7 +99,7 @@ class ScrapeWebsite extends Command
|
|||||||
return $products;
|
return $products;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function saveProduct(\stdClass $product): void
|
private function saveProduct(\stdClass $product, Country $country): void
|
||||||
{
|
{
|
||||||
/** @var Product $productModel */
|
/** @var Product $productModel */
|
||||||
$productModel = Product::firstOrNew(['skuID' => $product->skuID]);
|
$productModel = Product::firstOrNew(['skuID' => $product->skuID]);
|
||||||
|
|||||||
22
src/Models/Country.php
Normal file
22
src/Models/Country.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ namespace Krzysiej\RyobiCrawler\Models;
|
|||||||
use Carbon\Traits\Date;
|
use Carbon\Traits\Date;
|
||||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
|
||||||
@@ -32,6 +33,11 @@ class Product extends Model
|
|||||||
public $timestamps = true;
|
public $timestamps = true;
|
||||||
public $fillable = ['skuID'];
|
public $fillable = ['skuID'];
|
||||||
|
|
||||||
|
public function country(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Country::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function price(): HasMany
|
public function price(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Price::class);
|
return $this->hasMany(Price::class);
|
||||||
|
|||||||
Reference in New Issue
Block a user