Andy McKay

Mar 16, 2009

Putting weather onto a site


Just following on from the last post about Cleartrain, someone asked in irc which product to use to put the weather onto their Plone site. There's a bunch of old ones out there. The simple answer is, don't. Just do it in Javascript, for example:

    <div id="weather-feed" />
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    
    google.load("feeds", "1");

    var rss = new Object();

    rss.url = "http://weather.yahooapis.com/forecastrss?p=";
    rss.node = "CAXX0518";  // vancouver

    function initialize() {
        var feed = new google.feeds.Feed(rss.url + rss.node);
        feed.load(function(result) {
          if (!result.error) {
            var msg = document.getElementById("weather-feed");
            msg.innerHTML = msg.innerHTML + '<div>' + result.feed.title + '</div>';
            for (var i = 0; i < result.feed.entries.length; i++) {
              var entry = result.feed.entries[i];
              var link = '<a href="' + entry.link + '">' + entry.title + '</a>';
              var para = '<p>' + entry.content + '</p>';
              msg.innerHTML = msg.innerHTML + '<di >' + link + para + '</div>';
            };
          }
        });
    };

    google.setOnLoadCallback(initialize);
    
    </script>

This pulls in the weather from Yahoo, via Google. See http://developer.yahoo.com/weather/#request for more.

Example:

<div id="weather-feed" />
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

google.load("feeds", "1");

var rss = new Object();

rss.url = "http://weather.yahooapis.com/forecastrss?p=";
rss.node = "CAXX0518";  // vancouver

function initialize() {
    var feed = new google.feeds.Feed(rss.url + rss.node);
    feed.load(function(result) {
      if (!result.error) {
        var msg = document.getElementById("weather-feed");
        msg.innerHTML = msg.innerHTML + '<div>' + result.feed.title + '</div>';
        for (var i = 0; i < result.feed.entries.length; i++) {
          var entry = result.feed.entries[i];
          var link = '<a href="' + entry.link + '">' + entry.title + '</a>';
          var para = '<p>' + entry.content + '</p>';
          msg.innerHTML = msg.innerHTML + '<di >' + link + para + '</div>';
        };
      }
    });
};

google.setOnLoadCallback(initialize);

</script>