简体   繁体   English

通过python将图片从目录发布到tumblr

[英]Post picture from directory to tumblr via python

I searched the boards and found a thread to which I used to make a script to upload a picture to tumblr. 我搜索了木板,找到了一个用来编写脚本以将图片上传到tumblr的线程。

from tumblr import Api
import sys

def antearaTumblr(blog, email, password):
    BLOG=blog
    USER=email
    PASSWORD=password
    api = Api(BLOG,USER,PASSWORD)
    #data = open('C:/Users/Kevin/Desktop/python-tumblr-0.1/1329360987775.jpg').read()
    api.write_photo('http://www.jonathanworthington.co.uk/wp-content/uploads/2008/07/etc.jpg')
    title = ''
    body = ''
    api.write_regular(title,body)

This does work, it uses write_photo to grab a photo from the internet and upload it to my tumblr. 确实有效,它使用write_photo从互联网上抓取一张照片并将其上传到我的tumblr。 However, I don't know how to make it grab a photo from a directory on my PC, like this.... 但是,我不知道如何使它像这样从我的PC上的目录中抓取照片。

from tumblr import Api
import sys

def antearaTumblr(blog, email, password):
    BLOG=blog
    USER=email
    PASSWORD=password
    api = Api(BLOG,USER,PASSWORD)
    data = open('C:/Users/Kevin/Desktop/python-tumblr-0.1/1329360987775.jpg').read()
    title = ''
    body = ''
    api.write_regular(title,data)

It succeeds in uploading, however all it uploads is this... ÿØÿà... which I assume is the jpg header. 它成功上传,但是它上传的只是这个......Øÿà...,我假设是jpg标头。

I found this topic Post picture to Tumblr using Python but I don't know exactly what the replies are saying. 我找到了使用Python将图片发布到Tumblr的主题但我不知道确切的答复是什么。

Does anyone know how to get it to work using a directory and not a link? 有谁知道如何使用目录而不是链接使其工作?

Also, i tried this https://gist.github.com/1242662 but honestly I just had no idea how to do it. 另外,我尝试了这个https://gist.github.com/1242662,但老实说,我只是不知道该怎么做。

Thanks for any help I receive. 感谢您收到的任何帮助。

Looking at the linked post it seems that data is a keyword argument, so you'd call api.write like this: 查看链接的帖子,似乎数据是关键字参数,因此您可以这样调用api.write

api.write_regular(title, data=data)

I have no idea if that will work or not, but tumblr's API seems well documented . 我不知道这是否行得通,但是tumblr的API似乎有据可查 I would recommend using requests instead, which is by far the nicest HTTP library for Python. 我建议改用requests ,这是迄今为止Python最好的HTTP库。 To upload a photo: 要上传照片:

import requests

url = 'https://www.tumblr.com/api/write'
data = {'email': your_email, 'password': your_password, 'type': 'photo'}
files = {'data': open('your_image.jpg')}

requests.post(url, data=data, files=files)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM