-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
95 lines (83 loc) · 2.19 KB
/
build.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
'use strict'
const {fetch} = require('fetch-ponyfill')({Promise: require('pinkie-promise')})
const csv = require('csv-parser')
const showError = (err) => {
console.error(err)
process.exit(1)
}
const fetchOperators = () => {
return fetch('https://vbb-gtfs.jannisr.de/latest/agency.csv', {
redirect: 'follow',
mode: 'cors',
headers: {
'user-agent': 'https://github.com/derhuerst/vbb-station-operators'
}
})
.then((res) => {
if (!res.ok) {
const err = new Error(res.statusText)
err.statusCode = res.status
throw err
}
return new Promise((yay, nay) => {
const parser = csv()
res.body.pipe(parser)
res.body.once('error', nay)
parser.once('error', nay)
const operators = {} // by id
parser.on('data', (agency) => {
const parsed = {
id: agency.agency_id,
name: agency.agency_name || null,
url: agency.agency_url || null,
timezone: agency.agency_timezone || null,
lang: agency.agency_lang || null,
phone: agency.agency_phone || null
}
operators[parsed.id] = parsed
})
parser.on('end', () => yay(operators))
})
})
}
const fetchStations = () => {
// todo: @poldixd will change this to a new format soon!
return fetch('https://raw.githubusercontent.com/poldixd/vbb-stations/master/all-stations-with-operators.json', {
redirect: 'follow',
mode: 'cors',
headers: {
'user-agent': 'https://github.com/derhuerst/vbb-station-operators'
}
})
.then((res) => {
if (!res.ok) {
const err = new Error(res.statusText)
err.statusCode = res.status
throw err
return
}
return res.json()
})
}
Promise.all([
fetchOperators(),
fetchStations()
])
.then(([allOperators, allStations]) => {
const operators = {} // by id
const stations = {} // by station id
for (let [id, _, operatorIds] of allStations) {
for (let operatorId of operatorIds) {
const operator = allOperators[operatorId]
if (!operator) {
console.error(id + ' has an invalid operator id ' + operatorId)
continue
}
if (!operators[operator.id]) operators[operator.id] = operator
if (!stations[id]) stations[id] = []
stations[id].push(operator.id)
}
}
process.stdout.write(JSON.stringify({operators, stations}))
})
.catch(showError)