Created
June 4, 2013 02:51
-
-
Save mnot/5703249 to your computer and use it in GitHub Desktop.
Figure out how long it takes to get to a set of airports from a "home" location.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var argv = require('optimist').argv | |
var http = require('http'); | |
var $ = require('jquery'); | |
var ietf_airports = { | |
IETF_airports: [ | |
"TXL", | |
"YVR", | |
"LHR", | |
"YYZ", | |
"HNL", | |
"DFW", | |
"PRG", | |
"NRT" | |
], | |
run: function (home_airport) { | |
for (airport_num in this.IETF_airports) { | |
var destination_airport = this.IETF_airports[airport_num]; | |
if (home_airport != destination_airport) { | |
this.flying_time(home_airport, destination_airport, this.add_up) | |
} else { | |
this.add_up(0); | |
} | |
} | |
}, | |
flying_time: function (from, to, callback) { | |
var html = ''; | |
var options = { | |
hostname: 'www.rome2rio.com', | |
port: 80, | |
path: '/s/' + from + '/' + to, | |
method: 'GET', | |
headers: { | |
'User-Agent': "Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1" | |
} | |
}; | |
http.get(options, function(res) { | |
res.on('data', function(data) { | |
// collect the data chunks to the variable named "html" | |
html += data; | |
}).on('end', function() { | |
// the whole of webpage data has been collected. parsing time! | |
var time = $(html).find('h2 span:first').text(); | |
console.log("time: " + time) | |
var hours = 0; | |
var minutes = 0; | |
var value = 0; | |
var args = time.split(' '); | |
var state = null; | |
for (i in args) { | |
var arg = args[i].trim(); | |
if (['hr', 'hrs'].indexOf(arg) > -1) { | |
hours = value; | |
} else if (['min', 'mins'].indexOf(arg) > -1) { | |
minutes = value; | |
} else { | |
value = parseInt(arg); | |
} | |
} | |
callback((hours * 60) + minutes); | |
}).on('error', function() { | |
console.log("HTTP Error") | |
}); | |
}); | |
}, | |
total: 0, | |
seen: 0, | |
add_up: function (duration) { | |
ietf_airports.total += (duration * 2); | |
ietf_airports.seen += 1; | |
if (ietf_airports.seen == ietf_airports.IETF_airports.length) { | |
var hours = Math.floor(ietf_airports.total / 60); | |
var mins = ietf_airports.total % 60; | |
console.log(hours + ":" + mins); | |
} | |
} | |
} | |
ietf_airports.run(argv._[0]); | |
// ietf_airports.flying_time('MEL', "SFO", console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment