-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbusStops.js
83 lines (81 loc) · 4.12 KB
/
busStops.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
// Import the readline package from NPM
import readline from 'readline-sync';
// Import the winston logging package from NPM
// import winston from 'winston/lib/winston/config';
import winston from 'winston'
// Creating a logger
const logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'combined.log' })
]
});
let locationData = 1;
let busStops;
let busInfo;
let nearestStops = []
while (typeof (locationData) != "string") {
try {
// Get the postcode from the user via the terminal
const postCode = readline.prompt({ prompt: "Postcode: " });
const postresponse = await fetch(`https://api.postcodes.io/postcodes/${postCode}`);
if (postresponse.status !== 200) {
logger.error(`Postcode API error - status ${postresponse.status}, Postcode: ${postCode}`);
throw `Postcode API error - status ${postresponse.status}`
}
locationData = await postresponse.json();
let lat = locationData.result.latitude
let long = locationData.result.longitude
const tflresponse =
await fetch(`https://api.tfl.gov.uk/StopPoint/?lat=${lat}&lon=${long}&stopTypes=NaptanPublicBusCoachTram`)
if (tflresponse.status !== 200){
logger.error(`TfL API error - status ${tflresponse.status}`);
throw `TfL API error - status ${tflresponse.status}`
}
busStops = await tflresponse.json()
for (let i = 0; i < 2; i++) {
const busInfoResp =
await fetch(`https://api.tfl.gov.uk/StopPoint/${busStops.stopPoints[i].naptanId}/Arrivals`);
if (busInfoResp.status !== 200) {
logger.error(`Bus Info Response error: ${busInfoResp.status}`);
throw `Bus Info Response error: ${busInfoResp.status}`;
}
const busInfo = await busInfoResp.json();
if (busInfo.length == 0) {
logger.error(`No stop information for ${postCode}`);
throw `No stop information for ${postCode}`
}
const sortedBusInfo = busInfo.sort((bus1, bus2) => bus1.timeToStation - bus2.timeToStation);
// nearestStops.push(busStops.stopPoints[i].naptanId)
nearestStops.push({"naptanId": busStops.stopPoints[i].naptanId, "commonName":busStops.stopPoints[i].commonName})
console.log(`Next bus at stop ${busStops.stopPoints[i].commonName} is ${sortedBusInfo[0].lineId} arriving in ${Math.round(sortedBusInfo[0].timeToStation / 60)} mins\n`);
}
;
// Ask question...
const directions = (readline.prompt({prompt: "Do you want directions? Y/N "})).toLowerCase();
if (directions != "y" && directions != "n")
throw `Do not understand this response ${directions}. Enter Y or N`;
else if (directions == "y"){
for (let j = 0; j < nearestStops.length; j++) {
const directionsResp = await fetch(`https://api.tfl.gov.uk/Journey/JourneyResults/${postCode}/to/${nearestStops[j].naptanId}`)
if (directionsResp.status != 200) {
logger.error(`Unable to direct from ${postCode} to ${nearestStops[j].commonName}`)
throw `Unable to direct from ${postCode} to ${nearestStops[j].commonName}`
}
const route = await directionsResp.json();
// console.log(route.journeys[0].duration);
// console.log(route.journeys[0].legs[0].instruction.steps[0].description);
// loop through steps, display direction and summary to user
let instructions = ""
for (let k = 0; k < route.journeys[0].legs[0].instruction.steps.length; k++) {
instructions += `${route.journeys[0].legs[0].instruction.steps[k].descriptionHeading.trim()} `
instructions += `on ${route.journeys[0].legs[0].instruction.steps[k].description}. `
}
console.log(`Directions from ${postCode.toUpperCase()} to ${nearestStops[j].commonName} are:\n\t${instructions}\n`)
}
}
break
} catch (err) {
console.log(err)
}
}