-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight-tracker.js
executable file
·104 lines (91 loc) · 2.88 KB
/
flight-tracker.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
#!/usr/bin/env node
const axios = require('axios');
const { printTable } = require('console-table-printer');
const readline = require('readline');
require('dotenv').config();
const apiKey = process.env.API_KEY;
// Create interface to read user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Prompt user for flight number
function promptFlightNumber() {
rl.question('Please enter a flight number: ', async (flightNumber) => {
if (!flightNumber.trim()) {
console.log('Flight number cannot be empty. Please try again.');
promptFlightNumber(); // Prompt again
return;
}
if (!/^[A-Z]{2}\d+$/i.test(flightNumber.trim())) {
console.log('Invalid flight number format. Please enter a valid flight number.');
promptFlightNumber(); // Prompt again
return;
}
console.log('Fetching flight information...');
try {
const flightData = await fetchFlightInformation(flightNumber.toUpperCase());
printFlightDetails(flightData);
} catch (error) {
console.error('Error fetching flight data:', error);
} finally {
rl.close();
}
});
}
promptFlightNumber();
async function fetchFlightInformation(flightNumber) {
const apiUrl = `http://api.aviationstack.com/v1/flights?access_key=${apiKey}&flight_iata=${flightNumber.toUpperCase()}`;
const response = await axios.get(apiUrl);
if (response.data && response.data.data.length > 0) {
return response.data.data[0];
} else {
throw new Error("Flight information currently unavailable.");
}
}
function printFlightDetails(flightData) {
const formattedData1 = [
{
'Flight Number': flightData.flight.iata || 'Unavailable',
'Date': flightData.flight_date || 'Unavailable',
'Status': flightData.flight_status || 'Unavailable',
}
];
const formattedData2 = [
{
'Departure Airport': flightData.departure.airport || 'Unavailable',
'Arrival Airport': flightData.arrival.airport || 'Unavailable',
'Airline': flightData.airline.name || 'Unavailable',
}
];
const formattedData3 = [
{
'Scheduled Departure Time': formatDate(flightData.departure.scheduled),
'Actual Departure Time': formatDate(flightData.departure.actual),
}
];
const formattedData4 = [
{
'Scheduled Arrival Time': formatDate(flightData.arrival.scheduled),
'Actual Arrival Time': formatDate(flightData.arrival.actual),
}
];
printTable(formattedData1);
printTable(formattedData2);
printTable(formattedData3);
printTable(formattedData4);
}
function formatDate(dateString) {
if (!dateString) return 'Unavailable';
const date = new Date(dateString);
const options = {
timeZone: 'UTC',
timeZoneName: 'short',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
};
return date.toLocaleString('en-US', options);
}