-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbusboard.js
More file actions
131 lines (112 loc) · 4.1 KB
/
busboard.js
File metadata and controls
131 lines (112 loc) · 4.1 KB
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
import fetch from 'node-fetch'
import { config } from 'dotenv';
import { trace } from 'console';
import promptSync from 'prompt-sync';
const prompt = promptSync();
const api_key = process.env.API_KEY;
config()
// const stopPointId = '490008660N567';
let stopType = 'NaptanPublicBusCoachTram';
// let postcode = '';
async function getArrivalsFromTfl(stopPointId) {
try {
const busStopResponse = await fetch(`https://api.tfl.gov.uk/StopPoint/${stopPointId}/Arrivals?api_key=${api_key}`);
const busStopResponseBody = await busStopResponse.json();
if (busStopResponse.status === 200){
return busStopResponseBody;
}
else {
throw new Error(busStopResponseBody.httpStatusCode + ' ' + busStopResponseBody.message);
}
}
catch (err) {
return err
}
}
async function getBusStopArrivals(busStopCode) {
try {
let busData = await getArrivalsFromTfl(busStopCode);
if(busData.length === 0) {
return 'no buses coming'
}
else {
busData = busData.slice(0, 5).map(element => {
let timeToWait = Math.floor(element.timeToStation / 60);
return element.lineName + ', ' + element.towards + ', ' + `${timeToWait} minutes`;
});
return busData;
}
}
catch (err) {
console.log(err);
}
}
async function getPostCodeData() {
let postcode = prompt("Please enter a postcode ");
postcode = postcode.replaceAll(' ','');
try {
let enteredPC = false;
let postCodeRegex = '^\w{5,7}$^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))[0-9][A-Za-z]{2})$';
while (!(enteredPC)) {
if (postcode === 'null' || (!postcode.match(postCodeRegex))) {
postcode = prompt("Please enter a valid postcode ");
postcode = postcode.replaceAll(' ','');
}
else if (postcode.match(postCodeRegex)) {
enteredPC = true;
}
}
const postCodeResponse = await fetch(`https://api.postcodes.io/postcodes/${postcode}/`);
const postCodeResponseBody = await postCodeResponse.json();
if(postCodeResponseBody.status === 200) {
return [postCodeResponseBody.result.latitude, postCodeResponseBody.result.longitude];
}
else {
throw new Error(postCodeResponseBody.status + ' ' + postCodeResponseBody.error);
}
} catch (err) {
console.log(err);
}
}
// let coordinates = [5373825, 637383]
async function getNearestStopPoints(coordinates) {
try {
const nearByBusStopresponse = await fetch(`https://api.tfl.gov.uk/StopPoint/?lat=${coordinates[0]}&lon=${coordinates[1]}&stopTypes=${stopType}`);
const nearByBusStopresponseBody = await nearByBusStopresponse.json();
if (nearByBusStopresponse.status === 200) {
if (nearByBusStopresponseBody.stopPoints.length === 0) {
return 'no bus stops nearby'
} else {
return nearByBusStopresponseBody.stopPoints;
}
} else {
throw new Error(nearByBusStopresponseBody.httpStatusCode + ' ' + nearByBusStopresponseBody.message);
}
}
catch (err) {
return err
}
}
async function transportSpots() {
try {
let coordinates = await getPostCodeData();
let busStops = await getNearestStopPoints(coordinates);
let busStopInfo = {};
if (busStops == 'no bus stops nearby') {
return 'sorry, no tfl bus stops near you!'
}
else {
for (let i in busStops) {
busStopInfo[busStops[i].indicator] = {};
busStopInfo[busStops[i].indicator]['Arrivals'] = await getBusStopArrivals(busStops[i].naptanId);
}
return busStopInfo;
}
}
catch (err) {
console.log(err);
}
}
console.log(await transportSpots());
// console.log(await getArrivalsFromTfl(stopPointId))
// console.log(await getNearestStopPoints(coordinates))