HankSmackHood
HankSmackHood

Reputation: 4873

Post picture to Tumblr using Python

I'm trying to post a picture to tumblr, using python, in particular: http://code.google.com/p/python-tumblr/

#!/usr/bin/python

from tumblr import Api
import sys

BLOG='example.tumblr.com'
USER='[email protected]'
PASSWORD='example'
api = Api(BLOG,USER,PASSWORD)
post_data = "picture.png"   
title = "Title of my entry"
body = "this is a story with a picture"

api.write_regular(title, body + post_data)

When I run this the result is that the blog arrives, but instead of:

Title of my entry

this is a story with a picture

[img]

I get this:

Title of my entry

this is a story with a picturepicture.png

Upvotes: 3

Views: 4502

Answers (3)

velocityzen
velocityzen

Reputation: 86

I made an example here with API v2 https://gist.github.com/1242662

Upvotes: 1

karlcow
karlcow

Reputation: 6972

In your current code, you are not posting an image but you are sending a string which is called "picture.png". As Daniel DiPaolo said you have to use write a photo. The Argument for write_photo is the link to the image, for example.

#!/usr/bin/python
from tumblr import Api
import sys

BLOG='example.tumblr.com'
USER='[email protected]'
PASSWORD='example'
api = Api(BLOG,USER,PASSWORD)
api.write_photo('http://example.org/somewhere/lolcat.jpg')

If you want to send HTML, you can create a body which is long containing the tags of your choices.

title = "life is amazing" 
body = """
_here my html code_
"""

Then write it with the API

api.write_regular(title,body)

and you should be all set.

data upload

to be more precise ;) in the case you want to send data you have to open the object. Let's say your image is "lolcat.jpg"

data = open('lolcat.jpg').read()

Upvotes: 2

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56390

You aren't actually sending the image data, you're just sending a string with the filename in it, so that's not too surprising. The write_regular call allows HTML so if you can upload the photo somewhere, you should be able to use an <img src="..." /> tag in your post text to have the image displayed within your post.

Or you can use the write_photo call to upload the photo (and not just the filename!) to Tumblr and then somehow get the URL to that and use that in your <img> tag for your post.

Upvotes: 2

Related Questions