-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackflights.py
88 lines (65 loc) · 2.19 KB
/
trackflights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import requests
import json
import datetime
import time
# requires OAUTH setup
def getAirportStats(icao):
response = requests.get("https://api.ivao.aero/v2/airports/" + icao + "/stats")
data = response.json()
if response.status_code != 200:
print("error retrieving data")
print(data)
return;
print(data)
#getAirportStats('KJFK')
def getPilotsAtICAOs(dep = [], arr = []):
response = requests.get("https://api.ivao.aero/v2/tracker/whazzup")
data = response.json()
if response.status_code != 200:
print("error retrieving data")
print(response.status_code)
return;
matchingPilots = []
for elem in data['clients']['pilots']:
if elem['flightPlan'] == None:
continue
if elem['flightPlan']['departureId'] in dep:
matchingPilots.append(elem)
elif elem['flightPlan']['arrivalId'] in arr:
matchingPilots.append(elem)
return matchingPilots
def dumpFlights(flights, startTime):
print('\nWRITING FLIGHT LOG FILE...')
currTime = datetime.datetime.utcnow()
filePath = 'FLIGHTLOG' + currTime.strftime('%f') + '.txt'
f = open(filePath, 'at')
f.write('FLIGHT LOG RECORDED FROM ' + startTime.strftime('%c') + ' TO ' + currTime.strftime('%c'))
f.write('\nDEPARTURE ICAOs: ')
f.write('\nARRIVAL ICAOs: ')
f.write('\nNUM FLIGHTS: ' + str(len(flights)))
f.write('\n\n\n')
for flight in flights.values():
f.write(str(flight))
f.write('\n\n')
f.close()
print('DONE, CHECK ' + filePath)
def mainLoop():
flights = {}
startTime = datetime.datetime.utcnow()
try:
while True:
print('fetching new flights')
pilots = getPilotsAtICAOs(['KJFK'])
print('found ' + str(len(pilots)) + ' matching flights')
for pilot in pilots:
if pilot['id'] in flights:
continue # skip flights we already logged
else:
flights[pilot['id']] = pilot
print('sleeping...')
time.sleep(15)
except KeyboardInterrupt:
print('\n\nSHUTTING DOWN')
dumpFlights(flights, startTime);
print('DONE, CYA')
mainLoop()