feature/handle-multiple-countries (#45)
Reviewed-on: #45 Co-authored-by: Krzysiej <krzysiej@gmail.com> Co-committed-by: Krzysiej <krzysiej@gmail.com>
This commit was merged in pull request #45.
This commit is contained in:
@@ -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;
|
||||
@@ -36,16 +36,21 @@ 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."\n");
|
||||
$progress->start();
|
||||
$products = $this->getProducts($country);
|
||||
$progress->setMaxSteps(count($products));
|
||||
foreach ($products as $product) {
|
||||
$this->saveProduct($product, $country);
|
||||
$progress->advance();
|
||||
}
|
||||
$progress->finish();
|
||||
$output->writeln('');
|
||||
$output->writeln('Scrape products - DONE');
|
||||
$output->writeln('');
|
||||
}
|
||||
$progress->finish();
|
||||
$output->writeln('Scrape products - DONE');
|
||||
$output->writeln('');
|
||||
|
||||
$output->writeln('Update prices');
|
||||
$products = Product::all();
|
||||
@@ -69,18 +74,19 @@ 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://pl.ryobitools.eu/api/product-listing/get-products', [
|
||||
$res = $this->client->request('POST', $country->productsUrl, [
|
||||
'form_params' => [
|
||||
"includePreviousPages" => false,
|
||||
"pageIndex" => $page,
|
||||
"pageSize" => 100,
|
||||
"cultureCode" => "pl-PL",
|
||||
"cultureCode" => $country->cultureCode,
|
||||
]
|
||||
]);
|
||||
$responseObject = json_decode($res->getBody()->getContents());
|
||||
@@ -95,10 +101,14 @@ class ScrapeWebsite extends Command
|
||||
return $products;
|
||||
}
|
||||
|
||||
private function saveProduct(\stdClass $product): void
|
||||
private function saveProduct(\stdClass $product, Country $country): void
|
||||
{
|
||||
// if ($product->skuID == 0) {
|
||||
// dump([$product->skuID, $product->name]);
|
||||
// }
|
||||
// return;
|
||||
/** @var Product $productModel */
|
||||
$productModel = Product::firstOrNew(['skuID' => $product->skuID]);
|
||||
$productModel = Product::firstOrNew(['skuID' => $product->skuID, 'country_id' => $country->id]);
|
||||
|
||||
$productModel->skuID = $product->skuID;
|
||||
$productModel->name = $product->name;
|
||||
@@ -111,6 +121,7 @@ class ScrapeWebsite extends Command
|
||||
$productModel->url = $product->url;
|
||||
$productModel->lastSeen = date("Y-m-d");
|
||||
$productModel->touch('updated_at');
|
||||
$productModel->country()->associate($country);
|
||||
$productModel->save();
|
||||
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user