Add price and product models
This commit is contained in:
21
browser.php
21
browser.php
@@ -3,6 +3,8 @@
|
||||
include_once 'vendor/autoload.php';
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Krzysiej\RyobiCrawler\Models\Price;
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection([
|
||||
@@ -11,29 +13,34 @@ $capsule->addConnection([
|
||||
'prefix' => '',
|
||||
]);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
echo '<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"></head>';
|
||||
echo "<table class='table table-hover'>";
|
||||
if (isset($_GET['product_id'])) {
|
||||
$products = Capsule::table('product')->leftJoin('price', 'product.id', '=', 'price.product_id')->where('product.id', '=', $_GET['product_id'])->get();
|
||||
foreach ($products as $product) {
|
||||
$product = Product::with('price')->find($_GET['product_id']);
|
||||
echo "<tr>
|
||||
<td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td>
|
||||
<td><a href='?product_id=$product->id'>$product->name</a></td>
|
||||
<td>$product->subTitle</td>
|
||||
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>
|
||||
<td>$product->price</td>
|
||||
<td>$product->created_at</td>
|
||||
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>";
|
||||
/** @var Price $price */
|
||||
foreach ($product->price as $price) {
|
||||
echo "<tr>
|
||||
<td>$price->price</td>
|
||||
<td>$price->lowestProductPrice30Days</td>
|
||||
<td>$price->productStandardPrice</td>
|
||||
<td>$price->created_at</td>
|
||||
</tr>";
|
||||
}
|
||||
} else {
|
||||
$products = Capsule::table('product')->get();
|
||||
$products = Product::with('price')->get();
|
||||
foreach ($products as $product) {
|
||||
echo "<tr>
|
||||
<td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td>
|
||||
<td><a href='?product_id=$product->id'>$product->name</a></td>
|
||||
<td>$product->subTitle</td>
|
||||
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>
|
||||
<td>$product->price</td>
|
||||
<td>{$product->price->last()->price}</td>
|
||||
</tr>";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,5 +4,10 @@
|
||||
"symfony/var-dumper": "^7.0",
|
||||
"illuminate/database": "^11.0",
|
||||
"ext-json": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Krzysiej\\RyobiCrawler\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
47
index.php
47
index.php
@@ -3,6 +3,8 @@
|
||||
include_once 'vendor/autoload.php';
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Krzysiej\RyobiCrawler\Models\Price;
|
||||
use Krzysiej\RyobiCrawler\Models\Product;
|
||||
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
echo 'Execute this script in cli only';
|
||||
@@ -14,6 +16,7 @@ $capsule->addConnection([
|
||||
'database' => __DIR__ . '/database.sqlite',
|
||||
]);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$client = new GuzzleHttp\Client();
|
||||
$page = 0;
|
||||
do {
|
||||
@@ -29,29 +32,27 @@ do {
|
||||
$responseObject = json_decode($res->getBody()->getContents());
|
||||
$products = $responseObject->products;
|
||||
foreach ($products as $product) {
|
||||
Capsule::table('product')->updateOrInsert([
|
||||
'skuID' => $product->skuID,
|
||||
], [
|
||||
'name' => $product->name,
|
||||
'availableQuantity' => $product->availableQuantity,
|
||||
'stock' => $product->stock,
|
||||
'categories' => json_encode($product->categories),
|
||||
'image' => $product->image,
|
||||
'subTitle' => $product->subTitle,
|
||||
'variantCode' => $product->variantCode,
|
||||
'modelCode' => $product->modelCode,
|
||||
'url' => $product->url,
|
||||
'updated_at' => date('Y-m-d'),
|
||||
]);
|
||||
$databaseProduct = Capsule::table('product')->where('skuID', '=', $product->skuID)->first();
|
||||
Capsule::table('price')->updateOrInsert([
|
||||
'product_id' => $databaseProduct->id,
|
||||
'created_at' => date('Y-m-d'),
|
||||
], [
|
||||
'price' => $product->productPrice,
|
||||
'productStandardPrice' => $product->productStandardPrice,
|
||||
'lowestProductPrice30Days' => $product->lowestProductPrice30Days,
|
||||
]);
|
||||
/** @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";
|
||||
|
||||
@@ -17,11 +17,12 @@ $capsule->addConnection([
|
||||
]);
|
||||
$capsule->setAsGlobal();
|
||||
|
||||
if (!Capsule::schema()->hasTable('product')) {
|
||||
Capsule::schema()->create('product', function (Blueprint $table) {
|
||||
if (!Capsule::schema()->hasTable('products')) {
|
||||
Capsule::schema()->create('products', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->string('skuID');
|
||||
$table->integer('skuID')->unique();
|
||||
$table->integer('agilityID');
|
||||
$table->integer('availableQuantity');
|
||||
$table->integer('stock');
|
||||
$table->json('categories');
|
||||
@@ -35,8 +36,8 @@ if (!Capsule::schema()->hasTable('product')) {
|
||||
}
|
||||
|
||||
|
||||
if (!Capsule::schema()->hasTable('price')) {
|
||||
Capsule::schema()->create('price', function (Blueprint $table) {
|
||||
if (!Capsule::schema()->hasTable('prices')) {
|
||||
Capsule::schema()->create('prices', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->foreignId('product_id');
|
||||
$table->float('price');
|
||||
|
||||
21
src/Models/Price.php
Normal file
21
src/Models/Price.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @property float $price
|
||||
* @property float $productStandardPrice
|
||||
* @property float $lowestProductPrice30Days
|
||||
*/
|
||||
class Price extends Model
|
||||
{
|
||||
public $timestamps = true;
|
||||
|
||||
public function product(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
}
|
||||
29
src/Models/Product.php
Normal file
29
src/Models/Product.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Krzysiej\RyobiCrawler\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* @property integer $skuID
|
||||
* @property integer $agilityID
|
||||
* @property string $name
|
||||
* @property integer $availableQuantity
|
||||
* @property integer $stock
|
||||
* @property string[] $categories
|
||||
* @property string $image
|
||||
* @property string $subTitle
|
||||
* @property string $variantCode
|
||||
* @property string $modelCode
|
||||
* @property string $url
|
||||
*/
|
||||
class Product extends Model
|
||||
{
|
||||
public $timestamps = true;
|
||||
public $fillable = ['skuID'];
|
||||
public function price(): HasMany
|
||||
{
|
||||
return $this->hasMany(Price::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user