Python推特多个图片Twitter API

时间:2016-09-06 13:48:12

标签: python twitter

我正在尝试编写一个脚本,将使用API​​将两个图像发布到Twitter,任何想法为什么这不起作用?它只发布第一张图片。新的,谢谢!

from TwitterAPI import TwitterAPI
import urllib

api = TwitterAPI('','','','')

x = []

file = open('im1.png', 'rb')
data = file.read()
r = api.request('media/upload', None, {'media': data})
media_id = r.json()['media_id']
print('UPLOAD MEDIA SUCCESS' if r.status_code == 200 else 'UPLOAD MEDIA FAILURE')
x.append(str(media_id))


file = open('im2.png', 'rb')
data1 = file.read()
r = api.request('media/upload', None, {'media': data1})
media_id = r.json()['media_id']
print('UPLOAD MEDIA SUCCESS' if r.status_code == 200 else 'UPLOAD MEDIA FAILURE')
x.append(str(media_id))


if r.status_code == 200:
    media_id = r.json()['media_id']
    r = api.request('statuses/update', {'status':'Test', 'media_ids':media_id})
    print('UPDATE STATUS SUCCESS' if r.status_code == 200 else 'UPDATE STATUS FAILURE')

2 个答案:

答案 0 :(得分:0)

使用tweepy(更容易);

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

Im1 = urllib.urlretrieve('http://www.meteociel.fr/cartes_obs/temp_uk.png','im1.png')

images = ('im1.png', 'im1.png')
media_ids = [api.media_upload(i).media_id_string for i in images]
api.update_status(status='msg', media_ids=media_ids)

答案 1 :(得分:0)

你快到了。您只是忘了使用您的ID数组x。对代码的最后部分进行以下更改。

if r.status_code == 200:
    media_id = ','.join(x)
    r = api.request('statuses/update', {'status':'Test', 'media_ids':media_id})
    print('UPDATE STATUS SUCCESS' if r.status_code == 200 else 'UPDATE STATUS FAILURE')