This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
api.js
223 lines (183 loc) · 7.4 KB
/
api.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const cheerio = require('cheerio');
const moment = require('moment');
const request = require('request');
var brotli = require('brotli');
const debug = (...args) => {
if (true) {
console.log.apply(console, args);
}
}
function parsePosition(position) {
debug('Position: ', position);
return {
"error": position.error,
"data":
{
timestamp: position.data.timestamp,
unixtime: position.data.unixtime,
latitude: parseFloat(position.data.latitude),
longitude: parseFloat(position.data.longitude),
course: parseFloat(position.data.course),
speed: parseFloat(position.data.speed)
}
}
}
const headersVF = {
'User-Agent': 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3703.0 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded',
'cache-control': 'max-age=0',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'upgrade-insecure-requests':1,
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8'
};
const headersMT = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3703.0 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded',
'cache-control': 'max-age=0',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'upgrade-insecure-requests':1,
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8'
};
function getLocationFromVF(mmsi, cb) {
const url = `https://www.vesselfinder.com/vessels/somestring-MMSI-${mmsi}`;
debug('getLocationFromVF', url);
debug('error VF');
cb({ error: 'an unknown error occured' });
}
function getLocationFromMT(mmsi, cb) {
const url = `https://www.marinetraffic.com/en/data/?asset_type=vessels&columns=flag,shipname,photo,recognized_next_port,reported_eta,reported_destination,current_port,imo,mmsi,ship_type,show_on_live_map,time_of_latest_position,lat_of_latest_position,lon_of_latest_position&mmsi|eq|mmsi=${mmsi}`;
debug('getLocationFromMT', url);
headers = headersMT;
const options = {
url,
headers,
};
request(options, function (error, response, html) {
if (!error && response.statusCode == 200 || response.statusCode == 403) {
console.log('first request successsfull, set cookie');
let secondRequestHeaders = headers;
secondRequestHeaders.cookie = response.headers['set-cookie'];
secondRequestHeaders.referer = `https://www.marinetraffic.com/en/data/?asset_type=vessels&columns=flag,shipname,photo,recognized_next_port,reported_eta,reported_destination,current_port,imo,mmsi,ship_type,show_on_live_map,time_of_latest_position,lat_of_latest_position,lon_of_latest_position&mmsi|eq|mmsi=${mmsi}`;
secondRequestHeaders['Vessel-Image'] = '007fb60815c6558c472a846479502b668e08';
request({
url: `https://www.marinetraffic.com/en/reports?asset_type=vessels&columns=flag,shipname,photo,recognized_next_port,reported_eta,reported_destination,current_port,imo,mmsi,ship_type,show_on_live_map,time_of_latest_position,lat_of_latest_position,lon_of_latest_position&mmsi=${mmsi}`,
headers: secondRequestHeaders
}, function (error, response, html) {
if (!error && response.statusCode == 200 || response.statusCode == 403) {
console.log('second request worked');
console.log(html);
let parsed = JSON.parse(html);
console.log(parsed);
if (parsed.totalCount > 0)
{
const latitude = parseFloat(parsed.data[0].LAT);
const longitude = parseFloat(parsed.data[0].LON);
const speed = parseFloat(parsed.data[0].SPEED);
const course = parseFloat(parsed.data[0].COURSE);
const timestamp = new Date(parsed.data[0].LAST_POS*1000).toISOString();
const unixtime = new Date(parsed.data[0].LAST_POS*1000).getTime()/1000;
console.log(123);
//const $ = cheerio.load(html);
console.log(timestamp, speed ,course ,latitude ,longitude)
if (timestamp && latitude && longitude) {
cb(
parsePosition({
error: null,
data: {
timestamp: timestamp,
unixtime,
course: course,
speed,
latitude,
longitude,
}
})
);
} else {
cb({ error: 'missing needed position data' });
}
} else {
cb({ error: 'no records were found' });
}
} else {
cb({ error });
}
});
} else {
cb({ error });
}
});
}
function getLocation(mmsi, cb) {
debug('getting location for vessel: ', mmsi);
getLocationFromVF(mmsi, function(VFResult) {
debug('got location from vf', VFResult);
getLocationFromMT(mmsi, function (MTResult) {
if (MTResult.error) {
cb(VFResult);
} else {
debug('got location from mt', MTResult);
if (!VFResult.data) {
return cb(MTResult);
}
const vfDate = moment( VFResult.data.timestamp);
const mtDate = moment(MTResult.data.timestamp);
const secondsDiff = mtDate.diff(vfDate, 'seconds')
debug('time diff in seconds: ', secondsDiff);
cb(secondsDiff > 0 ? MTResult : VFResult);
}
});
});
}
function getVesselsInPort(shipPort, cb) {
const url = `https://www.marinetraffic.com/en/reports?asset_type=vessels&columns=flag,shipname,photo,recognized_next_port,reported_eta,reported_destination,current_port,imo,ship_type,show_on_live_map,time_of_latest_position,lat_of_latest_position,lon_of_latest_position,current_port_country,notes¤t_port_in_name=${shipPort}`;
debug('getVesselsInPort', url);
const headers={
'accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, brotli',
'Vessel-Image': '0053e92efe9e7772299d24de2d0985adea14',
'X-Requested-With': 'XMLHttpRequest'
}
const options = {
url,
headers,
json: true,
gzip: true,
deflate: true,
brotli:true
};
request(options, function (error, response, html) {
if (!error && response.statusCode == 200 || typeof response != 'undefined' && response.statusCode == 403) {
return cb(response.body.data.map((vessel) => ({
name: vessel.SHIPNAME,
id: vessel.SHIP_ID,
lat: Number(vessel.LAT),
lon: Number(vessel.LON),
timestamp: vessel.LAST_POS,
mmsi: vessel.MMSI,
imo: vessel.IMO,
callsign: vessel.CALLSIGN,
speed: Number(vessel.SPEED),
area: vessel.AREA_CODE,
type: vessel.TYPE_SUMMARY,
country: vessel.COUNTRY,
destination: vessel.DESTINATION,
port_current_id: vessel.PORT_ID,
port_current: vessel.CURRENT_PORT,
port_next_id: vessel.NEXT_PORT_ID,
port_next: vessel.NEXT_PORT_NAME,
})));
} else {
debug('error in getVesselsInPort');
cb({ error: 'an unknown error occured' });
return false;
}
});
}
module.exports = {
getLocationFromVF: getLocationFromVF,
getLocationFromMT: getLocationFromMT,
getLocation: getLocation,
getVesselsInPort:getVesselsInPort,
};