From d96fa63298c5d17253911e4d799960ba40b30ceb Mon Sep 17 00:00:00 2001
From: Hickmeister <35031453+Hickmeister@users.noreply.github.com>
Date: Sun, 5 Jan 2025 03:39:59 +0000
Subject: [PATCH] Added price tracking cards
---
public/viewFilament.php | 293 ++++++++++++++++++++
src/filamentTracker/addFilament.php | 6 +-
src/filamentTracker/getFilamentPrices.php | 42 +--
src/filamentTracker/trackFilamentPrices.php | 98 -------
4 files changed, 316 insertions(+), 123 deletions(-)
create mode 100644 public/viewFilament.php
delete mode 100644 src/filamentTracker/trackFilamentPrices.php
diff --git a/public/viewFilament.php b/public/viewFilament.php
new file mode 100644
index 0000000..472f272
--- /dev/null
+++ b/public/viewFilament.php
@@ -0,0 +1,293 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ TOD Dashboard
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/filamentTracker/addFilament.php b/src/filamentTracker/addFilament.php
index ea4bb98..0993f63 100644
--- a/src/filamentTracker/addFilament.php
+++ b/src/filamentTracker/addFilament.php
@@ -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;
diff --git a/src/filamentTracker/getFilamentPrices.php b/src/filamentTracker/getFilamentPrices.php
index ab25444..ef788e4 100644
--- a/src/filamentTracker/getFilamentPrices.php
+++ b/src/filamentTracker/getFilamentPrices.php
@@ -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' => []
];
}
diff --git a/src/filamentTracker/trackFilamentPrices.php b/src/filamentTracker/trackFilamentPrices.php
deleted file mode 100644
index 4293255..0000000
--- a/src/filamentTracker/trackFilamentPrices.php
+++ /dev/null
@@ -1,98 +0,0 @@
-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();
- }
-}
-?>