-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcabi.js
74 lines (69 loc) · 2.35 KB
/
cabi.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
const fs = require('fs')
const debug = require('debug')('cabi')
const fetch = require('node-fetch')
const geolib = require('geolib')
const userhome = require('userhome')
const cabirc = fs.readFileSync(userhome('.cabirc'), 'utf8').split('\n')
debug(`Read config ${cabirc}`)
const [locationString, stationInfoURL, stationStatusURL] = cabirc
const split = locationString.split(' ')
const loc = {
latitude: split[0],
longitude: split[1]
}
debug(loc)
debug('Getting station info...')
fetch(stationInfoURL)
.then(res => {
if (!res.ok) {
return new Error('fetch issue')
}
return res
})
.then(res => res.json())
.then(json => {
json.data.stations.forEach(e => {
e.away = geolib.getDistance(loc, {latitude: e.lat, longitude: e.lon})
})
const localStations = json.data.stations
.filter(e => e.away <= 2000)
debug('localStations', localStations.map(s => s.away))
// Second json fetch is simply nested
debug('Getting dock status...')
fetch(stationStatusURL)
.then(res => {
if (!res.ok) {
return new Error('fetch issue')
}
return res
})
.then(res => res.json())
.then(json => {
const localStationsInfo = json.data.stations
.filter(e => localStations
.map(s => s.station_id)
.includes(e.station_id))
debug('localStationsInfo', localStationsInfo)
localStationsInfo
.sort((a, b) => {
const stationAInfo = localStations[localStations.map(s => s.station_id).indexOf(a.station_id)]
const stationBInfo = localStations[localStations.map(s => s.station_id).indexOf(b.station_id)]
return stationAInfo.away - stationBInfo.away
})
.slice(0, 5)
.forEach(e => {
const stationInfo = localStations[localStations.map(s => s.station_id).indexOf(e.station_id)]
const bikes = parseInt(e.num_bikes_available)
const docks = parseInt(e.num_docks_available)
console.log(`${bikes} bikes ${docks} docks ${stationInfo.name} (${stationInfo.away}m)`)
const ebikes = parseInt(e.num_ebikes_available)
if (ebikes > 0) {
console.log(`⚡️ ebikes: ${stationInfo.name} has ${ebikes} ⚡️`)
}
})
})
})
.catch(err => {
console.error(err)
process.exit(1)
})