阻止Google Feed API使用缓存Feed

时间:2012-11-15 16:29:27

标签: rss google-api google-feed-api

我正在使用Google Feed JSAPI来阅读/解析Feed。问题是,当Feed更改时,之前的条目变为无效(链接和图像不起作用),因此我无法加载Feed的缓存版本。我认为加载Feed时可以选择不使用缓存版本,但我没有看到。我的解决方案是在变量(t)中添加到feed url的末尾,因此它是“唯一的”但这看起来很糟糕(但它确实有效)。谁知道有更好的方法呢?

    function onLoad() {
      // Create a feed instance that will grab feed feed.
      var feed = new google.feeds.Feed(feedLocation+"&t="+new Date().getTime());

      // Request the results in XML (so that we can parse out all the info
      feed.setResultFormat(google.feeds.Feed.XML_FORMAT);

      //must set this - if you don't, it defaults to 4
      feed.setNumEntries(50);

      // Calling load sends the request off.  It requires a callback function.
      feed.load(feedLoaded);
    }

2 个答案:

答案 0 :(得分:2)

这个答案可能会迟到,但我遇到了这篇文章并使用pxpgraphics评论得到了解决方案。对于那些需要使缓存无效的人来说,这样做。请注意,此代码示例从RSS提要中提取其他数据;根据您的需要进行修改。

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

function initialize() {
    var feedLocation = "http://feeds.bbci.co.uk/news/rss.xml";

    /*  Use the randomNum and the time to invalidate the Google cache and 
        fetch the latest articles.
    */
    var randomNum = Math.floor((Math.random() * 10000) + 1);
    var url = feedLocation + "?&t=" + new Date().getTime() + randomNum;

    var feed = new google.feeds.Feed(url);
    feed.setNumEntries(1000);
    feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feed");
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        var div = document.createElement("div");
        div.appendChild(document.createTextNode(entry.title));
        div.innerHTML += "<br>" + entry.publishedDate;
        div.innerHTML += "<br>" + entry.content;
        container.appendChild(div);
      }
    }
  });
}
google.setOnLoadCallback(initialize);

此代码假设您在页面上显示此DIV

<div id="feed"></div>

答案 1 :(得分:0)

尝试feed.includeHistoricalEntries();它可能会解决您的问题。

相关问题