Tweepy update_profile_image:不是远程URL

时间:2014-07-06 01:20:48

标签: python google-app-engine python-2.7 tweepy

在Tweepy文档中,它说文件名不能是远程URL!:

API.update_profile_image(filename)
Update the authenticating user’s profile image. Valid formats: GIF, JPG, or PNG

Parameters: filename – local path to image file to upload. Not a remote URL!
Return type:    User object

我正在使用GAE并且它不允许上传图像并保存在服务器上,所以我总是要将图像文件复制并通过我的应用程序文件夹并更新应用程序。 这很痛苦。

有什么建议吗?

我的代码如下:

import import cloudstorage as gcs
gcs_f_photourl = gcs.open(photourl,'r')
image = StringIO(gcs_f_photourl)
api.update_profile_image('IMG_102433453.jpg',image)
gcs_f_photourl.close()

错误:

image = StringIO(gcs_f_photourl)

TypeError: must be convertible to a buffer, not ReadBuffer

请帮忙。

1 个答案:

答案 0 :(得分:2)

recent commit添加了对要传入的任意文件对象的支持。您可以将文件对象作为file_关键字参数传递,或者作为第二个位置参数传递。然后仍然需要文件名,但仅用于确定mimetype。

在GAE上,使用保存图像数据的StringIO对象:

from cStringIO import StringIO

image = StringIO(imagedata)
API.update_profile_image('profile.jpg', image)

你需要一个支持搜索的文件(类似)对象; urllib2返回的响应对象没有。您必须阅读所有数据,从中创建StringIO并将其传递给Tweepy方法。

此更改是Tweepy 2.3.0的一部分。