-
Notifications
You must be signed in to change notification settings - Fork 3
/
hslData.js
64 lines (59 loc) · 1.69 KB
/
hslData.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
const { request, gql } = require('graphql-request');
const moment = require('moment');
const locations = require('./friendLocations');
const url = 'https://api.digitransit.fi/routing/v1/routers/hsl/index/graphql';
const query = (lat, lon) => gql`
{
nearest(lat: ${lat}, lon: ${lon}, maxDistance: 500, filterByPlaceTypes: DEPARTURE_ROW) {
edges {
node {
place {
...on DepartureRow {
stop {
lat
lon
name
}
stoptimes {
scheduledDeparture
realtimeDeparture
serviceDay
trip {
route {
shortName
longName
}
}
headsign
}
}
}
distance
}
}
}
}
`;
const getTime = (serviceDay, scheduledDeparture) => {
const timeStamp = serviceDay + scheduledDeparture;
return moment.unix(timeStamp).format('HH:mm:ss');
};
const nextDepartures = (data) => `
Seuraavat lähdöt:
${data.map((departure) => `
${departure.node.place.stop.name}
${departure.node.place.stoptimes.map((stoptimes) => `
${stoptimes.trip.route.longName}
${stoptimes.trip.route.shortName}
${getTime(stoptimes.serviceDay, stoptimes.scheduledDeparture)}
`).join('')}
`).join('')}
`;
const getHslData = async (ctx, name) => {
const { lat, lon } = locations.find((location) => location.name === name);
const data = await request(url, query(lat, lon));
const filteredData = data.nearest.edges.filter((node) => node.node.place.stoptimes.length > 0);
const botText = nextDepartures(filteredData);
ctx.reply(botText);
};
module.exports = getHslData;