Post multiple Twitter images with python

1.First of all

I wanted to post multiple images, but I was a little addicted to it, so I will post it. It is supposed to work with python 3 series

2. Posting an image and its arguments

Posting one image has many other articles and can be easily implemented. Reference: Tweet with image in Python Let's use the source code described here.

3. Post multiple photos

Use media / upload for multiple posts.

(get media / upload-> media_id) * Number of sheets-> statuses / update

Although it is media_id, let's insert a comma-separated character string in multiple posts. I borrowed the above source code and rewrote it as follows.

media.py



def tweet_with_image(oauth_sess, tweet_text, path_list_images):
    
    url_media = "https://upload.twitter.com/1.1/media/upload.json"
    url_text = "https://api.twitter.com/1.1/statuses/update.json"
    
    media_ids = ""
    
    #Loop for the number of images
    for path in path_list_images:
        files = {"media" : open(path, 'rb')}
        req_media = oauth_sess.post(url_media, files = files)

        #Check the response
        if req_media.status_code != 200:
            print ("Image update failed: {}".format(req_media.text))
            return -1
        
        media_id = json.loads(req_media.text)['media_id']
        media_id_string = json.loads(req_media.text)['media_id_string']
        print ("Media ID: {} ".format(media_id))
        #Comma the media ID string","Combine with
        if media_ids == "":
            media_ids += media_id_string
        else:
            media_ids = media_ids + "," + media_id_string
    
    print ("media_ids: ", media_ids)
    params = {'status': tweet_text, "media_ids": [media_ids]}
    req_text = oauth_sess.post(url_text, params = params)

    #Check the response again
    if req_text.status_code != 200:
        print ("Text update failed: {}".format(req_text.text))
        return -1
    
    print ("tweet uploaded\n")
    return 1

Recommended Posts

Post multiple Twitter images with python
Animate multiple still images with Python
Easily post to twitter with Python 3
Post youtube soaring title to twitter with python3
Twitter graphing memo with Python
POST json with Python3 script
Post to Twitter using Python
Search twitter tweets with python
Number recognition in images with Python
[Lambda] [Python] Post to Twitter from Lambda!
[Python] POST wav files with requests [POST]
Quine Post with Qiita API (Python)
Post images from Python to Tumblr
Load gif images with Python + OpenCV
[Python] Collect images easily with icrawler!
Multiple integrals with Python and Sympy
[Python] Creating multiple windows with Tkinter
Working with DICOM images in Python
[Memo] Tweet on twitter with python
Collecting information from Twitter with Python (Twitter API)
Capturing images with Pupil, python and OpenCV
Tweet from python with Twitter Developer + Tweepy
Post Test 3 (Working with PosgreSQL in Python)
Post from another account with Twitter API
Process multiple lists with for in Python
[python] Get Twitter timeline for multiple users
[python, openCV] base64 Face recognition with images
[Python] Read images with OpenCV (for beginners)
Question: Multiple integrals with python don't work
Add Gaussian noise to images with python2.7
Importing and exporting GeoTiff images with Python
Let's make a Twitter Bot with Python!
Manage multiple Python versions with update-alternatives (Ubuntu)
Read text in images with python OCR
Upload images to Google Drive with Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
twitter on python3
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Collecting information from Twitter with Python (Environment construction)
Make a Twitter trend bot with heroku + Python
Generating solid color images with python | Kaggle icon
Convert PDFs to images in bulk with Python
I want to display multiple images with matplotlib.
[Python] How to draw multiple graphs with Matplotlib
Manage Python multiple version environment with Pythonz, virtualenv
[Python] Takes representative values ​​of multiple images [Numpy]