Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
potch committed Oct 14, 2014
0 parents commit f32cb2c
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

```
% npm install -g flight-status
% flight UAL903
En Route / On Time
Departure: 02:51PM PDT
Arrival: 09:46AM CEST (+1)
[-------------✈ ]
2 hr 36 min elapsed, 7 hr 18 min remaining
```
94 changes: 94 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#! /usr/bin/env node

var request = require('request');
var cheerio = require('cheerio');
var opts = require("nomnom").parse();
var chalk = require('chalk');

var alias = {
'UA': 'UAL',
'United': 'UAL',
'UAL': 'UAL',
'Delta': 'DAL'
};

var airline = opts[0];
var number = opts[1];
if (!number) {
matches = airline.match(/([a-zA-Z]+)(\d+)/);
if (matches) {
airline = matches[1];
number = matches[2];
}
}
if (airline in alias) {
airline = alias[airline];
}

function print(s) {
s = s || '';
process.stdout.write(s);
}

function println(s) {
s = s || '';
print(s + '\n');
}

var code = airline + number;

request('http://flightaware.com/live/flight/' + code, function (error, response, body) {
if (!error && response.statusCode == 200) {
$ = cheerio.load(body);

println();

var data = {};

var table = $('.track-panel-data');
var trs = table.find('tr');
trs.each(function (i) {
var header = trs.eq(i).find('th').text().trim().toLowerCase();
var val = trs.eq(i).find('td').contents().first().text().trim();
data[header] = val;
});

println(data.status);

var times = $('.track-panel-actualtime');
var departure = times.eq(0);
if (departure.children().first().hasClass('flightStatusGood')) {
departure = chalk.green(departure.text().trim());
} else {
departure = chalk.red(departure.text().trim());
}
var arrival = times.eq(1);
if (arrival.children().first().hasClass('flightStatusGood')) {
arrival = chalk.green(arrival.text().trim());
} else {
arrival = chalk.red(arrival.text().trim());
}
println('Departure: ' + departure);
println('Arrival: ' + arrival);

var progress = $('.track-panel-progress');
if (progress.length) {
var elapsed = $('.track-panel-progress-fill').text().trim();
var remaining = $('.track-panel-progress-empty').text().trim();
var pct = $('.track-panel-progress-fill').css('width');
pct = parseInt(pct, 10) / 2 | 0;
var rem = 50 - pct;
print('\n' + chalk.bold('['));
print(chalk.cyan(Array(pct+1).join('-'))); // ✈
print(chalk.bold('✈'));
print(Array(rem+1).join(' '));
print(chalk.bold(']') + '\n');
println(elapsed + ' elapsed, ' + remaining + ' remaining');
}

println();

} else {
console.error(response.statusCode);
}
});
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "flight-status",
"version": "1.0.1",
"description": "grab flight status",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "potch",
"license": "ISC",
"bin": {
"flight": "index.js"
},
"dependencies": {
"chalk": "^0.5.1",
"cheerio": "^0.17.0",
"nomnom": "^1.8.0",
"request": "^2.45.0"
}
}

0 comments on commit f32cb2c

Please sign in to comment.