Post picture to Tumblr using Python

久未见 提交于 2019-12-07 15:37:44

问题


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


回答1:


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()




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/4938203/post-picture-to-tumblr-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!