Added price tracking cards
This commit is contained in:
@@ -4,11 +4,7 @@ require '../db.php';
|
||||
require_once '../envLoader.php';
|
||||
loadEnv(__DIR__ . '/../../.env');
|
||||
|
||||
// Check if the user is logged in
|
||||
if (!isset($_SESSION['userId'])) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'User not authenticated.']);
|
||||
exit;
|
||||
}
|
||||
include '../src/session_check.php';
|
||||
|
||||
use Goutte\Client;
|
||||
|
||||
|
||||
@@ -4,31 +4,32 @@ require '../db.php';
|
||||
require_once '../envLoader.php';
|
||||
loadEnv(__DIR__ . '/../../.env');
|
||||
|
||||
// Check if the user is logged in
|
||||
if (!isset($_SESSION['userId'])) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'User not authenticated.']);
|
||||
exit;
|
||||
}
|
||||
include '../src/session_check.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Fetch all filament prices from the database
|
||||
try {
|
||||
// Fetch all filament prices with a limit of 180 entries per filament using JOIN
|
||||
$stmt = $pdo->query("
|
||||
SELECT
|
||||
ft.filamentName,
|
||||
ft.brand,
|
||||
ft.material,
|
||||
ft.color,
|
||||
fp.price,
|
||||
fp.recordedAt
|
||||
FROM
|
||||
filamentTracker ft
|
||||
JOIN
|
||||
filamentPriceHistory fp ON ft.id = fp.filamentId
|
||||
ORDER BY
|
||||
ft.filamentName,
|
||||
fp.recordedAt ASC
|
||||
SELECT ft.filamentName,
|
||||
ft.brand,
|
||||
ft.material,
|
||||
ft.color,
|
||||
ft.amazonUrl,
|
||||
fp.price,
|
||||
fp.recordedAt
|
||||
FROM filamentTracker ft
|
||||
JOIN filamentPriceHistory fp ON ft.id = fp.filamentId
|
||||
JOIN (
|
||||
SELECT filamentId, recordedAt
|
||||
FROM (
|
||||
SELECT filamentId, recordedAt,
|
||||
ROW_NUMBER() OVER (PARTITION BY filamentId ORDER BY recordedAt DESC) as rn
|
||||
FROM filamentPriceHistory
|
||||
) ranked
|
||||
WHERE rn <= 180
|
||||
) filtered ON fp.filamentId = filtered.filamentId AND fp.recordedAt = filtered.recordedAt
|
||||
ORDER BY ft.filamentName, fp.recordedAt ASC
|
||||
");
|
||||
|
||||
$filaments = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
@@ -44,6 +45,7 @@ try {
|
||||
'brand' => $filament['brand'],
|
||||
'material' => $filament['material'],
|
||||
'color' => $filament['color'],
|
||||
'amazonUrl' => $filament['amazonUrl'],
|
||||
'prices' => []
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
require '../../vendor/autoload.php';
|
||||
require '../db.php';
|
||||
require_once '../envLoader.php';
|
||||
loadEnv(__DIR__ . '/../../.env');
|
||||
|
||||
use Goutte\Client;
|
||||
|
||||
function scrapeFilamentPrice($productName, $productUrl) {
|
||||
$client = new Client();
|
||||
$crawler = $client->request('GET', $productUrl);
|
||||
|
||||
// Extract the current price
|
||||
$price = $crawler->filter('span.a-price-whole')->first()->text();
|
||||
$price = str_replace(',', '', $price); // Clean the price string
|
||||
|
||||
// Extract the original price (if available)
|
||||
$originalPriceNode = $crawler->filter('span.a-price.a-text-price')->first();
|
||||
$originalPrice = $originalPriceNode->count() ? str_replace(',', '', $originalPriceNode->text()) : null;
|
||||
|
||||
// Calculate the discount percentage
|
||||
$discountPercentage = null;
|
||||
if ($originalPrice) {
|
||||
$discountPercentage = round(100 - (($price / $originalPrice) * 100), 2);
|
||||
}
|
||||
|
||||
$currency = 'GBP';
|
||||
|
||||
return [
|
||||
'productName' => $productName,
|
||||
'productUrl' => $productUrl,
|
||||
'price' => $price,
|
||||
'originalPrice' => $originalPrice,
|
||||
'discountPercentage' => $discountPercentage,
|
||||
'currency' => $currency
|
||||
];
|
||||
}
|
||||
|
||||
// Handle Form Submission (Add New Filament to Track)
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$productName = $_POST['productName'];
|
||||
$productUrl = $_POST['productUrl'];
|
||||
|
||||
$result = scrapeFilamentPrice($productName, $productUrl);
|
||||
|
||||
try {
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO filamentPrices (productName, productUrl, price, originalPrice, discountPercentage, currency)
|
||||
VALUES (:productName, :productUrl, :price, :originalPrice, :discountPercentage, :currency)
|
||||
");
|
||||
$stmt->execute([
|
||||
':productName' => $result['productName'],
|
||||
':productUrl' => $result['productUrl'],
|
||||
':price' => $result['price'],
|
||||
':originalPrice' => $result['originalPrice'],
|
||||
':discountPercentage' => $result['discountPercentage'],
|
||||
':currency' => $result['currency']
|
||||
]);
|
||||
|
||||
echo json_encode(['status' => 'success', 'message' => 'Filament price recorded successfully!']);
|
||||
} catch (PDOException $e) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Failed to record price: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Scheduled Scraping for Existing URLs (Cron Job)
|
||||
if (isset($argv[1]) && $argv[1] === 'cron') {
|
||||
try {
|
||||
$stmt = $pdo->query("SELECT * FROM filamentPrices GROUP BY productUrl");
|
||||
$filaments = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
foreach ($filaments as $filament) {
|
||||
$result = scrapeFilamentPrice($filament['productName'], $filament['productUrl']);
|
||||
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO filamentPrices (productName, productUrl, price, originalPrice, discountPercentage, currency)
|
||||
VALUES (:productName, :productUrl, :price, :originalPrice, :discountPercentage, :currency)
|
||||
");
|
||||
$stmt->execute([
|
||||
':productName' => $result['productName'],
|
||||
':productUrl' => $result['productUrl'],
|
||||
':price' => $result['price'],
|
||||
':originalPrice' => $result['originalPrice'],
|
||||
':discountPercentage' => $result['discountPercentage'],
|
||||
':currency' => $result['currency']
|
||||
]);
|
||||
|
||||
echo "Recorded: {$result['productName']} - £{$result['price']} (Discount: {$result['discountPercentage']}%)\n";
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
echo "Failed to fetch filament data: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user