-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathspider.py
More file actions
201 lines (160 loc) · 4.91 KB
/
Copy pathspider.py
File metadata and controls
201 lines (160 loc) · 4.91 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from __future__ import print_function
import requests
import re
import time
import pymongo
from bs4 import BeautifulSoup
base_url = "http://lhr.data.fr24.com/zones/fcgi/feed.js?faa=1&mlat=1&flarm=0" \
"&adsb=1&gnd=1&air=1&vehicles=0&estimated=0&maxage=0&gliders=0&stats=1"
flight_url = "https://api.flightradar24.com/common/v1/flight-playback.json?flightId=%s"
# """ Divid the earth surface into 252 zones for querying aircraft data """
# zones = []
# for i in xrange(7):
# for j in xrange(18):
# zones.append([70-20*i, 70-20*(i+1), -180+20*j, -180+20*(j+1)])
# Manuelly divid the earth surface into zones optimized for flights density
world_zones = [
[90, 70, -180, 180],
[70, 50, -180, -20],
[70, 50, -20, 0],
[70, 50, 0, 20],
[70, 50, 20, 40],
[70, 50, 40, 180],
[50, 30, -180, -120],
[50, 40, -120, -110],
[50, 40, -110, -100],
[40, 30, -120, -110],
[40, 30, -110, -100],
[50, 40, -100, -90],
[50, 40, -90, -80],
[40, 30, -100, -90],
[40, 30, -90, -80],
[50, 30, -80, -60],
[50, 30, -60, -40],
[50, 30, -40, -20],
[50, 30, -20, 0],
[50, 40, 0, 10],
[50, 40, 10, 20],
[40, 30, 0, 10],
[40, 30, 10, 20],
[50, 30, 20, 40],
[50, 30, 40, 60],
[50, 30, 60, 180],
[30, 10, -180, -100],
[30, 10, -100, -80],
[30, 10, -80, 100],
[30, 10, 100, 180],
[10, -10, -180, 180],
[-10, -30, -180, 180],
[-30, -90, -180, 180]
]
# using requests session to increace http performance
s = requests.Session()
s.headers.update({'user-agent': 'Mozilla/5.0'})
mclient = pymongo.MongoClient()
mCollAC = mclient.adb.aircraft
def trim_label(label):
"""
Trim label, for example:
Lufthansa (Star Alliance Livery) to Lufthansa
"""
regex = re.compile('\(.+')
trimed = regex.sub('', label).strip()
return trimed
def get_ac(key, data):
if len(data) != 18:
raise RuntimeError('wrong data length')
# get aircraft ids
icao = data[0]
regid = data[9]
mdl = data[8]
fr24id = key
if not (icao and mdl and regid):
raise RuntimeError('some field missing in data')
ac = {
'icao': icao.lower(),
'regid': regid.lower(),
'mdl': mdl.lower(),
'fr24id': fr24id.lower(),
'ts': int(time.time()),
}
return ac
def fetch_online_aircraft():
urls = []
for zone in world_zones:
bounds = ','.join(str(d) for d in zone)
url = base_url + "&bounds=" + bounds
urls.append(url)
acs = []
# ---- Get all the online aircraft from FR24 and update DB ----
for url in urls:
try:
response = s.get(url)
data = response.json()
except Exception, e:
print(e)
continue
for key, val in data.iteritems():
try:
ac = get_ac(key, val)
acs.append(ac)
except Exception, e:
# print(e)
continue
# Update or insert record
for ac in acs:
ac_old = mCollAC.find_one({'icao': ac['icao']})
if ac_old:
if 'type' in ac_old:
ac['type'] = ac_old['type']
if 'operator' in ac_old:
ac['operator'] = ac_old['operator']
mCollAC.update({'icao': ac['icao']}, ac, upsert=True)
return acs
def update_info(ac):
url = flight_url % ac['fr24id'].lower()
try:
response = s.get(url)
data = response.json()
d = data['result']['response']['data']['flight']['aircraft']
ac['operator'] = trim_label(d['owner'])
ac['type'] = d['model']['text'].strip()
mCollAC.update({'icao': ac['icao']}, ac)
return True
except:
return False
def update_new_acs_info():
acs = mCollAC.find({
'$or': [
{'type': {'$exists': False}},
{'operator': {'$exists': False}}
]
})
total_count = acs.count()
count = 0
for ac in acs:
count += 1
print("%d of %d updated" % (count, total_count))
update_info(ac)
time.sleep(0.5)
# ---------------------------------------------------
# functions to fix dirty strings in existing data
# ---------------------------------------------------
def trim_all_oeprator_labels():
acs = mCollAC.find()
total = acs.count()
for i, ac in enumerate(acs):
if i % 1000 == 0:
print("%d of %d completed." % (i, total))
if 'operator' in ac:
ac['operator'] = trim_label(ac['operator'])
mCollAC.replace_one({'_id': ac['_id']}, ac, upsert=True)
def trim_all_type_labels():
acs = mCollAC.find()
total = acs.count()
for i, ac in enumerate(acs):
if i % 1000 == 0:
print("%d of %d completed." % (i, total))
if ('type' in ac) and (ac['type'] is not None):
ac['type'] = trim_label(ac['type'])
mCollAC.replace_one({'_id': ac['_id']}, ac, upsert=True)