Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified LICENCE
100644 → 100755
Empty file.
92 changes: 84 additions & 8 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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<img src="http://l.yimg.com/a/i/us/we/52/29.gif"/><br />\n<b>Current Conditions:</b><br />\nShowers in the Vicinity, 57 F<BR />\n<BR /><b>Forecast:</b><BR />\nThu - PM Light Rain. High: 62 Low: 45<br />\nFri - Partly Cloudy. High: 70 Low: 48<br />\nSat - Mostly Sunny. High: 78 Low: 50<br />\nSun - Partly Cloudy. High: 57 Low: 42<br />\nMon - Partly Cloudy. High: 62 Low: 42<br />\n<br />\n<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Melbourne__AU/*http://weather.yahoo.com/forecast/ASXX0075_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>\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' }
]
}
```

Expand Down
Empty file modified package.json
100644 → 100755
Empty file.
220 changes: 147 additions & 73 deletions yahoo.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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<forecasts.length; i++) {

var forecast = forecasts[i]['$'];
weather.days.push({
day : forecast.day, //Day of the week (english, 3 letters, ex: Wed)
date : forecast.date, //Date, (english, dd Mmm yyyy)
low : forecast.low,
high : forecast.high,
text : forecast.text, //Description of conditions (ex: Partly Cloudy)
code : forecast.code, //@see http://developer.yahoo.com/weather/#codes
});

}

callback(weather);

} catch(e) {
onError('Failed to find weather');
return;
}

});
});
};






/**
* Get yahoo location base on geolocation.
*/
yahoo.where = function(options, callback) {
var url = printf(
"http://where.yahooapis.com/v1/places.q(\"%s\")?appid=%s",
options.location,
options.appid
);

if (yahoo.logging) {
console.log('Requesting %s', url);
}

request(url, function(error, res, body) {
var parser = new xml2js.Parser();

if (body) {
parser.parseString(body, function (err, result) {
try {
var woeid = result.place.woeid;
callback(woeid);
} catch(e) {
onError('Failed to fetch woeid');
}
});
} else {
onError(error);
}
});

var url = printf(
"http://where.yahooapis.com/v1/places.q(\"%s\")?appid=%s",
options.location,
options.appid
);

if (yahoo.logging) {
console.log('Requesting %s', url);
}

request(url, function(error, res, body) {

var parser = new xml2js.Parser();

if (body) {
parser.parseString(body, function (err, result) {
try {
var woeid = result.places.place[0].woeid[0];
callback(woeid);
} catch(e) {
onError('Failed to fetch woeid');
}
});
} else {
onError(error);
}
});
};