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

joa*_*yra 5 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/")
Run Code Online (Sandbox Code Playgroud)

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

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

duc*_*ive 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/")
Run Code Online (Sandbox Code Playgroud)