Skip to content

Commit

Permalink
Post live stream version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Prickett committed Feb 20, 2023
1 parent e8f6910 commit 905775b
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions enricher/enricher.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,74 @@
import * as dotenv from 'dotenv';
import { createClient } from 'redis';
import { createClient, commandOptions } from 'redis';
import fetch from 'node-fetch';

dotenv.config();

const REDIS_URL = process.env.REDIS_URL || 'redis:https://127.0.0.1:6379';
const FLIGHTAWARE_API_KEY = process.env.FLIGHTAWARE_API_KEY;
const FLIGHTAWARE_QUEUE = 'flightawarequeue';

const redisClient = createClient({
url: REDIS_URL
});

await redisClient.connect();

// TODO do something!!!!
// Loop over entries in the queue, and wait when there are none...
while (true) {
const response = await redisClient.brPop(
commandOptions({ isolated: true }),
FLIGHTAWARE_QUEUE,
5
);

if (response) {
// TODO decode callsign AND hex ident from the JSON in the list payload
const flightIdentifier = response.element;
console.log(response);
const flightAwareAPIURL = `https://aeroapi.flightaware.com/aeroapi/flights/${flightIdentifier}?max_pages=1`;

try {
const flightAwareResponse = await fetch(flightAwareAPIURL, {
headers: {
'x-apikey': FLIGHTAWARE_API_KEY,
'Accept': 'application/json'
}
});

if (flightAwareResponse.status === 200) {
const flightData = await flightAwareResponse.json();
for (const flight of flightData.flights) {
// The response contains an array of recent past, current and
// planned future flights with this ID. The one we want is
// currently in progress, so progress_percent between 1 and 99.
if (flight.progress_percent > 0 && flight.progress_percent < 100) {
// Grab the details we want and save them.
const flightDetails = {
registration: flight.registration,
origin_iata: flight.origin.code_iata,
origin_name: flight.origin.name,
destination_iata: flight.destination.code_iata,
destination_name: flight.destination.name,
aircraft_type: flight.aircraft_type,
operator_iata: flight.operator_iata,
flight_number: flight.flight_number
};

console.log('Save to flight:HEXIDENT');
console.log(flightDetails);
}
}

// TODO sleep for a couple of seconds...
} else {
console.log(`Error talking to FlightAware API returned ${flightAwareResponse.status} code.`);
}
} catch (e) {
console.log('Error talking to FlightAware API:');
console.log(e);
}
} else {
console.log('No new work to do.');
}
}

0 comments on commit 905775b

Please sign in to comment.