Blogger Javascript with JSON error on Posts > 500

前端 未结 4 1915
情歌与酒
情歌与酒 2021-01-03 11:52

I would like to show random posts to my blogger.

I got a javascript from googling and tried it, but the number of random posts are not correct (should be 10 but some

相关标签:
4条回答
  • 2021-01-03 12:01

    While you set the max-results to 1000, the entries returned from the server will be capped to 500. It will still return the right total number of entries, however, as you can see if you access json.feed.openSearch$totalResults.$t.

    Source

    0 讨论(0)
  • 2021-01-03 12:02

    So it seems that the feed entry at the randomized index is null, in that case add another condition to ensure that every entry in randomized indexes is not null.

    Replace:

    if(flag==0&&l!=0){
    

    with:

    if(flag==0&&l!=0&&json.feed.entry[l-1]!=null){
      randarray[i++]=l;
    }
    

    I hope that solves your problem.

    Regards,

    PP

    EDIT: Then you can call the feed url twice, this is the quick and dirty way to achieve so:

    <script type="text/javascript">
    var a=0;
    var b=0;
    var entries = new Array();
    function randomposts(json) {
      for (var i in json.feed.entry) {
        var entry = json.feed.entry[i];
        if (entry != null) {
            entries[b++] = entry;
        }
      }
      a++;
      if (a < 2) return;
      var randarray = new Array();
      var l=0;
      var flag;
      var numofpost=10;
    
      var total = entries.length;
    
      for(var i=0;i < numofpost;) {
        flag=0;
        randarray.length=numofpost;
        l=Math.floor(Math.random()*total);
        for(j in randarray){
          if(l==randarray[j]){
            flag=1;}
        }
        if(flag==0&&l!=0){
          randarray[i++]=l;
          //alert(l);
        }
      }
      // correct output
      // alert(randarray);
    
      document.write('<ul>');
    
      // dummy for testing 500 limit 
      //for (var x = 0; x < numofpost; x++) {
      //  randarray[x]= 495 + x;
      //}
    
      for(var n in randarray){
        var p=randarray[n];
        var entry=entries[p-1];
        var posttitle = entry.title.$t;
        for(var k=0; k < entry.link.length; k++){
          if(entry.link[k].rel=='alternate'){
            document.write('<li> ' + posttitle.link(entry.link[k].href) + '</li>');
    
          }
        }
      }
      document.write('</ul>');
    }
    </script>
    
    <script src="/feeds/posts/default?alt=json-in-script&start-index=1&max-results=500&callback=randomposts" type="text/javascript"></script>
    <script src="/feeds/posts/default?alt=json-in-script&start-index=501&max-results=500&callback=randomposts" type="text/javascript"></script>
    
    0 讨论(0)
  • 2021-01-03 12:06

    I recommend using Google JavaScript Client Library - Blogger API to retrieve posts of a blog.

    See the following example:

      <script>
      function renderResults(response) {
        if (response.items) {
          for (var i = 0; i < response.items.length; i++) {
            //do whatever you want with the posts of your blog
          }      
        }
        if(response.nextPageToken) {
          var blogId = 'XXX Your blogId XXX';
          var request = gapi.client.blogger.posts.list({
            'blogId': blogId,
            'pageToken': response.nextPageToken,
            'maxResults': 100,
          });
          request.execute(renderResults);
        }
      }
      function init() {
        gapi.client.setApiKey('XXX Get your API Key from https://code.google.com/apis/console XXX');
        gapi.client.load('blogger', 'v3', function() {
            var blogId = 'XXX Your blogId XXX';
            var request = gapi.client.blogger.posts.list({
              'blogId': blogId,
              'maxResults': 100,
            });
            request.execute(renderResults);        
        });
      }
      </script>
      <script src="https://apis.google.com/js/client.js?onload=init"></script>
    
    0 讨论(0)
  • 2021-01-03 12:21

    Blogger capped maximum number of entries per request to 500, even though it's not documented it's evidently so. If you want to get all your entries you have to re-build your feed chunk by chunk setting start-index property: /feeds/posts/default?start-index=501. Here you may find a bit more information: http://too-clever-by-half.blogspot.ru/2011/12/blog-feed-500-post-limit-for-more-than.html

    0 讨论(0)
提交回复
热议问题