search posts with blogger api v3 in specific date range

1.2k views Asked by At

If we want to search google blogger posts with blogger APIs v3, then we should follow the documentation in here. But how can we integrate a date range in query parameter q? i tried q=startDate:2016-01-01:T00:00:00+endDate:2017-09-05:T00:00:00 but it doesn't work.

I also tried to search posts from a certain date and after by using q=startDate:2016-01-01:T00:00:00 or q=startDate:"2016-01-01:T00:00:00" or q=startDate:2016-01-01 but still doesn't work. The URL encoding is done properly because I tested with labels search like q=label:symbols|label:fonts for searching posts which contain either the label symbols or the label fonts and it works just fine.

1

There are 1 answers

0
maclaud On

After a bit of search i finally find out that the answer was there but i was not looking carefully. When we want to search posts in a specific date range, we do not query the search:posts operation but the list:posts operation. For example if we want to search from October 2, 2016 to September 7, 2017 we send a request url of the following form:

https://www.googleapis.com/blogger/v3/blogs/{blogID}/posts? startDate=2016-10-02T00:00:00z&endDate=2017-09-07T00:00:00z &callback=handleResponse&key={our_received_key}

handleResponse is the callback function which is called when the response is received successfully. The above url query is ineffective because we receive also the body text of the post, which can impact performance. Also pagination must be used. The first is achieved by setting the fetchBodies=false and the second is achieved by using maxResults and pageToken.

A simplified script which encapsulates the above comments can be:

<!DOCTYPE html>
<html>
<head>
    <title>Blogger API Example</title>
</head>
<body>
    <div id="content"></div>
    <script>
        function handleResponse(response) {
            var post = "";
            for (var i in response.items)
            {
                document.getElementById("content").innerHTML += "<a href=\"" + response.items[i].url +
                    "\">" + response.items[i].title + "</a>" + "<br/>";
            }
      }
    </script>
    <script src='https://www.googleapis.com/blogger/v3/blogs/35636577512/posts?startDate=2016-10-02T00:00:00z&endDate=2017-09-07T00:00:00z&maxResults=10&fetchBodies=false&callback=handleResponse&key=RTza3456gfhf_Q345fgh'></script>
</body>
</html>

if the total number of posts is greater than the maxResults which is 10 in the example, then a nextPageToken will be sent which can be retrieved by response.nextPageToken and stored in the next url query.

https://www.googleapis.com/blogger/v3/blogs/35636577512/posts?startDate=2008-10-02T00:00:00z&endDate=2016-09-06T17:30:00z&pageToken=dfgdfRtdfdf234rT&fetchBodies=false&callback=handleResponse&key=RTza3456gfhf_Q345fgh'