Closed alphalin closed 9 years ago
Hey @alphalin, there is a method on the blog object that returns the total number of posts called getPostCount()
. But for your use case I think you need to pass the offset as a query string parameter. You'll make the initial call and then use the returned size of the collection as the offset for subsequent requests. I'll work on providing a code snippet.
Hi @KevinTCoughlin, I know the getPostCount() can get total posts of a blog but I can't find any input parameters I can use for my query in this method from document. It is great if you can provide a code snippet.
@alphalin something like this should work, but I would be a good citizen with its implementation. It can result in a large number of requests to the API depending on the blog's post count.
final Blog blog = client.blogInfo("your-blog.tumblr.com");
final int postCount = blog.getPostCount();
final List<Post> posts = new ArrayList<Post>();
int offset = 0;
while (offset < postCount) {
final Map<String, Object> params = new HashMap<String, Object>();
params.put("offset", offset);
final List<Post> newPosts = client.blogPosts("your-blog.tumblr.com", params);
posts.addAll(newPosts);
offset += newPosts.size();
}
The implementation is as follows:
0
, so I start at the most recent post20
offset
offset <= postCount
signaling that I've fetched all posts.@KevinTCoughlin thanks for the code snippet. as you said, it requests API many times inefficiently. is it possible to get total_posts directly from response rather than counting it one by one? Tumblr did provide total_posts in the API response. (refer to https://www.tumblr.com/docs/en/api/v2#posts )
@alphalin, the following returns total post count for a given blog.
final Blog blog = client.blogInfo("your-blog.tumblr.com");
final int postCount = blog.getPostCount();
@KevinTCoughlin blog.getPostCount()
is total posts of a blog, not total posts for specific query (e.g., tag=travel). you can see my first comment for example.
@alphalin commented 2 hours ago @KevinTCoughlin blog.getPostCount() is total posts of a blog, not total posts for specific query (e.g., tag=travel). you can see my first comment for example.
Not currently, you'll have to do something similar to what I had above:
List<Post>
or increase a counter by the number returned.20
you have the total count of posts for that tag.
Hi, I can't find a method to get total_posts. For example, I have a blog has 37 posts in total. 13 posts have tag "travel". I use blogPosts(String blogName, Map<String,?> options) to get posts with tag "travel" and the limit sets to 5. I can count List to get 5, but can't get the total_posts which should be 13.
Please help to make an enhancement.
Thanks.