I’m trying to write a script that will post two images to Twitter using the API, any idea why this doesn’t work? It only posts the first image. New to this, thanks!
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
Answers
Use tweepy (much easier);
You are almost there. You just forgot to use your array of ids
x
. Make the following change to the last part of your code.