-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdater.py
182 lines (153 loc) · 4.78 KB
/
updater.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from urllib.request import urlopen
from pymongo import MongoClient
import sys, time
DATA_SOURCE = "http://data.vattastic.com/vatsim-data.txt"
METAR_SOURCE = "ftp://tgftp.nws.noaa.gov/data/observations/metar/cycles/"
AIRPORT_SOURCE = "http://openflights.svn.sourceforge.net/viewvc/openflights/openflights/data/airports.dat"
AIRLINE_SOURCE = "http://openflights.svn.sourceforge.net/viewvc/openflights/openflights/data/airlines.dat"
conn = MongoClient('localhost', 27017)
db = conn.vatradar
def to_int(s):
s = s.strip()
return int(s) if s else 0
def download(url):
print("downloading %s" % url)
req = urlopen(url)
return req.readlines()
def update_metar():
cycle = time.strftime("%HZ.TXT", time.gmtime())
data = download(METAR_SOURCE + cycle)
for line in data:
try:
line = line.decode("utf-8").rstrip()
except UnicodeDecodeError:
continue
if line[:1].isalpha():
icao = line[0:4],
data = {
"icao": icao,
"metar": line,
}
db.metar.update({"icao": icao}, {"$set": data}, True)
print(icao, end="\r")
def atc(data):
update = {
"callsign": data[0],
"cid": to_int(data[1]),
"realname": data[2],
"frequency": data[4],
"position": [float(data[5]), float(data[6])],
"server": data[14],
"rating": data[16],
"type": to_int(data[18]),
"atis": data[35],
}
callsign = update["callsign"]
db.atc.update({"callsign": callsign}, {"$set": update}, True)
print(callsign, end="\r")
def pilot(data):
pos = [float(data[5]), float(data[6])];
update = {
"callsign": data[0],
"cid": to_int(data[1]),
"realname": data[2],
"position": pos,
"altitude": to_int(data[7]),
"speed": to_int(data[8]),
"aircraft": data[9],
"tas": to_int(data[10]),
"origin": data[11],
"flightlevel": data[12],
"destination": data[13],
"server": data[14],
"squawk": to_int(data[17]),
"deptime": to_int(data[22]),
"remarks": data[29],
"route": data[30],
"heading": to_int(data[38]),
}
callsign = update["callsign"]
db.pilots.update({"callsign": callsign}, {
"$set": update,
"$addToSet": {"path": pos}
}, True)
print(callsign, end="\r")
def client(data):
data = data.split(":")
try:
if data[3] == "PILOT": pilot(data)
elif data[3] == "ATC": atc(data)
except:
print("something wrong with " + str(data))
def update_data():
data = download(DATA_SOURCE)
state = None
for line in data:
try:
line = line.decode("iso-8859-1").rstrip()
except UnicodeDecodeError:
continue
if line.startswith(";"):
state = None
continue
elif line.startswith("!CLIENTS"):
state = client
continue
if state:
state(line)
def update_airlines():
data = download(AIRLINE_SOURCE)
for line in data:
try:
line = line.decode("iso-8859-1")
except UnicodeDecodeError:
continue
data = line.split(",")
if len(data) < 7: continue
update = {
"name": data[1].strip('"'),
"icao": data[4].strip('"'),
"rtf": data[5].strip('"'),
"country": data[6].strip('"'),
}
icao = update["icao"]
if (len(icao) != 3): continue
db.airlines.update({"icao": icao}, {"$set": update}, True)
print(icao, end="\r")
def update_airports():
data = download(AIRPORT_SOURCE)
for line in data:
try:
line = line.decode("iso-8859-1")
except UnicodeDecodeError:
continue
data = line.split(",")
if len(data) < 9: continue
try:
update = {
"name": data[1].strip('"'),
"city": data[2].strip('"'),
"country": data[3].strip('"'),
"iata": data[4].strip('"'),
"icao": data[5].strip('"'),
"position": [float(data[6]), float(data[7])],
"altitude": to_int(data[8]),
}
except ValueError:
continue
icao = update["icao"]
if (len(icao) != 4): continue
db.airports.update({"icao": icao}, {"$set": update}, True)
print(icao, end="\r")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("no command specified")
elif sys.argv[1] == "airports":
update_airports()
elif sys.argv[1] == "airlines":
update_airlines()
elif sys.argv[1] == "loop":
while True:
update_data()
#update_metar()
time.sleep(30)