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';
|
include_once 'vendor/autoload.php';
|
||||||
|
|
||||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||||
|
use Krzysiej\RyobiCrawler\Models\Price;
|
||||||
|
use Krzysiej\RyobiCrawler\Models\Product;
|
||||||
|
|
||||||
$capsule = new Capsule;
|
$capsule = new Capsule;
|
||||||
$capsule->addConnection([
|
$capsule->addConnection([
|
||||||
@@ -11,29 +13,34 @@ $capsule->addConnection([
|
|||||||
'prefix' => '',
|
'prefix' => '',
|
||||||
]);
|
]);
|
||||||
$capsule->setAsGlobal();
|
$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 '<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'>";
|
echo "<table class='table table-hover'>";
|
||||||
if (isset($_GET['product_id'])) {
|
if (isset($_GET['product_id'])) {
|
||||||
$products = Capsule::table('product')->leftJoin('price', 'product.id', '=', 'price.product_id')->where('product.id', '=', $_GET['product_id'])->get();
|
$product = Product::with('price')->find($_GET['product_id']);
|
||||||
foreach ($products as $product) {
|
|
||||||
echo "<tr>
|
echo "<tr>
|
||||||
<td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td>
|
<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><a href='?product_id=$product->id'>$product->name</a></td>
|
||||||
<td>$product->subTitle</td>
|
<td>$product->subTitle</td>
|
||||||
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>
|
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>";
|
||||||
<td>$product->price</td>
|
/** @var Price $price */
|
||||||
<td>$product->created_at</td>
|
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>";
|
</tr>";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$products = Capsule::table('product')->get();
|
$products = Product::with('price')->get();
|
||||||
foreach ($products as $product) {
|
foreach ($products as $product) {
|
||||||
echo "<tr>
|
echo "<tr>
|
||||||
<td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td>
|
<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><a href='?product_id=$product->id'>$product->name</a></td>
|
||||||
<td>$product->subTitle</td>
|
<td>$product->subTitle</td>
|
||||||
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>
|
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>
|
||||||
<td>$product->price</td>
|
<td>{$product->price->last()->price}</td>
|
||||||
</tr>";
|
</tr>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,10 @@
|
|||||||
"symfony/var-dumper": "^7.0",
|
"symfony/var-dumper": "^7.0",
|
||||||
"illuminate/database": "^11.0",
|
"illuminate/database": "^11.0",
|
||||||
"ext-json": "*"
|
"ext-json": "*"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Krzysiej\\RyobiCrawler\\": "src/"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
47
index.php
47
index.php
@@ -3,6 +3,8 @@
|
|||||||
include_once 'vendor/autoload.php';
|
include_once 'vendor/autoload.php';
|
||||||
|
|
||||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||||
|
use Krzysiej\RyobiCrawler\Models\Price;
|
||||||
|
use Krzysiej\RyobiCrawler\Models\Product;
|
||||||
|
|
||||||
if (php_sapi_name() !== 'cli') {
|
if (php_sapi_name() !== 'cli') {
|
||||||
echo 'Execute this script in cli only';
|
echo 'Execute this script in cli only';
|
||||||
@@ -14,6 +16,7 @@ $capsule->addConnection([
|
|||||||
'database' => __DIR__ . '/database.sqlite',
|
'database' => __DIR__ . '/database.sqlite',
|
||||||
]);
|
]);
|
||||||
$capsule->setAsGlobal();
|
$capsule->setAsGlobal();
|
||||||
|
$capsule->bootEloquent();
|
||||||
$client = new GuzzleHttp\Client();
|
$client = new GuzzleHttp\Client();
|
||||||
$page = 0;
|
$page = 0;
|
||||||
do {
|
do {
|
||||||
@@ -29,29 +32,27 @@ do {
|
|||||||
$responseObject = json_decode($res->getBody()->getContents());
|
$responseObject = json_decode($res->getBody()->getContents());
|
||||||
$products = $responseObject->products;
|
$products = $responseObject->products;
|
||||||
foreach ($products as $product) {
|
foreach ($products as $product) {
|
||||||
Capsule::table('product')->updateOrInsert([
|
/** @var Product $productModel */
|
||||||
'skuID' => $product->skuID,
|
$productModel = Product::firstOrNew(['skuID' => $product->skuID]);
|
||||||
], [
|
$productModel->skuID = $product->skuID;
|
||||||
'name' => $product->name,
|
$productModel->name = $product->name;
|
||||||
'availableQuantity' => $product->availableQuantity,
|
$productModel->availableQuantity = $product->availableQuantity;
|
||||||
'stock' => $product->stock,
|
$productModel->stock = $product->stock;
|
||||||
'categories' => json_encode($product->categories),
|
$productModel->categories = json_encode($product->categories);
|
||||||
'image' => $product->image,
|
$productModel->image = $product->image;
|
||||||
'subTitle' => $product->subTitle,
|
$productModel->subTitle = $product->subTitle;
|
||||||
'variantCode' => $product->variantCode,
|
$productModel->variantCode = $product->variantCode;
|
||||||
'modelCode' => $product->modelCode,
|
$productModel->modelCode = $product->modelCode;
|
||||||
'url' => $product->url,
|
$productModel->url = $product->url;
|
||||||
'updated_at' => date('Y-m-d'),
|
$productModel->save();
|
||||||
]);
|
$priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||||
$databaseProduct = Capsule::table('product')->where('skuID', '=', $product->skuID)->first();
|
if (!$priceExists) {
|
||||||
Capsule::table('price')->updateOrInsert([
|
$price = new Price();
|
||||||
'product_id' => $databaseProduct->id,
|
$price->price = $product->productPrice;
|
||||||
'created_at' => date('Y-m-d'),
|
$price->productStandardPrice = $product->productStandardPrice;
|
||||||
], [
|
$price->lowestProductPrice30Days = $product->lowestProductPrice30Days;
|
||||||
'price' => $product->productPrice,
|
$productModel->price()->save($price);
|
||||||
'productStandardPrice' => $product->productStandardPrice,
|
}
|
||||||
'lowestProductPrice30Days' => $product->lowestProductPrice30Days,
|
|
||||||
]);
|
|
||||||
echo ".";
|
echo ".";
|
||||||
}
|
}
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|||||||
@@ -17,11 +17,12 @@ $capsule->addConnection([
|
|||||||
]);
|
]);
|
||||||
$capsule->setAsGlobal();
|
$capsule->setAsGlobal();
|
||||||
|
|
||||||
if (!Capsule::schema()->hasTable('product')) {
|
if (!Capsule::schema()->hasTable('products')) {
|
||||||
Capsule::schema()->create('product', function (Blueprint $table) {
|
Capsule::schema()->create('products', function (Blueprint $table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
$table->string('skuID');
|
$table->integer('skuID')->unique();
|
||||||
|
$table->integer('agilityID');
|
||||||
$table->integer('availableQuantity');
|
$table->integer('availableQuantity');
|
||||||
$table->integer('stock');
|
$table->integer('stock');
|
||||||
$table->json('categories');
|
$table->json('categories');
|
||||||
@@ -35,8 +36,8 @@ if (!Capsule::schema()->hasTable('product')) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!Capsule::schema()->hasTable('price')) {
|
if (!Capsule::schema()->hasTable('prices')) {
|
||||||
Capsule::schema()->create('price', function (Blueprint $table) {
|
Capsule::schema()->create('prices', function (Blueprint $table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->foreignId('product_id');
|
$table->foreignId('product_id');
|
||||||
$table->float('price');
|
$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