Change application to symfony/console application
This commit is contained in:
77
src/Command/ScrapeWebsite.php
Normal file
77
src/Command/ScrapeWebsite.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Command;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Krzysiej\RyobiCrawler\Models\Price;
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
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\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'app:scrape')]
|
||||
class ScrapeWebsite extends Command
|
||||
{
|
||||
private Client $client;
|
||||
private Capsule $capsule;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->capsule = new Capsule;
|
||||
$this->capsule->addConnection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => __DIR__ .'/../../database.sqlite',
|
||||
]);
|
||||
$this->capsule->setAsGlobal();
|
||||
$this->capsule->bootEloquent();
|
||||
$this->client = new Client();
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$page = 0;
|
||||
do {
|
||||
$res = $this->client->request('POST', 'https://pl.ryobitools.eu/api/product-listing/get-products', [
|
||||
'form_params' => [
|
||||
"includePreviousPages" => false,
|
||||
"pageIndex" => $page,
|
||||
"pageSize" => 100,
|
||||
"cultureCode" => "pl-PL",
|
||||
]
|
||||
]);
|
||||
$page++;
|
||||
$responseObject = json_decode($res->getBody()->getContents());
|
||||
$products = $responseObject->products;
|
||||
foreach ($products as $product) {
|
||||
/** @var Product $productModel */
|
||||
$productModel = Product::firstOrNew(['skuID' => $product->skuID]);
|
||||
$productModel->skuID = $product->skuID;
|
||||
$productModel->name = $product->name;
|
||||
$productModel->availableQuantity = $product->availableQuantity;
|
||||
$productModel->stock = $product->stock;
|
||||
$productModel->categories = json_encode($product->categories);
|
||||
$productModel->image = $product->image;
|
||||
$productModel->subTitle = $product->subTitle;
|
||||
$productModel->variantCode = $product->variantCode;
|
||||
$productModel->modelCode = $product->modelCode;
|
||||
$productModel->url = $product->url;
|
||||
$productModel->save();
|
||||
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||
if (!$priceExists) {
|
||||
$price = new Price();
|
||||
$price->price = $product->productPrice;
|
||||
$price->productStandardPrice = $product->productStandardPrice;
|
||||
$price->lowestProductPrice30Days = $product->lowestProductPrice30Days;
|
||||
$productModel->price()->save($price);
|
||||
}
|
||||
echo ".";
|
||||
}
|
||||
echo "\n";
|
||||
} while ((bool)$responseObject->canLoadMore);
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user