2014-11-23 49 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

相關問題