Created
October 15, 2019 17:53
-
-
Save keksipurkki/8bc1c09293691c903e81de663e8c2424 to your computer and use it in GitHub Desktop.
Tram stops example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fetch = require("node-fetch"); | |
const query = | |
` | |
{ | |
stops(ids: ["HSL:1040406", "HSL:1050432"]) { | |
name | |
stoptimesForPatterns { | |
pattern { | |
id | |
route { | |
id | |
shortName | |
} | |
} | |
stoptimes { | |
realtimeDeparture | |
serviceDay | |
realtime | |
} | |
} | |
} | |
} | |
`; | |
async function main({ interval }) { | |
const response = await fetch("https://api.digitransit.fi/routing/v1/routers/hsl/index/graphql", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/graphql" | |
}, | |
body: query | |
}); | |
const body = await response.json(); | |
if (!body) { | |
throw new Error("No data to show"); | |
} | |
const { data } = body; | |
const formatted = data.stops.map( ({ name, stoptimesForPatterns }) => ({ | |
name, | |
trams: String(stoptimesForPatterns.map(s => s.pattern.route).flatMap(s => s.shortName)) | |
})); | |
console.table(formatted); | |
return setTimeout(main, interval, { interval }); | |
} | |
main({ interval: 5000 }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment