0

We've been using the Yahoo weather feed for our Intranet for several years with no issues. Today, it stopped working. I determined that it was no longer returning results if we pass the zip code. So, a slight mod to the query and now we send the WOEID and Yahoo is happy once again.

However... before this broke, we were able to see the next 5 days of weather, which is fine. Now, Yahoo is returning a 10 day forecast which results in a much larger/longer weather panel on the page. I'd like to go back to 5 or even 7 days. I've tried various things based on other YQL queries such as adding LIMIT 5 to the end of the query, no good. Added (5) after the table name as they do for some of their other queries and that did not work.

Is it not possible to limit the number of days returned? Or do I have to just get all 10 days back and then limit what I display when looping through the results?

We're using the jquery.zWeatherFeed plugin by Zazar which is no longer supported:

/**
 * Plugin: jquery.zWeatherFeed
 * 
 * Version: 1.2.1
 * (c) Copyright 2011-2013, Zazar Ltd
 * 
 * Description: jQuery plugin for display of Yahoo! Weather feeds
 * 
 * History:
 * 1.2.1 - Handle invalid locations
 * 1.2.0 - Added forecast data option
 * 1.1.0 - Added user callback function
 *         New option to use WOEID identifiers
 *         New day/night CSS class for feed items
 *         Updated full forecast link to feed link location
 * 1.0.3 - Changed full forecast link to Weather Channel due to invalid Yahoo! link
       Add 'linktarget' option for forecast link
 * 1.0.2 - Correction to options / link
 * 1.0.1 - Added hourly caching to YQL to avoid rate limits
 *         Uses Weather Channel location ID and not Yahoo WOEID
 *         Displays day or night background images
 *
 **/

(function($){

    $.fn.weatherfeed = function(locations, options, fn) {   

        // Set plugin defaults
        var defaults = {
            unit: 'c',
            image: true,
            country: false,
            highlow: true,
            wind: true,
            humidity: false,
            visibility: false,
            sunrise: false,
            sunset: false,
            forecast: false,
            link: true,
            showerror: true,
            linktarget: '_self',
            //woeid: true,
            refresh: 30
        };  
        var options = $.extend(defaults, options); 
        var row = 'odd';

        // Functions
        return this.each(function(i, e) {
            var $e = $(e);

            // Add feed class to user div
            if (!$e.hasClass('weatherFeed')) $e.addClass('weatherFeed');

            // Check and append locations
            if (!$.isArray(locations)) return false;

            var count = locations.length;
            if (count > 10) count = 10;

            var locationid = '';

            for (var i=0; i<count; i++) {
                if (locationid != '') locationid += ',';
                locationid += "'"+ locations[i] + "'";
            }

            // Cache results for an hour to prevent overuse
            now = new Date();

            // Select location ID type
            //var queryType = options.woeid ? 'woeid' : 'location';

            // Create Yahoo Weather feed API address
            var query = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=" + locationid + ")";
            var api = 'https://query.yahooapis.com/v1/public/yql?q='+ encodeURIComponent(query) +'&rnd='+ now.getFullYear() + now.getMonth() + now.getDay() + now.getHours() +'&format=json&callback=?';

            // Request feed data
            sendRequest(query, api, options);

            if (options.refresh > 0) {

                // Set timer interval for scrolling     
                var interval = setInterval(function(){ sendRequest(query, api, options); }, options.refresh * 60000);
            }

            // Function to gather new weather data
            function sendRequest(query, api, options) {

                // Reset odd and even classes
                row = 'odd';

                // Clear user div
                $e.html('');

                $.ajax({
                    type: 'GET',
                    url: api,
                    dataType: 'json',
                    success: function(data) {

                        if (data.query) {

                            if (data.query.results.channel.length > 0 ) {

                                // Multiple locations
                                var result = data.query.results.channel.length;
                                for (var i=0; i<result; i++) {

                                    // Create weather feed item
                                    _process(e, data.query.results.channel[i], options);
                                }
                            } else {

                                // Single location only
                                _process(e, data.query.results.channel, options);
                            }

                            // Optional user callback function
                            if ($.isFunction(fn)) fn.call(this,$e);

                        } else {
                            if (options.showerror) $e.html('<p>Weather information unavailable</p>');
                        }
                    },
                    error: function(data) {
                        if (options.showerror) $e.html('<p>Weather request failed</p>');
                    }
                });             
            };

            // Function to each feed item
            var _process = function(e, feed, options) {
                var $e = $(e);

                // Check for invalid location
                if (feed.description != 'Yahoo! Weather Error') {

                    // Format feed items
                    var wd = feed.wind.direction;
                    if (wd>=348.75&&wd<=360){wd="N"};if(wd>=0&&wd<11.25){wd="N"};if(wd>=11.25&&wd<33.75){wd="NNE"};if(wd>=33.75&&wd<56.25){wd="NE"};if(wd>=56.25&&wd<78.75){wd="ENE"};if(wd>=78.75&&wd<101.25){wd="E"};if(wd>=101.25&&wd<123.75){wd="ESE"};if(wd>=123.75&&wd<146.25){wd="SE"};if(wd>=146.25&&wd<168.75){wd="SSE"};if(wd>=168.75&&wd<191.25){wd="S"};if(wd>=191.25 && wd<213.75){wd="SSW"};if(wd>=213.75&&wd<236.25){wd="SW"};if(wd>=236.25&&wd<258.75){wd="WSW"};if(wd>=258.75 && wd<281.25){wd="W"};if(wd>=281.25&&wd<303.75){wd="WNW"};if(wd>=303.75&&wd<326.25){wd="NW"};if(wd>=326.25&&wd<348.75){wd="NNW"};
                    var wf = feed.item.forecast[0];

                    // Determine day or night image
                    wpd = feed.item.pubDate;
                    n = wpd.indexOf(":");
                    tpb = _getTimeAsDate(wpd.substr(n-2,8));
                    tsr = _getTimeAsDate(feed.astronomy.sunrise);
                    tss = _getTimeAsDate(feed.astronomy.sunset);

                    // Get night or day
                    if (tpb>tsr && tpb<tss) { daynight = 'day'; } else { daynight = 'night'; }

                    // Add item container
                    var html = '<div class="weatherItem '+ row +' '+ daynight +'"';
                    if (options.image) html += ' style="background-image: url(http://l.yimg.com/a/i/us/nws/weather/gr/'+ feed.item.condition.code + daynight.substring(0,1) +'.png); background-repeat: no-repeat;"';
                    html += '>';

                    // Add item data
                    html += '<div class="weatherTemp">'+ feed.item.condition.temp +'&deg;</div>';
                    html += '<div class="weatherDesc">'+ feed.item.condition.text +'</div>';

                    // Add optional data
                    html += '<br style="clear:both;">';
                    if (options.highlow) html += '<div class="weatherRange">High: '+ wf.high +'&deg; &nbsp; Low: '+ wf.low +'&deg;</div>';
                    if (options.wind) html += '<div class="weatherWind">Wind: '+ wd +' '+ feed.wind.speed + feed.units.speed +'</div>';
                    if (options.humidity) html += '<div class="weatherHumidity">Humidity: '+ feed.atmosphere.humidity +'%</div>';
                    if (options.visibility) html += '<div class="weatherVisibility">Visibility: '+ feed.atmosphere.visibility +'</div>';
                    if (options.sunrise) html += '<div class="weatherSunrise">Sunrise: '+ feed.astronomy.sunrise + '  &nbsp;  Sunset: '+ feed.astronomy.sunset +'</div>';

                    // Add item forecast data
                    if (options.forecast) {

                        html += '<div class="weatherForecast">';

                        var wfi = feed.item.forecast;

                        for (var i=0; i<wfi.length; i++) {
                            html += '<div class="weatherForecastItem" style="background-image: url(http://l.yimg.com/a/i/us/nws/weather/gr/'+ wfi[i].code +'s.png); background-repeat: no-repeat;">';
                            html += '<div class="weatherForecastDay">'+ _getDateAsString(wfi[i].date) +'</div>';
                            html += '<div class="weatherForecastText">'+ wfi[i].text +'</div>';
                            html += '<div class="weatherForecastRange">High: '+ wfi[i].high +'&deg; &nbsp; Low: '+ wfi[i].low +'&deg;</div>';
                            html += '</div>'
                        }

                        html += '</div>'
                    }

                    if (options.link) html += '<div class="weatherLink"><a href="'+ feed.link +'" target="'+ options.linktarget +'" title="Read full forecast">View complete forecast</a></div>';

                } else {
                    var html = '<div class="weatherItem '+ row +'">';
                    html += '<div class="weatherError">City not found</div>';
                }

                html += '</div>';

                // Alternate row classes
                if (row == 'odd') { row = 'even'; } else { row = 'odd'; }

                $e.append(html);
            };

            // Get time string as date
            var _getTimeAsDate = function(t) {
                d = new Date();
                r = new Date(d.toDateString() +' '+ t);

                return r;
            };

            // Get date as full string
            var _getDateAsString = function(d) {
                var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
                var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                var orgDate = new Date(d);

                var dateAsString = dayNames[orgDate.getDay()] + ", " + monthNames[orgDate.getMonth()] + " " + orgDate.getDate() + ", " + orgDate.getFullYear();

                return dateAsString;
            };

        });
    };

})(jQuery);
Connie DeCinko
  • 996
  • 5
  • 19
  • 39
  • can you make `if (count > 10) count = 5;` and test it? – Kalpesh Singh Mar 25 '16 at 16:44
  • in your yql query try using a remote limit, to limit the number of rows retrieved from the datasource, the first value is the offset and the second value is number of rows, i.e (0,5) `var query = "select * from weather.forecast(0,5) where woeid in (select woeid from geo.places(1) where text=" + locationid + ")";` – user2950720 Mar 25 '16 at 16:51
  • worst case can always splice response array or limit the loop – charlietfl Mar 25 '16 at 17:15
  • No change for either of the above. Both suggestions make sense based upon their documentation. You can test the query via their testing tool at https://developer.yahoo.com/yql/ and I still get all 10 days back. select * from weather.forecast(0,5) where woeid in (select woeid from geo.places(1) where text=85016) – Connie DeCinko Mar 25 '16 at 17:22
  • My solution for now, is to limit the loop to the first 5 forecasts. It means I'm pulling back more information than I need but until I can figure out how to limit that... – Connie DeCinko Mar 25 '16 at 17:30

0 Answers0