2014-11-23 11 views
5

ho avuto questo piccolo script perfettamente funzionante per il mese scorsoCome postare l'immagine su Twitter con 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/") 

Ma ora Twitter ha deprecato questo metodo. Twython mi dice che dovrei usare Twython.upload_media, ma non riesco a trovare alcun documento sul suo utilizzo. Anche i siti ufficiali di Twython presentano ancora un esempio con update_status_with_media.

Qualcuno sa come farlo o dove trovare alcuni esempi/informazioni?

risposta

5

Ok ho avuto lo stesso problema e mi sono incasinato e ho funzionato.

ho messo nella tua codice qui sotto (non testato, però)

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

Grazie! Alla fine, cambiai i wrapers e andai con i tweepy. È, a mio parere, più semplice. Puoi controllare il codice su github: https://github.com/joaquinlpereyra/ImageTwitterBot/blob/master/ImageTwitterBot.py – joaquinlpereyra

+0

puoi trovare la documentazione qui: https: //twython.readthedocs.org/en/latest/api. html – Txugo

1

Quando si esegue twitter.update_status, è obbligatoria stato e media_ids

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

aggiungendo 'status =' e 'media_ids =' ha funzionato per me, grazie! – rcpilotp51