Skip to content

Instantly share code, notes, and snippets.

@mvark
Last active June 29, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mvark/5231461 to your computer and use it in GitHub Desktop.
Save mvark/5231461 to your computer and use it in GitHub Desktop.
Get Weather information for a WOEID using YQL, jQuery
<!DOCTYPE html>
<html>
<head>
<title>Weather Forecast using YQL</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
//Developer notes - http://mvark.blogspot.in/2013/03/how-to-find-weather-information-for.html
//use YQL Console to build query - http://developer.yahoo.com/yql/console/
function getWeather(woeid, unit) {
var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid=" + woeid + " %20and%20u=%27" + unit +"%27&format=json&callback=cbfunc";
$.getScript(url, function(data) { } );
}
window.cbfunc = function(data){
var forecast='';
forecast = "<br>" + data.query.results.channel.description + "<br>";
forecast += data.query.results.channel.item.description;
$("#weather").append(forecast);
};
getWeather(2295420, "c"); //Bangalore temperature in Centigrade
getWeather(2459115, "f"); //New York temperature in Fahrenheit
getWeather(2490383, "f"); //Seattle temperature in Fahrenheit
/* JSONP response returned by YQL query
cbfunc({
"query": {
"count": 1,
"created": "2013-03-24T09:42:39Z",
"lang": "en-US",
"results": {
"channel": {
"title": "Yahoo! Weather - Bangalore, IN",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html",
"description": "Yahoo! Weather for Bangalore, IN",
"language": "en-us",
"lastBuildDate": "Sun, 24 Mar 2013 2:31 pm IST",
"ttl": "60",
"location": {
"city": "Bangalore",
"country": "India",
"region": "KA"
},
"units": {
"distance": "km",
"pressure": "mb",
"speed": "km/h",
"temperature": "C"
},
"wind": {
"chill": "32",
"direction": "80",
"speed": "16.09"
},
"atmosphere": {
"humidity": "14",
"pressure": "982.05",
"rising": "0",
"visibility": "8"
},
"astronomy": {
"sunrise": "6:20 am",
"sunset": "6:30 pm"
},
"image": {
"title": "Yahoo! Weather",
"width": "142",
"height": "18",
"link": "http://weather.yahoo.com",
"url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
},
"item": {
"title": "Conditions for Bangalore, IN at 2:31 pm IST",
"lat": "12.96",
"long": "77.62",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html",
"pubDate": "Sun, 24 Mar 2013 2:31 pm IST",
"condition": {
"code": "32",
"date": "Sun, 24 Mar 2013 2:31 pm IST",
"temp": "32",
"text": "Sunny"
},
"description": "\n<img src=\"http://l.yimg.com/a/i/us/we/52/32.gif\"/><br />\n<b>Current Conditions:</b><br />\nSunny, 32 C<BR />\n<BR /><b>Forecast:</b><BR />\nSun - Clear. High: 33 Low: 18<br />\nMon - Sunny. High: 34 Low: 20<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n",
"forecast": [
{
"code": "31",
"date": "24 Mar 2013",
"day": "Sun",
"high": "33",
"low": "18",
"text": "Clear"
},
{
"code": "32",
"date": "25 Mar 2013",
"day": "Mon",
"high": "34",
"low": "20",
"text": "Sunny"
}
],
"guid": {
"isPermaLink": "false",
"content": "INXX0012_2013_03_25_7_00_IST"
}
}
}
}
}
});
*/
</script>
</head>
<body>
<div id="weather"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment