Search code examples
pythontumblr

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='example@example.com'
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


Solution

  • 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='example@example.com'
    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()