How to add multiple images to a tweet in python?

65 Views Asked by At

I can tweet adding one image using Twython like so:

    twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
    # twitter: upload media
    gif = open('ex.gif', 'rb')
    response = twitter.upload_media(media=gif)
    status = '"' + quote + '" ' + link + ' #sopranosgif'
    twitter.update_status(status=status, media_ids=[response['media_id']])

but could not find any way to use multiple images in one tweet. So I found a solution from 2015 with TwitterAPI Add multiple images to a tweet with python and twitterApi?

And tried that

    api = TwitterAPI(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) #(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_TOKEN_KEY,ACCESS_TOKEN_SECRET)
    data = ""
    for x in range(0, 4):
        file = open(filename, 'rb') 
        stream = file.read()
        data = data + stream

    r = api.request('media/upload', None, {'media': data})
    print('UPLOAD MEDIA SUCCESS' if r.status_code == 200 else 'UPLOAD MEDIA FAILURE')

    # STEP 2 - post tweet with a reference to uploaded image
    if r.status_code == 200:
        media_id = r.json()['media_id']
        r = api.request('statuses/update', {'status':TWEET_TEXT, 'media_ids':media_id})
        print('UPDATE STATUS SUCCESS' if r.status_code == 200 else 'UPDATE STATUS FAILURE')

Following the guide, but get a different error:

    data = data + stream
TypeError: unsupported operand type(s) for +: 'NoneType' and 'bytes'
0

There are 0 best solutions below