2011-06-01 17:55:35 +04:00
|
|
|
// Get weather info for given address (or for the default one, "Mountain View")
|
|
|
|
|
2011-09-13 00:17:54 +04:00
|
|
|
var page = require('webpage').create(),
|
2011-06-01 17:55:35 +04:00
|
|
|
address = "Mountain View"; //< default value
|
|
|
|
|
|
|
|
// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
|
|
|
|
page.onConsoleMessage = function(msg) {
|
|
|
|
console.log(msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Print usage message, if no twitter ID is passed
|
|
|
|
if (phantom.args.length < 1) {
|
|
|
|
console.log("Usage: weather.js [address]");
|
2010-12-28 09:39:12 +03:00
|
|
|
} else {
|
2011-06-01 17:55:35 +04:00
|
|
|
address = phantom.args.join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heading
|
|
|
|
console.log("*** Loading weather information for '" + address + "' ***\n");
|
2010-12-28 09:39:12 +03:00
|
|
|
|
2011-06-01 17:55:35 +04:00
|
|
|
// Open Google "secret" Weather API and, onPageLoad, do...
|
|
|
|
page.open(encodeURI('http://www.google.com/ig/api?weather=' + address), function (status) {
|
|
|
|
// Check for page load success
|
|
|
|
if (status !== "success") {
|
|
|
|
console.log("Unable to access network");
|
2011-01-01 09:17:51 +03:00
|
|
|
} else {
|
2011-06-01 17:55:35 +04:00
|
|
|
// Execute some DOM inspection within the page context
|
|
|
|
page.evaluate(function() {
|
|
|
|
if (document.querySelectorAll('problem_cause').length > 0) {
|
|
|
|
console.log('No data available for ' + address);
|
|
|
|
} else {
|
|
|
|
function data (s, e) {
|
|
|
|
var el;
|
|
|
|
e = e || document;
|
|
|
|
el = e.querySelector(s);
|
|
|
|
return el ? el.attributes.data.value : undefined;
|
|
|
|
};
|
2010-12-28 09:39:12 +03:00
|
|
|
|
2011-06-01 17:55:35 +04:00
|
|
|
console.log('City: ' + data('weather > forecast_information > city'));
|
|
|
|
console.log('Current condition: ' + data('weather > current_conditions > condition'));
|
|
|
|
console.log('Temperature: ' + data('weather > current_conditions > temp_f') + ' F');
|
|
|
|
console.log(data('weather > current_conditions > humidity'));
|
|
|
|
console.log(data('weather > current_conditions > wind_condition'));
|
|
|
|
console.log('');
|
2010-12-28 09:39:12 +03:00
|
|
|
|
2011-06-01 17:55:35 +04:00
|
|
|
var forecasts = document.querySelectorAll('weather > forecast_conditions');
|
|
|
|
for (var i = 0; i < forecasts.length; ++i) {
|
|
|
|
var f = forecasts[i];
|
|
|
|
console.log(data('day_of_week', f) + ': ' +
|
|
|
|
data('low', f) + '-' + data('high', f) + ' F ' +
|
|
|
|
data('condition', f));
|
|
|
|
}
|
2011-01-01 09:17:51 +03:00
|
|
|
}
|
2011-06-01 17:55:35 +04:00
|
|
|
});
|
2010-12-28 09:39:12 +03:00
|
|
|
}
|
|
|
|
phantom.exit();
|
2011-06-01 17:55:35 +04:00
|
|
|
});
|