-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_train_live_status.js
72 lines (50 loc) · 2.85 KB
/
get_train_live_status.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
let trainNo5 = document.querySelector("#trainNumber5");
let tbody5 = document.querySelector('#tableAppend5');
let button5 = document.querySelector('.submit-btn5');
let flag5 = true;
let stMap = new Map([
["BJU", "Barauni Junction"],
["BDTS", "Bandra Terminus"]
]);
let resultBox5 = document.querySelector('.resultBox5');
button5.addEventListener('click',()=>{
event.preventDefault(); // Stops webpage from reloading automatically after form submission
console.log(trainNo5.value);
if(trainNo5.value=="number"){
alert("Please select train number");
return;
}
// API Call
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': 'a1e0032c9amsh0c59e81fe5967c8p1e443bjsn602da0c29fd4', // Change rapidapi key here
'X-RapidAPI-Host': 'irctc1.p.rapidapi.com'
}
};
fetch(`https://irctc1.p.rapidapi.com/api/v1/liveTrainStatus?trainNo=${trainNo5.value}&startDay=1`, options)
.then(response => response.json())
.then(response => {
console.log(response);
let CurrStName = response.data.current_station_name;
let CName = CurrStName.slice(0,CurrStName.length-1);
tbody5.getElementsByTagName('tr')[0].getElementsByTagName('td')[1].innerText = `${response.data.update_time}`;
tbody5.getElementsByTagName('tr')[1].getElementsByTagName('td')[1].innerText = `${stMap.get(response.data.source)}`;
tbody5.getElementsByTagName('tr')[2].getElementsByTagName('td')[1].innerText = `${stMap.get(response.data.destination)}`;
tbody5.getElementsByTagName('tr')[3].getElementsByTagName('td')[1].innerText = `${response.data.total_distance}km`;
tbody5.getElementsByTagName('tr')[4].getElementsByTagName('td')[1].innerText = `${response.data.distance_from_source}km`;
tbody5.getElementsByTagName('tr')[5].getElementsByTagName('td')[1].innerText = `${CName}`;
tbody5.getElementsByTagName('tr')[6].getElementsByTagName('td')[1].innerText = `${response.data.current_state_code}`;
for(let i=0; i<response.data.current_location_info.length; i++){
let tr5 = document.createElement('tr');
tr5.innerHTML = `<td><a href="#">Message${i+1}</a></td><td class="amount">${response.data.current_location_info[i].readable_message}</td>`;
tbody5.appendChild(tr5);
}
})
.catch(err => console.error(err));
// Show resultBox
if(flag5){
resultBox5.style.display = 'grid';
flag5 = false;
}
});