Search code examples
pythontumblr

Getting only 20 posts returned through PyTumblr


I am using PyTumblr to return all of my posts, but it is only returning 20. I found the kwarg for the posts function, called limit, but when I specified 1000 it still returned 20. Any idea what I'm doing wrong?

CLIENT = pt.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET)
all_posts = CLIENT.posts(BLOG_URL, limit=1000)

Solution

  • Tumblr’s API only allows to specify a limit up to 20. So your limit of 1000 is being ignored and you get 20 instead. You will have to use paging in combination with the offset parameter instead.

    You could write yourself some generator which—similar to infinite scrolling—requests the next page as long as you keep requesting more posts from it:

    def getAllPosts (client, blog):
        offset = 0
        while True:
            posts = client.posts(blog, limit=20, offset=offset)
            if not posts:
                return
    
            for post in posts:
                yield post
    
            offset += 20