getMessage() . "\n"); } $resultsFile = __DIR__ . '/results.json'; if (!file_exists($resultsFile)) { die("No results file found.\n"); } $content = file_get_contents($resultsFile); $data = json_decode($content, true); if (empty($data) || !is_array($data)) { die("No data to process or invalid JSON.\n"); } $insertedCount = 0; $stmt = $con->prepare("INSERT INTO scraped_competitor_prices (task_id, app_name, competitor_name, start_location, end_location, start_lat, start_lng, end_lat, end_lng, price_amount, price_per_km, currency, country_code) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); foreach ($data as $row) { if (isset($row['status']) && $row['status'] !== 'success') { continue; } $taskId = $row['task_id'] ?? null; $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; $amount = 0.0; $currency = 'JOD'; if (is_numeric($priceRaw)) { $amount = (float)$priceRaw; } else { $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 if ($amount <= 0) { continue; } $distanceKm = (float)($resultData['distance_km'] ?? 1); if ($distanceKm <= 0) $distanceKm = 1; $pricePerKm = $amount / $distanceKm; $startLat = $resultData['start_lat'] ?? null; $startLng = $resultData['start_lng'] ?? null; $endLat = $resultData['end_lat'] ?? null; $endLng = $resultData['end_lng'] ?? null; $countryCode = 'JO'; // Default for now, as scraping is in Jordan if ($stmt->execute([ $taskId, $appName, $appName, $startLoc, $endLoc, $startLat, $startLng, $endLat, $endLng, $amount, $pricePerKm, $currency, $countryCode ])) { $insertedCount++; } else { echo "Failed to insert task_id: $taskId. Error: " . implode(" ", $stmt->errorInfo()) . "\n"; } } // Clear the JSON file after successfully inserting data file_put_contents($resultsFile, json_encode([], JSON_PRETTY_PRINT)); echo "Successfully inserted $insertedCount records into the database and cleared results.json.\n";