tumblr / jumblr

Tumblr API v2 Java Client
Apache License 2.0
278 stars 105 forks source link

Is it possible to get total_posts of post list? #58

Closed alphalin closed 9 years ago

alphalin commented 9 years ago

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.

KevinTCoughlin commented 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.

alphalin commented 9 years ago

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.

KevinTCoughlin commented 9 years ago

@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:

alphalin commented 9 years ago

@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 )

KevinTCoughlin commented 9 years ago

@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();
alphalin commented 9 years 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.

KevinTCoughlin commented 9 years ago

@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: