Add conversion rate to products, prices, and save them while scraping.
All checks were successful
/ deploy-job (push) Successful in 1s
All checks were successful
/ deploy-job (push) Successful in 1s
This commit is contained in:
@@ -246,6 +246,16 @@ class Migrate extends Command
|
|||||||
$table->json('promotions')->nullable();
|
$table->json('promotions')->nullable();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (!Capsule::schema()->hasColumn('prices', 'conversionRate')) {
|
||||||
|
Capsule::schema()->table('prices', function (Blueprint $table) {
|
||||||
|
$table->float('conversionRate')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!Capsule::schema()->hasColumn('products', 'conversionRate')) {
|
||||||
|
Capsule::schema()->table('products', function (Blueprint $table) {
|
||||||
|
$table->float('conversionRate')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function index(): void
|
public function index(): void
|
||||||
|
|||||||
@@ -23,13 +23,13 @@ class ScrapeWebsite extends Command
|
|||||||
{
|
{
|
||||||
const COUNTRY_ID = 'country';
|
const COUNTRY_ID = 'country';
|
||||||
private Client $client;
|
private Client $client;
|
||||||
|
private array $rates;
|
||||||
|
|
||||||
public function __construct(protected Capsule $database)
|
public function __construct(protected Capsule $database)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->client = new Client();
|
$this->client = new Client();
|
||||||
$this->getCurrencyExchange();
|
$this->rates = $this->getCurrencyExchange();
|
||||||
die();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure(): void
|
protected function configure(): void
|
||||||
@@ -70,6 +70,7 @@ class ScrapeWebsite extends Command
|
|||||||
$product->priceLowest = $product->lowestPrice->price;
|
$product->priceLowest = $product->lowestPrice->price;
|
||||||
$product->lastSeen = $newestPrice->created_at->format('Y-m-d');
|
$product->lastSeen = $newestPrice->created_at->format('Y-m-d');
|
||||||
$product->stock = $currentStock->stock;
|
$product->stock = $currentStock->stock;
|
||||||
|
$product->conversionRate = $newestPrice->conversionRate;
|
||||||
$product->save(['timestamps' => false]);
|
$product->save(['timestamps' => false]);
|
||||||
$progress->advance();
|
$progress->advance();
|
||||||
}
|
}
|
||||||
@@ -100,7 +101,7 @@ class ScrapeWebsite extends Command
|
|||||||
$products = array_merge($products, $responseObject->products);
|
$products = array_merge($products, $responseObject->products);
|
||||||
$page++;
|
$page++;
|
||||||
$canLoadMore = $responseObject->canLoadMore;
|
$canLoadMore = $responseObject->canLoadMore;
|
||||||
} catch (GuzzleException $e) {
|
} catch (GuzzleException) {
|
||||||
return $products;
|
return $products;
|
||||||
}
|
}
|
||||||
} while ($canLoadMore);
|
} while ($canLoadMore);
|
||||||
@@ -134,6 +135,7 @@ class ScrapeWebsite extends Command
|
|||||||
$price->price = $product->productPrice;
|
$price->price = $product->productPrice;
|
||||||
$price->productStandardPrice = $product->productStandardPrice;
|
$price->productStandardPrice = $product->productStandardPrice;
|
||||||
$price->lowestProductPrice30Days = $product->lowestProductPrice30Days;
|
$price->lowestProductPrice30Days = $product->lowestProductPrice30Days;
|
||||||
|
$price->conversionRate = $this->getConversionRate($country->currency);
|
||||||
$productModel->price()->save($price);
|
$productModel->price()->save($price);
|
||||||
}
|
}
|
||||||
$stockExist = $productModel->stock()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
$stockExist = $productModel->stock()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists();
|
||||||
@@ -150,11 +152,21 @@ class ScrapeWebsite extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCurrencyExchange(): void {
|
public function getCurrencyExchange(): array
|
||||||
|
{
|
||||||
$result = $this->client->request('GET', 'https://api.nbp.pl/api/exchangerates/tables/A/?format=json');
|
$result = $this->client->request('GET', 'https://api.nbp.pl/api/exchangerates/tables/A/?format=json');
|
||||||
dd(json_decode($result->getBody()->getContents()));
|
$rates = ['PLN' => 1.0];
|
||||||
|
foreach(json_decode($result->getBody()->getContents(),true)[0]['rates'] as $rate){
|
||||||
|
$rates[$rate['code']] = $rate['mid'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rates;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getConversionRate(string $currency): float
|
||||||
|
{
|
||||||
|
$currency = strtoupper($currency);
|
||||||
|
|
||||||
|
return $this->rates[$currency];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|||||||
* @property float $price
|
* @property float $price
|
||||||
* @property float $productStandardPrice
|
* @property float $productStandardPrice
|
||||||
* @property float $lowestProductPrice30Days
|
* @property float $lowestProductPrice30Days
|
||||||
|
* @property float $conversionRate
|
||||||
*/
|
*/
|
||||||
class Price extends Model
|
class Price extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ use function Symfony\Component\Clock\now;
|
|||||||
* @property float $priceLowest
|
* @property float $priceLowest
|
||||||
* @property float $productStandardPrice
|
* @property float $productStandardPrice
|
||||||
* @property float $lowestProductPrice30Days
|
* @property float $lowestProductPrice30Days
|
||||||
|
* @property float $conversionRate
|
||||||
* @property Date $lastSeen
|
* @property Date $lastSeen
|
||||||
* @property integer $stock
|
* @property integer $stock
|
||||||
* @property Object $promotions
|
* @property Object $promotions
|
||||||
|
|||||||
Reference in New Issue
Block a user