如何使用Twython将图像发布到Twitter?

时间:2014-11-23 21:03:25

标签: python twitter twython

上个月我有一个小脚本完美运作

from twython import Twython
import glob
import random

app_key = "XXX"
app_secret = "XXX"
oauth_token = "XXX"
oauth_token_secret = "XXX"
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)

    def RandomImageTwitt(folder):
        #Takes the folder where your images are as the input
        images = glob.glob(folder + "*")
        image_open = open(images[random.randint(0,len(images))-1])
        twitter.update_status_with_media(media=image_open)

RandomImageTwitt("/home/XXX/.reddit-twitter-image/XXX/")

但是现在Twitter已经弃用了这种方法。 Twython告诉我应该使用Twython.upload_media,但我找不到任何关于它的使用的文档。甚至Twython官方网站仍然列出了一个update_status_with_media的例子。

任何人都知道该怎么做或在哪里找到一些例子/信息?

2 个答案:

答案 0 :(得分:5)

好的,我有同样的问题,我搞砸了它,并让它工作。

我已将它放入下面的代码中(尽管未经过测试)

from twython import Twython
import glob
import random

app_key = "XXX"
app_secret = "XXX"
oauth_token = "XXX"
oauth_token_secret = "XXX"
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)

    def RandomImageTwitt(folder):
        #Takes the folder where your images are as the input
        images = glob.glob(folder + "*")
        image_open = open(images[random.randint(0,len(images))-1])
        #new code starts here 
        image_ids = twitter.upload_media(media=image_open)
        twitter.update_status('hello this is a status',image_ids['media_id'])


RandomImageTwitt("/home/XXX/.reddit-twitter-image/XXX/")

答案 1 :(得分:1)

当您执行twitter.update_status时,必须状态 media_ids

twitter.update_status(status='hello this is a status', media_ids=image_ids['media_id'])