I started using Yahoo\'s free weather API to get the weather data I need, but it seems each time I request a city weather data there is a chance that either I get updated data o
I have a workaround: repeatedly call the api until you get a valid result. Here is the code I used to make my own custom weather widget: (when you test the code, it sometimes returns data not available even after 30 tries!)
I also tweeted on #YDN a few days back, but did not get any response.
var ydnwthr={};
var ydnStaleness=100000; //YDN weather api is f'ed up and returns stale data, so hammer it repeatedly and then test for data staleness
var abortydn=30; //abort after these many calls
var delayBetweencalls=200; //in milliseconds
var ydncounter=0;
var ydnInterval;
var apiquery=escape('select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="lansing, mi")');
var ydnapiurl='https://query.yahooapis.com/v1/public/yql?q='+apiquery+'&format=json&env='+escape("store://datatables.org/alltableswithkeys");
$(function() {
function markupWeather(){
if(ydnStaleness>1.5) $("#weather").html('weather data not available..');
else{
/****************THIS IS WHERE YOU DO WHATEVER YOU WANT WITH THE "GOOD" ydnwthr OBJECT ***********/
var wthrMarkup = "" + ydnwthr.title.replace(/Conditions for | e[sd]t/ig,"") + "" +ydncounter+""+"";
wthrMarkup += "";
wthrMarkup += ydnwthr.condition.text + ", " + ydnwthr.condition.temp +"°";
$("#weather").html(wthrMarkup);
$("#swBodyBg").css('background-image', 'url(' + /".*"/.exec(ydnwthr.description) + ')');
for (i=0;i<5;i++){ //get 5 day weather and fit to container - (done without jquery for convenience)
var el = document.createElement("div");
el.innerText = ydnwthr.forecast[i].day.substr(0,2) + ": " + ydnwthr.forecast[i].text + ", " + ydnwthr.forecast[i].high + "/"+ ydnwthr.forecast[i].low;
$("#swBody")[0].appendChild(el);
while(parseInt(window.getComputedStyle(el, null).getPropertyValue('height')) > 42) {
var fontSize = parseFloat(window.getComputedStyle(el, null).getPropertyValue('font-size'));
el.style.fontSize = (fontSize - 1) + 'px';
}
}
}
}//end markupWeather
ydnInterval=setInterval(function(){
if(ydnStaleness<=2 || ydncounter>=abortydn) {
clearInterval(ydnInterval);
markupWeather();
}
$.ajax({url:ydnapiurl}).done(function(data){
ydnwthr = data.query.results.channel.item;
if(ydnwthr.pubDate) ydnStaleness=(new Date()-new Date(ydnwthr.pubDate))/3600000;
});
ydncounter++;
},delayBetweencalls);
});
#weather {border:3px ridge silver;border-radius:5px;cursor:pointer;width:180px;font:10pt/28px arial,sans-serif}
#swHead {background:#bddeff;font-weight:bold;text-align:center;border-bottom:1px solid silver}
#swBodyBg{position:absolute;background-position:center;background-size:100px 100px;background-repeat:no-repeat;opacity: 0.5;height:100%;width:100%;z-index:-1}
#swBody{margin:0px 3px;position:relative;}
#swCurrent{padding:5px;font:bold 1.1em arial; width:100%;text-align:center}
getting weather...