From c2eab190459f7ddb34aef504557292fec06baf90 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Tue, 30 Jun 2026 20:54:00 +0300 Subject: [PATCH] Update: 2026-06-30 20:54:00 --- backend/bot/cron_empty_results_to_db.php | 47 +++++++++++++++++------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/backend/bot/cron_empty_results_to_db.php b/backend/bot/cron_empty_results_to_db.php index 16f9e663..de9d4120 100644 --- a/backend/bot/cron_empty_results_to_db.php +++ b/backend/bot/cron_empty_results_to_db.php @@ -38,25 +38,44 @@ $insertedCount = 0; $stmt = $con->prepare("INSERT INTO scraped_competitor_prices (task_id, app_name, start_location, end_location, price_amount, currency) VALUES (?, ?, ?, ?, ?, ?)"); foreach ($data as $row) { + if (isset($row['status']) && $row['status'] !== 'success') { + continue; + } + $taskId = $row['task_id'] ?? null; - $appName = $row['app'] ?? 'Unknown'; - $startLoc = $row['start_location'] ?? ''; - $endLoc = $row['end_location'] ?? ''; - $priceStr = $row['price'] ?? ''; + $resultData = $row['result_data'] ?? []; + + $appName = $resultData['app'] ?? $row['app'] ?? 'Unknown'; + + $startLoc = $resultData['start_location'] ?? $row['start_location'] ?? ''; + if (empty($startLoc) && !empty($resultData['start_lat'])) { + $startLoc = "Lat: {$resultData['start_lat']}, Lng: {$resultData['start_lng']}"; + } + + $endLoc = $resultData['end_location'] ?? $row['end_location'] ?? ''; + if (empty($endLoc) && !empty($resultData['end_lat'])) { + $endLoc = "Lat: {$resultData['end_lat']}, Lng: {$resultData['end_lng']}"; + } + + $priceRaw = $resultData['price'] ?? $row['price'] ?? 0; - // Extract numeric amount and currency from string like "2.5 JOD" $amount = 0.0; $currency = 'JOD'; - // Match numeric parts (including decimals) and text parts - if (preg_match('/([\d\.]+)\s*([A-Za-z]+|د\.ا)/', $priceStr, $matches)) { - $amount = (float)$matches[1]; - $currency = $matches[2]; - if ($currency === 'د.ا') { - $currency = 'JOD'; - } + if (is_numeric($priceRaw)) { + $amount = (float)$priceRaw; } else { - $amount = (float)$priceStr; + $priceStr = (string)$priceRaw; + // Match numeric parts (including decimals) and text parts + if (preg_match('/([\d\.]+)\s*([A-Za-z]+|د\.ا)/', $priceStr, $matches)) { + $amount = (float)$matches[1]; + $currency = $matches[2]; + if ($currency === 'د.ا') { + $currency = 'JOD'; + } + } else { + $amount = (float)$priceStr; + } } // Ignore invalid entries without an amount @@ -66,6 +85,8 @@ foreach ($data as $row) { if ($stmt->execute([$taskId, $appName, $startLoc, $endLoc, $amount, $currency])) { $insertedCount++; + } else { + echo "Failed to insert task_id: $taskId. Error: " . implode(" ", $stmt->errorInfo()) . "\n"; } }