How to post image to twitter with Twython?
Asked Answered
N

2

5

I had this small script working perfectly 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 I should use Twython.upload_media, but I can't find any doc on its use. Even Twython official sites still lists an example with update_status_with_media.

Anyone knows how to do it or where to find some examples / info?

Nonpayment answered 23/11, 2014 at 21:3 Comment(0)
C
5

Ok i had the same problem and I messed about with it and got it working.

I have put it into your 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/")
Crandall answered 2/12, 2014 at 10:21 Comment(2)
Thanks! In the end, I switched wrapers and went with tweepy. It is, in my opinion, simpler. You can check the code on github: github.com/joaquinlpereyra/ImageTwitterBot/blob/master/…Nonpayment
you can find documentation here:twython.readthedocs.org/en/latest/api.htmlSacrilege
D
2

When you do twitter.update_status, is mandatory status and media_ids

twitter.update_status(status='hello this is a status', media_ids=image_ids['media_id'])
Diddle answered 11/2, 2015 at 23:14 Comment(1)
adding the status= and media_ids= worked for me, thanks!Avantgarde

© 2022 - 2024 — McMap. All rights reserved.