Mirror of Quill
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.0 KiB

  1. <?php
  2. chdir(dirname(__FILE__).'/..');
  3. require 'vendor/autoload.php';
  4. $flights = ORM::for_table('flights')
  5. ->where('active', 1)
  6. ->find_many();
  7. foreach($flights as $flight) {
  8. $user = ORM::for_table('users')
  9. ->where('id', $flight->user_id)
  10. ->where_not_equal('flightaware_apikey', '')
  11. ->find_one();
  12. if($user) {
  13. echo date('Y-m-d H:i:s')."\n";
  14. echo "Processing flight ".$flight->flight." for ".$user->url."\n";
  15. $ch = curl_init('http://flightxml.flightaware.com/json/FlightXML2/InFlightInfo?ident='.$flight->flight);
  16. curl_setopt($ch, CURLOPT_USERPWD, $user->flightaware_username.':'.$user->flightaware_apikey);
  17. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  18. $json = curl_exec($ch);
  19. $data = json_decode($json, true);
  20. #$data = json_decode($flight->lastposition, true);
  21. $flightData = $data['InFlightInfoResult'];
  22. $flight->lastposition = $json;
  23. $flight->save();
  24. if($flightData['departureTime']) {
  25. if($flightData['departureTime'] < strtotime($flight->date_created)) {
  26. echo "This flight departed before the checkin was made so this is probably the wrong flight\n";
  27. } else {
  28. $has_new_location = false;
  29. $flight_ended = false;
  30. // Add this point to the list
  31. if($flight->positions)
  32. $positions = json_decode($flight->positions, true);
  33. else
  34. $positions = [];
  35. if($flightData['latitude']) {
  36. $positions[] = [
  37. 'date' => date('Y-m-d H:i:s'),
  38. 'lat' => $flightData['latitude'],
  39. 'lng' => $flightData['longitude'],
  40. 'altitude' => $flightData['altitude'],
  41. 'heading' => $flightData['heading'],
  42. 'speed' => $flightData['groundspeed'],
  43. ];
  44. $flight->positions = json_encode($positions);
  45. $has_new_location = true;
  46. }
  47. if($has_new_location) {
  48. $latitude = $flightData['latitude'];
  49. $longitude = $flightData['longitude'];
  50. } else {
  51. $latitude = $positions[count($positions)-1]['lat'];
  52. $longitude = $positions[count($positions)-1]['lng'];
  53. }
  54. if($flightData['arrivalTime']) {
  55. $flight->arrival_time = date('Y-m-d H:i:s', $flightData['arrivalTime']);
  56. $flight->active = 0;
  57. $flight_ended = true;
  58. }
  59. if($flight_ended || $has_new_location) {
  60. $flight->departure_time = date('Y-m-d H:i:s', $flightData['departureTime']);
  61. $flight->save();
  62. $checkin = [
  63. 'type' => ['h-card'],
  64. 'properties' => [
  65. 'name' => [$flight->flight],
  66. 'url' => ['http://flightaware.com/live/flight/'.$flight->flight],
  67. 'latitude' => [$latitude],
  68. 'longitude' => [$longitude],
  69. ]
  70. ];
  71. // Geocode the location
  72. $geocode = json_decode(file_get_contents('https://atlas.p3k.io/api/geocode?latitude='.$latitude.'&longitude='.$longitude), true);
  73. if($geocode) {
  74. $checkin['properties']['locality'] = [$geocode['locality']];
  75. $checkin['properties']['region'] = [$geocode['region']];
  76. $checkin['properties']['country-name'] = [$geocode['country']];
  77. $tz = new DateTimeZone($geocode['timezone']);
  78. } else {
  79. $tz = new DateTimeZone('UTC');
  80. }
  81. $departure = new DateTime($flight->departure_time);
  82. $departure->setTimeZone($tz);
  83. $trip = [
  84. 'type' => ['h-trip'],
  85. 'properties' => [
  86. 'mode-of-transport' => ['plane'],
  87. 'start' => [$departure->format('c')],
  88. 'flight' => [$flightData['ident']],
  89. 'flight-id' => [$flightData['faFlightID']],
  90. 'aircraft' => [$flightData['type']],
  91. 'origin' => [$flightData['origin']],
  92. 'destination' => [$flightData['destination']],
  93. 'speed' => [
  94. [
  95. 'type' => ['h-measure'],
  96. 'properties' => [
  97. 'num' => [$flightData['groundspeed']],
  98. 'unit' => ['mph'],
  99. ]
  100. ]
  101. ],
  102. 'route' => [Config::$base_url.'flight/'.$flight->id.'/'.$flightData['faFlightID'].'/route.json']
  103. ]
  104. ];
  105. if($flight->arrival_time) {
  106. $arrival = new DateTime($flight->arrival_time);
  107. $arrival->setTimeZone($tz);
  108. $trip['properties']['end'] = [$arrival->format('c')];
  109. }
  110. // Convert this to a Micropub request
  111. $micropub = [
  112. 'action' => 'update',
  113. 'url' => $flight->url,
  114. 'replace' => [
  115. 'checkin' => $checkin,
  116. 'trip' => $trip,
  117. ]
  118. ];
  119. $r = micropub_post_for_user($user, $micropub, null, true);
  120. print_r($r['response']);
  121. }
  122. }
  123. } else {
  124. echo "It looks like the flight has not yet departed\n";
  125. }
  126. print_r($data);
  127. } else {
  128. echo "User ".$user->url." has no FlightAware credentials\n";
  129. }
  130. }