Change application to symfony/console application
This commit is contained in:
58
index.php
58
index.php
@@ -2,59 +2,17 @@
|
||||
|
||||
include_once 'vendor/autoload.php';
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Krzysiej\RyobiCrawler\Models\Price;
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
|
||||
use Krzysiej\RyobiCrawler\Command\ScrapeWebsite;
|
||||
|
||||
use Symfony\Component\Console\Application;
|
||||
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
header('Location: browser.php');
|
||||
echo 'Execute this script in cli only';
|
||||
exit;
|
||||
}
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => __DIR__ . '/database.sqlite',
|
||||
]);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$client = new GuzzleHttp\Client();
|
||||
$page = 0;
|
||||
do {
|
||||
$res = $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);
|
||||
|
||||
$application = new Application('Ryobi website scraper application', '1.0.0');
|
||||
$application->add(new ScrapeWebsite());
|
||||
$application->run();
|
||||
|
||||
Reference in New Issue
Block a user