Update: 2026-06-30 14:32:38

This commit is contained in:
Hamza-Ayed
2026-06-30 14:32:39 +03:00
parent ef0ee91fd9
commit 1f1a3385e3
2 changed files with 92 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
<?php
/**
* cron_empty_results_to_db.php
* سكربت لتفريغ نتائج البوت من ملف results.json إلى قاعدة بيانات MySQL
* يُفضل تشغيله برمجياً كل ساعة أو حسب الحاجة
*/
// Allow script to run indefinitely
set_time_limit(0);
ini_set('memory_limit', '256M');
// Mock request to satisfy connect.php dependencies if any
$_SERVER['REQUEST_METHOD'] = 'POST';
require_once __DIR__ . '/../core/bootstrap.php';
require_once __DIR__ . '/../functions.php';
try {
$con = Database::get('main');
} catch (Exception $e) {
die("Database connection failed: " . $e->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, start_location, end_location, price_amount, currency) VALUES (?, ?, ?, ?, ?, ?)");
foreach ($data as $row) {
$taskId = $row['task_id'] ?? null;
$appName = $row['app'] ?? 'Unknown';
$startLoc = $row['start_location'] ?? '';
$endLoc = $row['end_location'] ?? '';
$priceStr = $row['price'] ?? '';
// 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';
}
} else {
$amount = (float)$priceStr;
}
// Ignore invalid entries without an amount
if ($amount <= 0) {
continue;
}
if ($stmt->execute([$taskId, $appName, $startLoc, $endLoc, $amount, $currency])) {
$insertedCount++;
}
}
// 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";

View File

@@ -1946,3 +1946,20 @@ CREATE TABLE IF NOT EXISTS `market_health_reports` (
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `idx_country_date` (`country_code`, `report_date`) KEY `idx_country_date` (`country_code`, `report_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Table structure for table `scraped_competitor_prices`
--
CREATE TABLE IF NOT EXISTS `scraped_competitor_prices` (
`id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`task_id` varchar(100) DEFAULT NULL,
`app_name` varchar(100) NOT NULL,
`start_location` varchar(255) NOT NULL,
`end_location` varchar(255) NOT NULL,
`price_amount` decimal(8,2) NOT NULL,
`currency` varchar(10) NOT NULL DEFAULT 'JOD',
`scraped_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
KEY `idx_app_name` (`app_name`),
KEY `idx_start_location` (`start_location`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;