Created
October 30, 2014 17:08
-
-
Save ryanstevens/f510f9355ad1bdcfd524 to your computer and use it in GitHub Desktop.
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
var microdom = require('microdom'); | |
var createSOAPClient = require('soap').createClient; | |
var argv = require('optimist').argv; | |
var Table = require('cli-table'); | |
// ref: http://graphical.weather.gov/xml/ | |
createSOAPClient( | |
'http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl', | |
function clientCreated(err, client) { | |
if (err) { | |
console.error('an error has occurred'); | |
console.error(err.stack); | |
return; | |
} | |
// get the lat/lon for "primary cities" | |
client.LatLonListCityNames({displayLevel: 1}, function primaryCitiesCallback(e, r) { | |
var dom = microdom(r.listLatLonOut['$value'].replace(/\n/g, '')); | |
var coords = dom.child(0).child(0).child(0).value.split(' ').map(function(a) { | |
// split lat lon | |
return a.split(',').map(parseFloat); | |
}); | |
var cities = {}; | |
dom.child(0).child(1).child(0).value.split('|').forEach(function(city, i) { | |
var parts = city.split(','); | |
cities[parts[0]] = { | |
latlon : coords[i], | |
state: parts[1] | |
} | |
}); | |
var target = argv._[0]; | |
if (!target || !cities[target]) { | |
console.log('provide a city (' + Object.keys(cities).join(', ') + ')'); | |
return; | |
} | |
var city = cities[target]; | |
var date = new Date(); | |
var body = { | |
latitude: city.latlon[0], | |
longitude: city.latlon[1], | |
startDate: [date.getFullYear(), ('0' + (date.getUTCMonth() + 1)).substring(-2), date.getUTCDate()].join('-'), | |
numDays: 7, | |
Unit: 'e', | |
format: '24 hourly' | |
}; | |
client.NDFDgenByDay(body, function(e, r) { | |
if (e) { | |
throw e; | |
} | |
var forcastDom = microdom(r.dwmlByDayOut['$value'].replace(/\n/g, '').replace(/ *</g, '<')); | |
var forcastData = forcastDom.child(0).child(1).child(5); | |
var highs = forcastData.child(0); | |
var lows = forcastData.child(1); | |
var table = new Table({ | |
head: ['day', 'high', 'low'] | |
}); | |
for (var i=0; i<7; i++) { | |
var high = highs.child(i+1).child(0); | |
var low = lows.child(i+1).child(0); | |
var row = [ | |
'+' + i, | |
high ? high.value : '-', | |
low ? low.value : '-', | |
]; | |
table.push(row); | |
} | |
console.log(table.toString()); | |
}); | |
}); | |
}); | |
function extractTags(xml, tagArray) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment