2014-11-23 50 views
5

我有这个小脚本上个月如何使用Twython将图像发布到Twitter?

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的例子。

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

回答

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/") 
+0

谢谢!最后,我换了包装纸,然后用tweepy去了。在我看来,它更简单。你可以在github上查看代码:https://github.com/joaquinlpereyra/ImageTwitterBot/blob/master/ImageTwitterBot.py – joaquinlpereyra 2014-12-03 15:15:55

+0

你可以在这里找到文档:https://twython.readthedocs.org/en/latest/api。 html – Txugo 2014-12-13 00:40:14

1

当你这样做twitter.update_status,是强制性状态media_ids

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

加上'status ='和'media_ids ='为我工作,谢谢! – rcpilotp51 2015-10-14 16:29:51

相关问题