2014-11-23 22 views
5

tôi đã script nhỏ này làm việc hoàn hảo cho các tháng trướcCách đăng hình ảnh lên Twitter bằng 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/") 

Nhưng bây giờ Twitter đã bị phản đối phương pháp này. Twython nói với tôi rằng tôi nên sử dụng Twython.upload_media, nhưng tôi không thể tìm thấy bất kỳ tài liệu nào về việc sử dụng nó. Ngay cả các trang web chính thức của Twython vẫn liệt kê một ví dụ với update_status_with_media.

Bất cứ ai cũng biết cách thực hiện hoặc tìm nơi để tìm một số ví dụ/thông tin?

Trả lời

5

Ok tôi đã có cùng một vấn đề và tôi đã sai lầm về nó và làm cho nó hoạt động.

Tôi đã đặt nó vào mã của bạn dưới đây (không kiểm tra nó mặc dù)

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

Cảm ơn! Cuối cùng, tôi đã chuyển sang gói và đi với tweepy. Đó là, theo ý kiến ​​của tôi, đơn giản hơn. Bạn có thể kiểm tra mã trên github: https://github.com/joaquinlpereyra/ImageTwitterBot/blob/master/ImageTwitterBot.py – joaquinlpereyra

+0

bạn có thể tìm tài liệu tại đây: https: //twython.readthedocs.org/en/latest/api. html – Txugo

1

Khi bạn làm twitter.update_status, là bắt buộc tình trạngmedia_ids

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

thêm 'status =' và 'media_ids =' làm việc cho tôi, cảm ơn! – rcpilotp51

Các vấn đề liên quan