I got node.js working with jQuery and this plugin: http://simpleweatherjs.com/. Now I want to use the weather data for another service instead of putting it into the HTML, but I can't access/print the data. The error function works, the success function doesn't.
var jsdom = require("jsdom");
jsdom.env({
html: '<html><body><div id="weather"></div></body></html>',
scripts: [
'http://code.jquery.com/jquery-2.1.1.min.js',
'http://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.0.2/jquery.simpleWeather.min.js'
],
done: function(errors, window) {
var $ = window.jQuery;
$.simpleWeather({
location: 'Paris',
woeid: '615702',
unit: 'c',
success: function(weather) {
console.log(weather.temp+'°'+weather.units.temp);
},
error: function(error) {
console.log(error.message);
}
});
}
});
This is how it would look normally http://codepen.io/fleeting/pen/xwpar
There's a node module for the plugin, too, but I don't know how to get it to work and there's no documentation.
I'd suggest not to use the jQuery plugin, but rather use the underlaying data directly.
Simpleweather uses a yahoo API to retrieve the weather information.
In order to get the weather information in a easy to use JSON format, you can use yahoo's Query language
1. Get the location ID
For example if I wanted to get the weather information in "Stockholm, Sweden" I could query for
SELECT woeid FROM geo.places where text="Stockholm, Sweden" LIMIT 1
which would return "woeid": "906057" [Execute YQL]
2. Get the weather info
Now it's time to use the location ID we retrieved in the previous step.
SELECT item.condition.temp FROM weather.forecast WHERE woeid = 906057
This will return "temp": "81" [Execute YQL]
Automating it
In order to programaticaly receive the temperature in node, I'd suggest using the request module. You can copy the API endopint from the query builder page. In this example it would be
I guess you could easily go from here, but I'll incldue a simple node example for the sake of completeness:
var request = require('request');
var url = 'https://query.yahooapis.com/v1/public/yql?q=SELECT%20item.condition.temp%20FROM%20weather.forecast%20WHERE%20woeid%20%3D%20906057&format=json';
request(url, function (err, resp, body) {
if (err || resp.statusCode != 200)
return console.log('Could not get weather information');
var json = JSON.parse(body);
console.log('Temperature in Stockholm:', json.query.results.channel.item.condition.temp);
});
jsdom does not implement XMLHttpRequest. See this issue for a possible solution.