Python Forum
Print more than 20 posts with Python Tumblr API
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print more than 20 posts with Python Tumblr API
#1


Good afternoon,

I'm very new to Python, but I'm trying to write a code which will allow me to download all of the posts (including the "notes") from a specified Tumblr account to my computer.

Given my inexperience with coding, I was trying to find a pre-made script which would allow me to do this. I found several brilliant scripts on GitHub, but none of them actually return the notes from Tumblr posts (as far as I can see, although please do correct me if anyone knows of one that does!).

Therefore, I tried to write my own script. I've had some success with the code below. It prints the most recent 20 posts from the given Tumblr (albeit in a rather ugly format -- essentially hundreds of lines of texts all printed into one line of a notepad file):

import pytumblr

# Authenticate via API Key
client = pytumblr.TumblrRestClient('myapikey')


# Make the request
client.posts('staff', limit=2000, offset=0, reblog_info=True, notes_info=True, filter='html')
#print out into a .txt file
with open('out.txt', 'w') as f:
	print >> f, client.posts('staff', limit=2000, offset=0, reblog_info=True, notes_info=True, filter='html')
However, I want the script to continuously print posts until it reaches the end of the specified blog, rather than stopping after the first 20 posts as I see here. I searched the internet and found a very similar question on StackOverflow (https://stackoverflow.com/questions/2168...h-pytumblr. Apparently, it is built into the Tumblr API that only the first 20 posts be returned, but it is possible to work around this by manipulating the "offset" after every yeild. The responder on that thread recommended that the following code be implemented:

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
I tried to work this into my own script in the following way:

import pytumblr

# Authenticate via API Key
client = pytumblr.TumblrRestClient('myapikey')

#parts I added based on the StackExchange post
blog =('staff')
def getAllPosts (client, blog):
    offset = 0
    while True:
        posts = client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True, filter='html')
        if not posts:
            return

        for post in posts:
            yield post
	

        offset += 20
		
		#my attempt at then printing this data out
with open('out.txt', 'w') as f:
    print >> f, client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True, filter='html')
However, when I actually run this script, the following error message is displayed on my command line

Error:
Traceback (most recent call last): File "C:\Users\izzy\test\pythontumblr1.py", line 21, in <module> print >> f, client.posts(blog, limit=20, offset=offset, reblog_info=True, notes_info=True, filter='html') NameError: name 'offset' is not defined
I am almost certain that I am doing something very obviously wrong ("a schoolboy mistake", as it were), but I have no idea how to fix this.

Any help would be very much appreciated!

For clarity's sake, I should note that I posted this question (with an earlier version of my script) to StackOverflow a day ago (link to my post, but I haven't had any replies yet.

Oh Gosh, it seems that I was rather hasty in crossposting and some very kind soul over at stackoverflow has talked me through my coding problems. I will link to their solution in case it is of use to anyone else: https://stackoverflow.com/questions/4731...tumblr-api. Thanks!
Reply
#2
looks like someone just responded. Also i removed you API key. You shouldnt post it on public forums.
Recommended Tutorials:
Reply
#3
(Nov-16-2017, 01:11 PM)metulburr Wrote: looks like someone just responded. Also i removed you API key. You shouldnt post it on public forums.

Ah, thank you! I wasn't going to, but I didn't want to cause annoyance by not having my script be reproducible! Thanks again!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020