-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
190 lines (163 loc) · 5.52 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const express = require("express");
require("dotenv").config();
const { createServer } = require("http");
const axios = require("axios");
const path = require("path");
const { Server } = require("socket.io");
const EventSource = require("eventsource");
const app = express();
const httpServer = createServer(app);
app.use(express.static(__dirname + "/build"));
app.use(express.json());
const io = new Server(httpServer, {
cors: {
origin: ["http://localhost:3000", "http://localhost:5500"],
methods: ["GET", "POST"],
},
});
app.get("/api/status", function (req, res) {
return res.send({ running: true });
});
app.get("/api", (req, res) => {
return res.send({ ready: true });
});
app.get("/api/vehicles", async (req, res) => {
const response = await axios.get(
`https://api-v3.mbta.com/vehicles?api_key=${process.env.MBTA_TOKEN}`
);
const vehicles = response.data;
return res.send({ vehicles: vehicles.data });
});
app.get("/api/routes", async (req, res) => {
const response = await axios.get(
`https://api-v3.mbta.com/routes?api_key=${process.env.MBTA_TOKEN}`
);
const routes = response.data;
return res.send({ routes: routes.data });
});
app.get("/api/routes/:route_id", async (req, res) => {
const { route_id } = req.params;
const response = await axios.get(
`https://api-v3.mbta.com/routes/${route_id}?api_key=${process.env.MBTA_TOKEN}`
);
const route = response.data;
return res.send({ route: route.data });
});
app.get("/api/shapes/:shapeId", async (req, res) => {
const response = await axios.get(
`https://api-v3.mbta.com/shapes?include=route&filter%5Broute%5D=${req.params.shapeId}`
);
const shapes = response.data;
return res.send({ shapes: shapes.data });
});
app.get("/api/vehicles/:vehicle_id", async (req, res) => {
const { vehicle_id } = req.params;
const response = await axios.get(
`https://api-v3.mbta.com/vehicles?filter%5Broute_type%5D=${vehicle_id}&api_key=${process.env.MBTA_TOKEN}`
);
const vehicles = response.data;
return res.send({ vehicles: vehicles.data });
});
app.get("/api/stops/:stop_id", async (req, res) => {
const { stop_id } = req.params;
const response = await axios.get(
`https://api-v3.mbta.com/stops?include=child_stops&filter%5Broute%5D=${stop_id}&api_key=${process.env.MBTA_TOKEN}`
);
const stops = response.data;
return res.send({ stops: stops.data });
});
app.get("/api/predictions/:route_id/:trip_id", async (req, res) => {
const { route_id, trip_id } = req.params;
const response = await axios.get(
`https://api-v3.mbta.com/predictions?include=stop&filter%5Broute%5D=${route_id}&filter%5Btrip%5D=${trip_id}&api_key=${process.env.MBTA_TOKEN}`
);
const predictions = response.data;
const included = response?.data?.included?.reverse();
const newPredictions = predictions.data.reduce(
(collector, prediction, index) => {
const [stop] = included.filter(
(include) => include.id === prediction?.relationships?.stop?.data?.id
);
const combined = {
attributes: {
departure_time: prediction?.attributes?.departure_time,
arrival_time: prediction?.attributes?.arrival_time,
platform_name: stop?.attributes?.name,
description: stop?.attributes?.description,
latitude: stop?.attributes?.latitude,
longitude: stop?.attributes?.longitude,
...stop,
},
id: prediction?.id,
relationships: {
...prediction.relationships,
},
};
collector.push(combined);
return collector;
},
[]
);
return res.send({
predictions: predictions.data,
combined: newPredictions,
});
});
app.get("/api/schedules/:trip_id", async (req, res) => {
const { trip_id } = req.params;
try {
const response = await axios.get(
`https://api-v3.mbta.com/schedules?include=prediction,stop&filter%5Btrip%5D=${trip_id}&api_key=${process.env.MBTA_TOKEN}`
);
const schedule = response.data;
const included = response.data.included;
const newSchedule = schedule.data.reduce((collector, schedule, index) => {
const [stop] = included.filter(
(include) => include.id === schedule?.relationships?.stop?.data?.id
);
const [prediction] = included.filter(
(include) =>
include.id === schedule?.relationships?.prediction?.data?.id
);
const combined = {
attributes: {
departure_time: schedule?.attributes?.departure_time,
arrival_time: schedule?.attributes?.arrival_time,
platform_name: stop?.attributes?.name,
description: stop?.attributes?.description,
latitude: stop?.attributes?.latitude,
longitude: stop?.attributes?.longitude,
prediction: { ...prediction },
...stop,
},
id: schedule?.id,
relationships: {
...schedule.relationships,
},
};
collector.push(combined);
return collector;
}, []);
return res.send({
schedule: schedule.data,
combined: newSchedule,
});
} catch (error) {
debugger;
}
});
app.get("/*", function (request, response) {
response.sendFile(path.resolve(__dirname, "build/index.html"));
});
const stream = new EventSource(
`https://api-v3.mbta.com/vehicles?api_key=${process.env.MBTA_TOKEN}`,
{ withCredentials: true }
);
stream.addEventListener("update", (event) => {
const parsed = JSON.parse(event.data);
io.emit(parsed.id, event);
});
const port = 5500;
httpServer.listen(process.env.PORT || port, () => {
console.log("MBTA Spy running");
});