tl; dr If you want to just see the answer, it is below the heading A Amended version
The second piece of code is a generator that gives messages one by one, so you should use it as part of something like a loop, and then do something with the output. Here is your code with some additional code that iterates over the generator and prints the data it returns.
import pytumblr def getAllPosts (client, blog): offset = 0 while True: posts = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True) if not posts: return for post in posts: yield post offset += 20
However, there are a couple of errors in this code. getAllPosts will not only output every post, it will also return other things, because it will iterate over the API response, as you can see from this example, I launched ipython in my shell.
In [7]: yielder = getAllPosts(client, 'staff') In [8]: next(yielder) Out[8]: 'blog' In [9]: next(yielder) Out[9]: 'posts' In [10]: next(yielder) Out[10]: 'total_posts' In [11]: next(yielder) Out[11]: 'supply_logging_positions' In [12]: next(yielder) Out[12]: 'blog' In [13]: next(yielder) Out[13]: 'posts' In [14]: next(yielder) Out[14]: 'total_posts'
This is because the posts object in getAllPosts is a dictionary that contains much more than just every post from the staff blog - it also has elements such as the number of blog posts, description of the blog when it was last updated, etc. . As-is code could potentially lead to an infinite loop, as the following condition:
if not posts: return
It will never be true due to the structure of the response, because the empty Tumblr API response from pytumblr looks like this:
{'blog': {'ask': False, 'ask_anon': False, 'ask_page_title': 'Ask me anything', 'can_send_fan_mail': False, 'can_subscribe': False, 'description': '', 'followed': False, 'is_adult': False, 'is_blocked_from_primary': False, 'is_nsfw': False, 'is_optout_ads': False, 'name': 'asdfasdf', 'posts': 0, 'reply_conditions': '3', 'share_likes': False, 'subscribed': False, 'title': 'Untitled', 'total_posts': 0, 'updated': 0, 'url': 'https://asdfasdf.tumblr.com/'}, 'posts': [], 'supply_logging_positions': [], 'total_posts': 0}
if not posts will be checked for this structure, and not for the posts field (this is an empty list here), so the condition will never end, because the answer dictionary is not empty (see Checking the value of truth in Python ).
Fixed version
Here's the code (mostly verified / verified) that captures the loop from your getAllPosts implementation and then uses this function to retrieve the messages and passes them to a file called (BLOG_NAME)-posts.txt .
import pytumblr def get_all_posts(client, blog): offset = 0 while True: response = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True)
It will be just a text dump of the responses of the API messages, so if you need to make it more attractive or something else you need.