Files
FloatBackOfffice/CRONS/singapore_mrtlrt_stop_fares.php
T
dev-chiefworks f76abffdcd first commit
2022-05-31 16:21:53 -04:00

159 lines
6.4 KiB
PHP

<?php
echo "[".date("Y-m-d H:i:s")."] GPS singaport_mrtlrt_stop_fares job is starting.\n";
require('../backend.php');
$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');
$db_host = $savvyext->cfgReadChar('database.host');
$db_name = $savvyext->cfgReadChar('database.name');
$db_user = $savvyext->cfgReadChar('database.user');
$db_pass = $savvyext->cfgReadChar('database.pass');
$db_port = $savvyext->cfgReadLong('database.port');
$connstr = "host=${db_host} port=${db_port} dbname=${db_name} user=${db_user} password=${db_pass}";
$conn = pg_connect($connstr);
$db_host = $savvyext->cfgReadChar('database_replica.host');
$db_name = $savvyext->cfgReadChar('database_replica.name');
$db_user = $savvyext->cfgReadChar('database_replica.user');
$db_pass = $savvyext->cfgReadChar('database_replica.pass');
$db_port = $savvyext->cfgReadLong('database_replica.port');
$readOnlyReplicaConnstr = "host=${db_host} port=${db_port} dbname=${db_name} user=${db_user} password=${db_pass}";
$readOnlyReplicaConn = pg_connect($readOnlyReplicaConnstr);
$q = "SELECT * FROM singapore_mrtlrt_stops_mytransport ORDER BY RANDOM()";
$r = pg_query($readOnlyReplicaConn, $q);
while ($f=pg_fetch_assoc($r)) {
singapore_mrtlrt_stop_fares($f);
}
echo "[".date("Y-m-d H:i:s")."] GPS singaport_mrtlrt_stop_fares job complete.\n";
function singapore_mrtlrt_stop_fares($from) {
global $readOnlyReplicaConn;
$q = "SELECT * FROM singapore_mrtlrt_stops_mytransport WHERE id<>".$from["id"]." ORDER BY RANDOM()";
$r = pg_query($readOnlyReplicaConn,$q);
while ($to=pg_fetch_assoc($r)) {
singapore_mrtlrt_stop_fare($from,$to);
}
}
function singapore_mrtlrt_stop_fare($from,$to) {
global $conn, $readOnlyReplicaConn;
echo "from=".$from["code1"]."_".$from["code2"].", to=".$to["code1"]."_".$to["code2"]."\n";
$q = "SELECT * FROM singapore_mrtlrt_stop_fares WHERE boarding_id='".$from["id"]."'";
$q.= " AND alight_id='".$to["id"]."'";
$r = pg_query($readOnlyReplicaConn,$q);
if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
// Update
if ($f["fare"]>0) {
$q = "";
} else {
$q = "UPDATE singapore_mrtlrt_stop_fares SET fare=%d, distance=%d WHERE id=".$f["id"]." RETURNING id";
}
} else {
// Insert
$q = "INSERT INTO singapore_mrtlrt_stop_fares (boarding_id,alight_id,fare,distance) VALUES(";
$q.= $from["id"].",".$to["id"].",%d,%d) RETURNING id";
}
if ($q!="") {
$entryId = $from["code1"] . "_" . $from["code2"];
$exitId = $to["code1"] . "_" . $to["code2"];
list($res, $err) = getMyTransportSGQuote($entryId,$exitId);
if (is_array($res) && array_key_exists('data',$res) && is_array($res['data']) && array_key_exists('fare_raw',$res['data']) && $res['data']['fare_raw']>0) {
$fare_raw = $res['data']['fare_raw'];
$distance = $res['data']['distance_raw'] * 10;
echo "--> ${fare_raw} (".$res['data']['distance']." => ${distance})\n";
$q = sprintf($q, $fare_raw, $distance);
//echo "$q\n";
$r = pg_query($conn, $q);
if ($r && pg_num_rows($r) && $f=pg_fetch_row($r)) {
echo "--> success: id=".$f[0]."\n";
} else {
echo "--> failure: ".pg_last_error($conn)."\n";
}
} else {
echo "ERROR: $err\n";
}
}
}
function getMyTransportSGQuote($entryId,$exitId) {
$url = 'https://www.mytransport.sg/content/mytransport/home/commuting/busservices/jcr:content/par/mtp_generic_tab/mtp_generic_tab6/distancefare.mrtFare?entryMarkerId='.$entryId.'&exitMarkerId='.$exitId.'&ticketType=30&tripContextParameter=&tripId=1';
$ch = curl_init($url);
//Create And Save Cookies
/*
$tmpfname = dirname(__FILE__).'/cookie.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname);
*/
$cookies = 'mtp_last_visit=Fare Calculator|/content/mytransport/home/commuting/busservices.html#Fare_Calculator,Bus Services|/content/mytransport/home/commuting/busservices.html';
$headers = [
'X-Requested-With: XMLHttpRequest',
'Sec-Fetch-Mode: cors',
'Sec-Fetch-Site: same-origin'
];
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.mytransport.sg/content/mytransport/home/commuting/busservices.html');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36');
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//https://stackoverflow.com/questions/310650/decode-gzipped-web-page-retrieved-via-curl-in-php
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
// EXECUTE
sleep(1);
$html = curl_exec($ch);
//error_log($url);
//error_log($html);
//echo trim($html);
curl_close($ch);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML(trim($html));
$finder = new DomXPath($dom);
// distance_fare_result_nm => Trip 1
// distance_fare_result_sv => Bus Svc 41
// distance_fare_result_bd => 28641 - Blk 284
// distance_fare_result_al => 42209 - Aft Toh Tuck Rise
// distance_fare_result_dt => 7.7 km
// distance_fare_result_fr => $1.29
// fare_raw => 129
// distance_raw => 770
$data = array(
'name' => getItem($finder,'distance_fare_result_nm'),
'service' => getItem($finder,'distance_fare_result_sv'),
'board' => getItem($finder,'distance_fare_result_bd'),
'alight' => getItem($finder,'distance_fare_result_al'),
'distance' => getItem($finder,'distance_fare_result_dt'),
'fare' => getItem($finder,'distance_fare_result_fr'),
'fare_raw' => getItem($finder,'fare_raw'),
'distance_raw' => getItem($finder,'distance_raw')
);
return [['data'=>$data],$html];
}
function getItem($finder,$class,$default='') {
$nlist = $finder->query("//*[@class='${class}']");
if ($nlist->length>0) {
$item = $nlist->item(0);
return $item->textContent;
}
return $default;
}