-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBusBoard.js
133 lines (92 loc) · 4.31 KB
/
BusBoard.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
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const readline = require('readline-sync')
async function getNextBusArrivals(BusStopCode){
//Fetching Bus Arrivals using Bus StopCode
try {
const NextBusArrivalsResponse = await fetch('https://api.tfl.gov.uk/StopPoint/' + BusStopCode + '/Arrivals');
if (NextBusArrivalsResponse.ok) {
const NextBusArrivalsData = await NextBusArrivalsResponse.json();
// Prints only Key information about upcoming buses instead of whole json response
const UpcomingBuses = [];
for (let i = 0; i < 5; i++) {
let BusInfo = {
'Bus Number': NextBusArrivalsData[i].lineName,
'Destination Name': NextBusArrivalsData[i].destinationName,
'Going Towards': NextBusArrivalsData[i].towards,
'Expected Arrival Time': NextBusArrivalsData[i].expectedArrival
};
UpcomingBuses.push(BusInfo);
}
console.log(UpcomingBuses);
} else throw ('Error');
} catch (err) {
console.log(err);
}
}
async function getStopPointsWithinRadius(PostCode){
console.log('\nPlease enter your search radius:');
const radius = readline.prompt();
//Fetching the coordinates for the given postcode
const PostCodeResponse= await fetch(`http://api.postcodes.io/postcodes/${PostCode}`);
const PostCodeData = await PostCodeResponse.json();
const { longitude, latitude } = PostCodeData.result;
//Passing the coordinates to get Stops within the Radius
const StopRadiusResponse = await fetch(`https://api.tfl.gov.uk/StopPoint/?lat=${latitude}&lon=${longitude}&stopTypes=NaptanPublicBusCoachTram&radius=${radius}`)
const StopRadiusData = await StopRadiusResponse.json();
//console.log(StopRadiusData);
//Print Next Bus Arrival Info for Two Stop Points Within Radius
for(let i=0; i<2; i++){
const BusStopCode = StopRadiusData.stopPoints[i].naptanId; //Getting Bus Stop Code from Stops Within Radius Data
//Printing Bus Arrival Info by calling getNextBusArrivals Function
console.log('Next Bus Arrival Info for Bus Stop Code ' + StopRadiusData.stopPoints[i].naptanId + ' near ' + PostCode)
await getNextBusArrivals(BusStopCode);
}
}
// Display Direction to the Nearest stop using journey planner
async function JourneyPlannerDirections(StartPostCode,DestinationPostCode){
//Fetching the directions to the nearest BusStop
const DirectionResponse= await fetch(`https://api.tfl.gov.uk/Journey/JourneyResults/${StartPostCode}/to/${DestinationPostCode}`);
const DirectionData = await DirectionResponse.json();
const Detail = DirectionData.journeys[0].legs[0].instruction.detailed;
const Directions = DirectionData.journeys[0].legs[0].instruction;
//Prints where to go to reach nearest Bus Stop
console.log(`To go to the nearest Bus Stop : ${Detail}`);
//Prints the Direction to the nearest Bus Stop
console.log(`\nTo ${Detail} . Follow these directions: `)
for(let i=0; i<Directions.steps.length; i++){
console.log(Directions.steps[i].descriptionHeading + Directions.steps[i].description);
}
}
//Function to get valid postcodes as input
async function ValidatePostCodes(){
do{
try {
//console.log('Please enter a PostCode: ');
PostCode = readline.prompt();
Response= await fetch(`http://api.postcodes.io/postcodes/${PostCode}`);
Data = await Response.json();
//console.log(Data)
if (Data.status === 404)
throw "The PostCode you entered in Invalid !!!";
}
catch(error) {
console.log(error);
}
}while(Data.status === 404);
}
//Journey Planner
async function JourneyPlanner(){
console.log('Please enter the Starting PostCode: ');
const PostCode1 = await ValidatePostCodes();
console.log('Please enter the Destination PostCode: ');
const PostCode2 = await ValidatePostCodes();
console.log('Do You want the directions to the nearest Bus Stop (Y/N): ');
const Response = readline.prompt();
if(Response.toString().toUpperCase() === 'Y'){
await JourneyPlannerDirections(PostCode1,PostCode2);
await getStopPointsWithinRadius(PostCode1);
}else{
await getStopPointsWithinRadius(PostCode1);
}
}
JourneyPlanner()