From 9661a4839ff7056dbd469f738611ebb88bec5c73 Mon Sep 17 00:00:00 2001 From: Guillaume Wuip Date: Wed, 9 Oct 2013 22:10:03 +0200 Subject: [PATCH 1/2] An expanded version. Wrap the whole xml into JSON. Update README too. --- README.md | 92 +++++++++++++++++++++-- yahoo.js | 220 ++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 231 insertions(+), 81 deletions(-) mode change 100644 => 100755 README.md mode change 100644 => 100755 yahoo.js diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 0ff9ba8..dcd8c94 --- a/README.md +++ b/README.md @@ -1,23 +1,99 @@ -## Example + +You can get all informations available in the xml as explain in th Yahoo! doc here : [http://developer.yahoo.com/weather](http://developer.yahoo.com/weather). + +## How to use ? + An example of how to get the weather based on location. ```javascript var weather = require('weather'); -weather({location: 'Melbourne'}, function(data) { - if (data.temp > 30) { +var params = { + location : 'Melbourne', + unit : 'f', // Celcius(default, "c") or Fahrenheit ("f") + appid : "MyYahhoAppId", + logging : false //Debug info or not +} + +weather(params, function(data) { + if (data.condition.temp > 30) { console.log("Damn it's hot!"); } }); ``` - Example JSON result: +###Example JSON result: ```javascript -{ - temp: 18, // Current temperature - high: 20, // High for the day - low: 9, // Low for the day +{ + date: 'Thu, 10 Oct 2013 5:29 am AEST', + location: { + city: 'Melbourne', + region: 'VIC', + country: 'Australia', + lat: '-37.87', + long: '145.1' + }, + units: { + temperature: 'F', + distance: 'mi', + pressure: 'in', + speed: 'mph' + }, + wind: { + chill: '57', + direction: '50', + speed: '8' + }, + atmosphere: { + humidity: '82', + visibility: '6.21', + pressure: '29.59', + rising: '0' + }, + astronomy: { + sunrise: '5:42 am', + sunset: '6:30 pm' + }, + condition: { + text: 'Showers in the Vicinity', + code: '29', + temp: '57', + date: 'Thu, 10 Oct 2013 5:29 am AEST' + }, + description: '\n
\nCurrent Conditions:
\nShowers in the Vicinity, 57 F
\n
Forecast:
\nThu - PM Light Rain. High: 62 Low: 45
\nFri - Partly Cloudy. High: 70 Low: 48
\nSat - Mostly Sunny. High: 78 Low: 50
\nSun - Partly Cloudy. High: 57 Low: 42
\nMon - Partly Cloudy. High: 62 Low: 42
\n
\nFull Forecast at Yahoo! Weather

\n(provided by The Weather Channel)
\n', + days: + [ { day: 'Thu', + date: '10 Oct 2013', + low: '45', + high: '62', + text: 'PM Light Rain', + code: '11' }, + { day: 'Fri', + date: '11 Oct 2013', + low: '48', + high: '70', + text: 'Partly Cloudy', + code: '30' }, + { day: 'Sat', + date: '12 Oct 2013', + low: '50', + high: '78', + text: 'Mostly Sunny', + code: '34' }, + { day: 'Sun', + date: '13 Oct 2013', + low: '42', + high: '57', + text: 'Partly Cloudy', + code: '30' }, + { day: 'Mon', + date: '14 Oct 2013', + low: '42', + high: '62', + text: 'Partly Cloudy', + code: '30' } + ] } ``` diff --git a/yahoo.js b/yahoo.js old mode 100644 new mode 100755 index 8034729..de7373e --- a/yahoo.js +++ b/yahoo.js @@ -1,95 +1,169 @@ -var xml2js = require('xml2js'), - request = require('request'), - printf = require('util').format; + +/** + * + * Yahoo! Weather - NodeJS Module + * + */ + + +var xml2js = require('xml2js'), + request = require('request'), + printf = require('util').format; + +/* + Init + */ +var yahoo = { + logging: false +}; + + + module.exports = function(options, callback) { - yahoo.logging = options.logging; - yahoo.where(options, function(woeid) { - yahoo.weather(woeid, callback); - }); + + yahoo.logging = options.logging; + + yahoo.where(options, function(woeid) { + //Default unit is celcius + if(!options.unit) { + options.unit = 'c'; + } + + yahoo.weather(woeid, options.unit, callback); + }); }; + + + + /** * Report error. */ var onError = function(msg) { - if (yahoo.logging) { - console.log('ERROR: ' + msg); - } + if (yahoo.logging) { + console.log('ERROR: ' + msg); + } - if (module.exports.error) { - module.exports.error(msg); - } + if (module.exports.error) { + module.exports.error(msg); + } }; -var yahoo = { - logging: false -}; + + + + /** * Get the Yahoo weather based on geolocation. */ -yahoo.weather = function(woeid, callback) { - var url = 'http://weather.yahooapis.com/forecastrss?w='+woeid+'&u=c'; - - if (yahoo.logging) { - console.log('Requesting %s', url); - } - - request(url, function(error, res, body) { - var parser = new xml2js.Parser(); - parser.parseString(body, function (err, result) { - var weather = {}, - high = /(?:High:\s)(-?\d+)/g, - low = /(?:Low:\s)(-?\d+)/g; - - try { - // Get the current temp - var condition = result.channel.item['yweather:condition']['@']; - weather = condition; - - // Get the high/low currently stored in Yahoo widget. - var description = result.channel.item['description']; - weather.high = high.exec(description)[1]; - weather.low = low.exec(description)[1]; - } catch(e) { - onError('Failed to find weather'); - return; - } - - callback(weather); - }); - }); +yahoo.weather = function(woeid, unit, callback) { + + var url = 'http://weather.yahooapis.com/forecastrss?w='+woeid+'&u='+unit; + + if (yahoo.logging) { + console.log('Requesting %s', url); + } + + request(url, function(error, res, body) { + + var parser = new xml2js.Parser(); + + parser.parseString(body, function (err, result) { + + + try { + + var weather = { + + //The date + date : result.rss.channel[0].item[0].pubDate[0], + + //Some general informations + //@see http://developer.yahoo.com/weather/#channel + location : result.rss.channel[0]['yweather:location'][0]['$'], + units : result.rss.channel[0]['yweather:units'][0]['$'], + wind : result.rss.channel[0]['yweather:wind'][0]['$'], + atmosphere : result.rss.channel[0]['yweather:atmosphere'][0]['$'], + astronomy : result.rss.channel[0]['yweather:astronomy'][0]['$'], + + //Item elements + //@see http://developer.yahoo.com/weather/#item + condition : result.rss.channel[0].item[0]['yweather:condition'][0]['$'], //Current conditions + description : result.rss.channel[0].item[0].description[0], //HTML widget + + days : [] + }; + + //We add latitude and longitude info to the location object + weather.location.lat = result.rss.channel[0].item[0]['geo:lat'][0]; + weather.location.long = result.rss.channel[0].item[0]['geo:long'][0]; + + + //Extract forecasts for nexts days + var forecasts = result.rss.channel[0].item[0]['yweather:forecast']; + for(var i=0; i Date: Wed, 9 Oct 2013 22:10:22 +0200 Subject: [PATCH 2/2] Nothing new. --- LICENCE | 0 package.json | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 LICENCE mode change 100644 => 100755 package.json diff --git a/LICENCE b/LICENCE old mode 100644 new mode 100755 diff --git a/package.json b/package.json old mode 100644 new mode 100755