Last active
June 23, 2017 22:40
-
-
Save rckclmbr/f56fe06dfd3de9a91f1319ecb010890e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import csv | |
import gzip | |
import os.path as path | |
from time import time, sleep | |
from urllib.request import urlretrieve | |
from geopy.distance import vincenty | |
import pyflightdata | |
import requests | |
SEC_IN_DAY = 60 * 60 * 24 | |
def get_sorted_data(relative_location): | |
resp = requests.get('https://opensky-network.org/api/states/all') | |
data = resp.json() | |
for d in data['states']: | |
plane_location = (d[6], d[5]) | |
distance = vincenty(relative_location, plane_location).miles | |
d.insert(0, distance) | |
d[2] = d[2].strip() | |
data['states'] = sorted(data['states'], key=lambda x: x[0]) | |
return data | |
def get_aircraft_data(): | |
gzip_filename = "aircraft_db.csv.gz" | |
if not path.exists(gzip_filename) or path.getmtime(gzip_filename) < time() - SEC_IN_DAY: | |
urlretrieve("https://junzisun.com/adb/download", gzip_filename) | |
ret = dict() | |
with gzip.open(gzip_filename, "rt") as f: | |
reader = csv.DictReader(f) | |
for row in reader: | |
ret[row['icao'].lower()] = row | |
return ret | |
def get_airports(regid): | |
regid = regid.lower() | |
flight = pyflightdata.get_history_by_tail_number(regid)[-1] | |
origin, dest = None, None | |
if flight['airport']['origin']: | |
origin = flight['airport']['origin']['name'] | |
if flight['airport']['destination']: | |
dest = flight['airport']['destination']['name'] | |
return origin, dest | |
if __name__ == "__main__": | |
all_aircraft_data = get_aircraft_data() | |
home = (37.4629937,-122.2099745) | |
for i in range(1000): | |
data = get_sorted_data(home) | |
closest_plane = data['states'][0] | |
( | |
distance, | |
icao24, | |
callsign, | |
origin_country, | |
time_position, | |
time_velocity, | |
longitude, | |
latitude, | |
altitude, | |
on_ground, | |
velocity, | |
heading, | |
vertical_rate, | |
sensors, | |
) = closest_plane[:14] | |
callsign = callsign.strip() | |
if icao24 not in all_aircraft_data: | |
print("Aircraft icao {} not found".format(icao24)) | |
sleep(10) | |
continue | |
aircraft_data = all_aircraft_data[icao24] | |
origin, dest = get_airports(aircraft_data['regid']) | |
print("That's a {} {} traveling from {} to {}".format( | |
aircraft_data['operator'], | |
aircraft_data['type'].replace('-', ' '), | |
origin, | |
dest, | |
)) | |
sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment