scraper update

This commit is contained in:
Hickmeister
2025-01-14 23:24:23 +00:00
parent 030d72c202
commit cc4e915cf5
2 changed files with 16 additions and 4 deletions

View File

@@ -48,7 +48,10 @@ try {
'color' => $filament['color'],
'amazonUrl' => $filament['amazonUrl'],
'prices' => [],
'currentDiscount' => $filament['currentDiscount'] ? json_decode($filament['currentDiscount'], true) : null // Decode JSON format
'currentDiscount' => $filament['currentDiscount'] ? json_decode($filament['currentDiscount'], true) : [
'discount' => ['value' => 0, 'type' => 'none'],
'voucher' => ['value' => 0, 'type' => 'none']
] // Decode JSON format and provide fallback
];
}

View File

@@ -18,7 +18,17 @@ try {
$scrapedData = scrapeAmazonData($amazonUrl);
if ($scrapedData && $scrapedData['price'] > 0) {
$currentDiscount = json_encode($scrapedData['voucher']); // Encode discount as JSON
// Prepare currentDiscount JSON structure with the correct format
$currentDiscount = json_encode([
'discount' => [
'value' => $scrapedData['currentDiscount']['discount']['value'] ?? 0,
'type' => $scrapedData['currentDiscount']['discount']['type'] ?? 'percentage',
],
'voucher' => [
'value' => $scrapedData['currentDiscount']['voucher']['value'] ?? 0,
'type' => $scrapedData['currentDiscount']['voucher']['type'] ?? null,
],
]);
$stmt = $pdo->prepare("
INSERT INTO filamentPriceHistory (filamentId, price, currentDiscount)
@@ -30,7 +40,7 @@ try {
':currentDiscount' => $currentDiscount
]);
echo "Updated price for {$filament['filamentName']}: £{$scrapedData['price']}, Discount: " . json_encode($scrapedData['voucher']) . "\n";
echo "Updated price for {$filament['filamentName']}: £{$scrapedData['price']}, Current Discount: {$currentDiscount}\n";
} else {
echo "Failed to update {$filament['filamentName']} (no price found or £0).\n";
}
@@ -41,4 +51,3 @@ try {
} catch (PDOException $e) {
echo "Database error: " . $e->getMessage();
}
?>