144 lines
5.4 KiB
PHP
144 lines
5.4 KiB
PHP
<?php
|
|
// =========================================================
|
|
// backend/bot/generate_price_tasks.php
|
|
// Cron Job: Runs every 15 minutes
|
|
// Generates random Short & Long trips in Damascus
|
|
// =========================================================
|
|
|
|
// CLI or Web execution
|
|
require_once __DIR__ . '/../core/bootstrap.php';
|
|
require_once __DIR__ . '/../functions.php';
|
|
|
|
try {
|
|
$con = Database::get('main');
|
|
} catch (Exception $e) {
|
|
die("Database connection failed\n");
|
|
}
|
|
|
|
// 1. Ensure Table Exists
|
|
$sql = "
|
|
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,
|
|
`competitor_name` varchar(100) NOT NULL,
|
|
`start_location` varchar(255) NOT NULL,
|
|
`end_location` varchar(255) NOT NULL,
|
|
`start_lat` decimal(10,7) DEFAULT NULL,
|
|
`start_lng` decimal(10,7) DEFAULT NULL,
|
|
`end_lat` decimal(10,7) DEFAULT NULL,
|
|
`end_lng` decimal(10,7) DEFAULT NULL,
|
|
`price_amount` decimal(8,2) NOT NULL,
|
|
`price_per_km` decimal(8,2) NOT NULL,
|
|
`currency` varchar(10) NOT NULL DEFAULT 'JOD',
|
|
`country_code` varchar(10) NOT NULL DEFAULT 'JO',
|
|
`scraped_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
KEY `idx_app_name` (`app_name`),
|
|
KEY `idx_competitor_name` (`competitor_name`),
|
|
KEY `idx_start_location` (`start_location`),
|
|
KEY `idx_country_code` (`country_code`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
";
|
|
$con->exec($sql);
|
|
|
|
// 2. Ten Key Regions in Damascus (Syria) and Amman (Jordan)
|
|
$countriesConfig = [
|
|
'SY' => [
|
|
'competitors' => ['yallago', 'zaken', 'tufaddal'],
|
|
'regions' => [
|
|
['name' => 'Umayyad Square', 'lat' => 33.5138, 'lng' => 36.2765],
|
|
['name' => 'Mezzeh', 'lat' => 33.5074, 'lng' => 36.2530],
|
|
['name' => 'Malki', 'lat' => 33.5220, 'lng' => 36.2840],
|
|
['name' => 'Kafersouseh', 'lat' => 33.4981, 'lng' => 36.2730],
|
|
['name' => 'Al-Midan', 'lat' => 33.4947, 'lng' => 36.2995],
|
|
['name' => 'Bab Tuma', 'lat' => 33.5126, 'lng' => 36.3150],
|
|
['name' => 'Rukneddine', 'lat' => 33.5350, 'lng' => 36.2950],
|
|
['name' => 'Dummar', 'lat' => 33.5385, 'lng' => 36.2250],
|
|
['name' => 'Baramkeh', 'lat' => 33.5100, 'lng' => 36.2885],
|
|
['name' => 'Muhajireen', 'lat' => 33.5320, 'lng' => 36.2720],
|
|
]
|
|
],
|
|
'JO' => [
|
|
'competitors' => ['com.careem.acma', 'com.ubercab', 'com.taxif.passenger', 'me.com.easytaxi'],
|
|
'regions' => [
|
|
['name' => 'Abdoun', 'lat' => 31.9392, 'lng' => 35.8942],
|
|
['name' => 'Jabal Amman', 'lat' => 31.9511, 'lng' => 35.9189],
|
|
['name' => 'Sweileh', 'lat' => 32.0167, 'lng' => 35.8333],
|
|
['name' => 'Khalda', 'lat' => 31.9861, 'lng' => 35.8450],
|
|
['name' => 'Al-Jubaiha', 'lat' => 32.0194, 'lng' => 35.8753],
|
|
['name' => 'Tla Al-Ali', 'lat' => 31.9961, 'lng' => 35.8647],
|
|
['name' => 'Shmeisani', 'lat' => 31.9680, 'lng' => 35.9020],
|
|
['name' => 'Um Uthaina', 'lat' => 31.9610, 'lng' => 35.8770],
|
|
['name' => 'Jabal Al-Weibdeh', 'lat' => 31.9560, 'lng' => 35.9220],
|
|
['name' => 'Marj Al-Hamam', 'lat' => 31.9000, 'lng' => 35.8500],
|
|
]
|
|
]
|
|
];
|
|
|
|
// Helper to generate a random point within a radius (in km)
|
|
function generateRandomPoint($lat, $lng, $radius) {
|
|
$radiusInDegrees = $radius / 111.0; // 1 degree is ~111km
|
|
$u = lcg_value();
|
|
$v = lcg_value();
|
|
$w = $radiusInDegrees * sqrt($u);
|
|
$t = 2 * pi() * $v;
|
|
$x = $w * cos($t);
|
|
$y = $w * sin($t);
|
|
|
|
// Adjust longitude based on latitude
|
|
$new_lng = $x / cos(deg2rad($lat));
|
|
$new_lat = $y;
|
|
|
|
return [
|
|
'lat' => $lat + $new_lat,
|
|
'lng' => $lng + $new_lng
|
|
];
|
|
}
|
|
|
|
$tasksCreated = 0;
|
|
|
|
foreach ($countriesConfig as $countryCode => $config) {
|
|
$competitors = $config['competitors'];
|
|
$regions = $config['regions'];
|
|
|
|
foreach ($regions as $region) {
|
|
// A. Generate Start Point (within 2km of region center)
|
|
$start = generateRandomPoint($region['lat'], $region['lng'], 2);
|
|
|
|
// B. Generate Short Trip (2-5 km from start)
|
|
$shortDist = rand(20, 50) / 10.0;
|
|
$shortEnd = generateRandomPoint($start['lat'], $start['lng'], $shortDist);
|
|
|
|
// C. Generate Long Trip (10-15 km from start)
|
|
$longDist = rand(100, 150) / 10.0;
|
|
$longEnd = generateRandomPoint($start['lat'], $start['lng'], $longDist);
|
|
|
|
$trips = [$shortEnd, $longEnd];
|
|
|
|
foreach ($trips as $end) {
|
|
foreach ($competitors as $app) {
|
|
$taskId = "prc_" . uniqid();
|
|
|
|
$taskData = [
|
|
"task_id" => $taskId,
|
|
"type" => "price_check",
|
|
"app" => $app,
|
|
"payload" => [
|
|
"start_lat" => $start['lat'],
|
|
"start_lng" => $start['lng'],
|
|
"end_lat" => $end['lat'],
|
|
"end_lng" => $end['lng']
|
|
]
|
|
];
|
|
|
|
// Push to Redis Queue
|
|
$redis->lpush('queue:bot:tasks', json_encode($taskData));
|
|
$tasksCreated++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
echo "Successfully generated and queued $tasksCreated pricing tasks.\n";
|