How to send image to twitter using Twython?

I had a small script working fine for the last month

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

But now Twitter has deprecated this method. Twython tells me that I should use Twython.upload_media, but I cannot find any document to use it. Even official Twython sites still provide an example with update_status_with_media.

Does anyone know how to do this or where to find some examples / information?

+5
source share
2 answers

Ok, I had the same problem and I messed it up and made it work.

I put it in my code below (not tested it though)

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

When you do twitter.update_status, be sure to status and media_ids

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

Source: https://habr.com/ru/post/1207540/


All Articles