-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
45 lines (40 loc) · 1.19 KB
/
index.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
const axios = require('axios')
const createAlert = require('./src/alert')
const createJam = require('./src/jam')
const SERVER_LIST = ['row-rtserver']
function notificationUrl (server, left, right, top, bottom) {
return axios.get(`https://www.waze.com/${server}/web/TGeoRSS`, {
params: {
left,
right,
top,
bottom,
tk: 'community',
format: 'JSON'
}
})
}
function getTraffic (segment) {
if (!segment.top ||
!segment.right ||
!segment.bottom ||
!segment.left) {
return Promise.reject('Please provide top, right, bottom, left')
}
return Promise.all(SERVER_LIST.map(server => notificationUrl(server, segment.left, segment.right, segment.top, segment.bottom)))
.then(responses => responses
.reduce((prev, curr) => {
const data = curr.data
const newObj = {}
if (data.alerts) {
newObj.alerts = prev.alerts.concat(data.alerts.map(alert => createAlert(alert)))
}
if (data.jams) {
newObj.jams = prev.jams.concat(data.jams.map(jam => createJam(jam)))
}
return Object.assign({}, prev, newObj)
}, { alerts: [], jams: [] }))
}
module.exports = {
getTraffic
}