Skip to content

Instantly share code, notes, and snippets.

@savamarkovic
Created August 17, 2017 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savamarkovic/e927d6fa3e31b357a915b60b614e9caf to your computer and use it in GitHub Desktop.
Save savamarkovic/e927d6fa3e31b357a915b60b614e9caf to your computer and use it in GitHub Desktop.
flightaware script
<?php
ini_set('display_startup_errors', 1);
ini_set('memory_limit','512M');
error_reporting(E_ALL);
$airline = 'AAL'; //Three-Letter Code
$howMany = 35000;
$maxsize = 50000;
$ref = time();
if($airline == null){return;}
//GOLD CODE
$con=mysqli_connect("localhost","BLACKED_OUT","BLACKED_OUT","upbeatho_schedules");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$table_name = $airline."_schedules_".$ref; //A table name that goes along with the reference ID Always Unique.
$sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` char(3) NOT NULL DEFAULT '',
`flightnum` varchar(10) NOT NULL,
`depicao` varchar(4) NOT NULL DEFAULT '',
`arricao` varchar(4) NOT NULL DEFAULT '',
`route` text NOT NULL,
`aircraft` text NOT NULL,
`flightlevel` varchar(6) NOT NULL DEFAULT '32000',
`distance` float NOT NULL DEFAULT '0',
`deptime` varchar(15) NOT NULL DEFAULT '',
`arrtime` varchar(15) NOT NULL DEFAULT '',
`flighttime` float NOT NULL DEFAULT '0',
`notes` text NOT NULL,
`price` float NOT NULL,
`flighttype` varchar(1) NOT NULL DEFAULT 'P',
`daysofweek` varchar(7) NOT NULL DEFAULT '1234560',
`enabled` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `depicao` (`depicao`),
KEY `flightnum` (`flightnum`),
KEY `depicao_arricao` (`depicao`,`arricao`),
KEY `code` (`code`),
KEY `idx_code_flightnum` (`code`,`flightnum`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
if(mysqli_query($con, $sql)){
$options = array(
'trace' => true,
'exceptions' => 0,
'login' => 'smarko',
'password' => 'BLACKED_OUT',
);
$client = new SoapClient('http://flightxml.flightaware.com/soap/FlightXML2/wsdl', $options);
$date_end = time();//curent time in unix
$date_beg = time() - 60*60*24*7;//current time 7 days ago in unix
$sizeparams = array(
"max_size" => $maxsize
);
$client->SetMaximumResultSize($sizeparams);
$params = array(
"startDate" => time(),
"endDate" => time()+(24 * 60 * 60 *2 ),
"origin" => "",
"destination" => "",
"airline" => $airline,
"flightno"=> "",
"howMany" => $howMany,
"offset" => 0
);
$result = $client->AirlineFlightSchedules($params);
$flights = $result->AirlineFlightSchedulesResult->data; //retrieve flight results
$count = 0;
$implemented = 0;
foreach ($flights as $flight) {
$count++;
$ident_carrier = substr($flight->ident, 0, 3);
$actualident_carrier = substr($flight->actual_ident, 0, 3);
$notes = "";
if($ident_carrier == $airline && ( $actualident_carrier != $airline || $actualident_carrier != "")){
//Flight Flown booked by airline but operated by another carrier
if($actualident_carrier == ""){
//Do nothing
}else{
$notes .= "Flight Operated by ". substr($flight->actual_ident, 0, 3).". "; //Add that to the notes
}
$implemented++;
}else if($ident_carrier != $airline){
//Probably a duplicate, just check the next flight
//continue;
}
$identity = $flight->ident;
$flightid = $flight->ident; //Could be mixed with ident
$orig = $flight->origin;
$dest = $flight->destination;
$deptim2 = $flight->departuretime;
$deptim = gmdate("H:i", $deptim2);
$arrtim2 = $flight->arrivaltime;
$arrtim = gmdate("H:i", $arrtim2);
$atype = $flight->aircrafttype;
$notes .= $flight->meal_service;
$code = substr($flight->ident, 0, 3);
$flightnum = substr( $flightid, 3);
//$flightnum = str_replace($airline, "", $identity);
$result3 = mysqli_query($con,"SELECT * FROM airports WHERE icao='$orig'");
$result3 = mysqli_fetch_assoc($result3);
$result4 = mysqli_query($con,"SELECT * FROM airports WHERE icao='$dest'");
$result4 = mysqli_fetch_assoc($result4);
$lat1 = $result3['lat'];
$lat2 = $result4['lat'];
$lon1 = $result3['lon'];
$lon2 = $result4['lon'];
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$dist = $dist * 69.11;
$routing = "See flightaware for routing options";
$cruise = 32000;
$utime = ($arrtim2 - $deptim2) / 60 / 60;
$utime = number_format((float)$utime, 2, '.', '');
$price = 500;
mysqli_query($con,"INSERT INTO `$table_name`(code, flightnum, depicao, arricao, aircraft, deptime, arrtime, flighttime, distance, notes, price) VALUES('$code', '$flightnum', '$orig', '$dest', '$atype', '$deptim', '$arrtim', '$utime', '$dist', '$notes', '$price')") or die(mysqli_error($con));
}
echo ' Finished. Retrieved ' . $count . ' Raw Schedules. ' . $implemented . ' Were implemented';
mysqli_close($con);
}
?>
<pre>
<?php //print_r($flights); ?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment