Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Created February 13, 2014 16:08
Show Gist options
  • Save jashkenas/8977952 to your computer and use it in GitHub Desktop.
Save jashkenas/8977952 to your computer and use it in GitHub Desktop.
var request = require('request');
var _ = require('underscore');
var url = "http://forecast.weather.gov/product.php?site=NWS&issuedby=OKX&product=PNS&format=txt&version=1&glossary=0";
var header = "********************STORM TOTAL SNOWFALL********************";
var footer = "**********************24 HOUR SNOWFALL**********************";
request(url, function(err, resp, body) {
if (err) throw err;
var text = body.split(header)[1];
text = text.split(footer)[0];
var lines = _.compact(text.split(/\n/));
lines = lines.slice(3);
var states = ['CONNECTICUT', 'NEW JERSEY', 'NEW YORK'];
var results = [];
var currentState, currentCounty;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (_.contains(states, line)) {
currentState = line;
} else if (/^\.\.\./.test(line)) {
currentCounty = line.match(/\.\.\.(.+) COUNTY/)[1];
} else {
var tuple = _.compact(line.split(/\s{2,}/));
results.push({
snowfall: tuple[1],
place: tuple[0],
county: currentCounty,
state: currentState,
time: tuple[2],
date: tuple[3],
source: tuple[4]
});
}
}
console.log(results);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment