From f32cb2c2bf2e2e99cb9b33035e8c45e1e9f0e237 Mon Sep 17 00:00:00 2001 From: Matt Claypotch Date: Mon, 13 Oct 2014 17:31:54 -0700 Subject: [PATCH] first --- .gitignore | 1 + README.md | 13 ++++++++ index.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 20 +++++++++++ 4 files changed, 128 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..3264744 --- /dev/null +++ b/README.md @@ -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 + +``` diff --git a/index.js b/index.js new file mode 100755 index 0000000..2a5b0ac --- /dev/null +++ b/index.js @@ -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); + } +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..7d79e8b --- /dev/null +++ b/package.json @@ -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" + } +}