Friday, March 20, 2015

How to fix blogger's atom 500 max results if you use its feed output for some coding

this is for you if
  1. you use blogger
  2. you use blogger atom feed output for coding, either json or json-in-script
  3. you probably have had codes (with javascript) assuming blogger atom maximum output is unlimited
i googled this when i had a similar problem on my other blogger site Lyricsbible.net . All the results say i should use:
http://www.lyricsbible.net/atom.xml?redirect=false&start-index=1&max-results=500
http://www.lyricsbible.net/atom.xml?redirect=false&start-index=501&max-results=500


But, i already have some codes assuming i can specify my max-results value to be anything. But no! Even if you set max-results=2000, the feed output will stop at 500.
Meanwhile if you do not set any value for max-result, it's set at 25 by default.

So, in case you already wrote codes like me, this simple fix will help you out without having to alter those codes. If you have not, you can learn here.

  1. Go To Edit Templates, and click Edit HTML. {you see templatehtml}
  2. First to a backup of your code, just in case something goes wrong, so you can restore the previous codes. Copy all the HTML into a text editor.
  3. Now place the following code just before the </head>
<script type='text/javascript'>
//<![CDATA[
var entry=[], setJsonCount=0; //set as global
  function setJson(input){//populate the feeds once and for all as global
    setJsonCount++;
    try{entry = entry.concat(input.feed.entry);
    if(input.feed.entry.length==500){
    var factor = setJsonCount*500 + 1 //e.g. 1*500+1=501, 2*500+1=1001 etc
        var str = '<script src="/feeds/posts/default?max-results=500&start-index='+factor+'&alt=json-in-script&callback=setJson"><\/script>';
        document.write(str)
}//end if
       }catch(e){}
}
//]]>
</script>
<script src='/feeds/posts/default?max-results=2000&amp;alt=json-in-script&amp;callback=setJson'/> 
Trace all the codes where you have written something like this: json.feed.entry or input.feed.entry or something-something.feed.entry. Remove the 'something-something' or 'json' or 'input' part, and also the '.feed.'

Also remove all variable definitions of entry. That is,
var entry = ...
Now your code should cope with over 500 posts without  crashing.
Good Luck!

 


No comments:

Post a Comment