Back

Step by Step Guide to Building a Tumblr API Integration in Python

Aug 2, 2024 • 4 minute read

Introduction

Hey there, fellow dev! Ready to dive into the world of Tumblr API integration? We're going to use PyTumblr to make this a breeze. Whether you're looking to automate your Tumblr posts or build a cool app, this guide has got you covered.

Prerequisites

Before we jump in, make sure you've got:

  • Python installed (3.6+ recommended)
  • PyTumblr package (pip install pytumblr)
  • Tumblr API credentials (grab these from the Tumblr API console)

Setting up the Tumblr Client

Let's get this party started:

import pytumblr # Replace with your actual credentials client = pytumblr.TumblrRestClient( 'consumer_key', 'consumer_secret', 'oauth_token', 'oauth_secret' )

Basic Operations

Now that we're set up, let's test the waters:

# Get blog info blog_info = client.blog_info('cooldeveloper.tumblr.com') # Fetch recent posts posts = client.posts('cooldeveloper.tumblr.com')

Creating and Managing Content

Time to make some noise on Tumblr:

# Create a text post client.create_text('cooldeveloper.tumblr.com', state='published', title='Hello World', body='My first post via API!') # Upload and post an image client.create_photo('cooldeveloper.tumblr.com', state='published', data='path/to/image.jpg') # Reblog a post client.reblog('cooldeveloper.tumblr.com', id='post_id', reblog_key='reblog_key')

Advanced Features

Let's kick it up a notch:

# Search for content results = client.tagged('python') # Get followers followers = client.followers('cooldeveloper.tumblr.com') # Handle pagination offset = 0 while True: posts = client.posts('cooldeveloper.tumblr.com', offset=offset) if not posts['posts']: break # Process posts offset += 20

Error Handling and Best Practices

Remember, things don't always go smoothly. Wrap your API calls in try-except blocks and respect rate limits. Tumblr's pretty chill, but don't push it!

try: client.create_text('cooldeveloper.tumblr.com', state='published', title='Oops', body='Did I break something?') except pytumblr.TumblrRestClient.TumblrError as e: print(f"Whoops! {e}")

Example Project: Building a Tumblr Content Aggregator

Let's put it all together with a simple content aggregator:

def aggregate_content(tag, limit=10): posts = client.tagged(tag, limit=limit) for post in posts: print(f"Post type: {post['type']}") print(f"Blog name: {post['blog_name']}") print(f"Post URL: {post['post_url']}") print("---") aggregate_content('python', limit=5)

Conclusion

And there you have it! You're now equipped to build some awesome Tumblr integrations. Remember, the Tumblr API is your playground – experiment, create, and most importantly, have fun!

For more in-depth info, check out the PyTumblr docs and Tumblr API docs. Now go forth and code something amazing!