Tweet with image in Python

I wrote earlier "Access Twitter API with Python", but around March 2014, it became possible to post multiple images, and along with that, around image tweets It seems that the specifications have changed, so I added it.

Until then, the statuses / update API for regular tweets and the [statuses / update_with_media](https: // dev) for tweets with images. .twitter.com/rest/reference/post/statuses/update_with_media), but there is a new media / upload It seems that it has been added and the previous update_with_media has been deprecated.

In the old API, the image and text were posted at the same time, but in the new API, it seems that the image is uploaded first, the media ID is obtained, and the text is tweeted.

The code looks like this. Replace CK, CS, AT, AS with your own key as appropriate. Python2, 3 works with either.

upload_media.py


#!/usr/bin/env python
# coding: utf-8

import json
from requests_oauthlib import OAuth1Session

CK = 'XXXXXXXXXXXXXXXXXXXXXX'                             # Consumer Key
CS = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'         # Consumer Secret
AT = 'XXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # Access Token
AS = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'         # Accesss Token Secert

url_media = "https://upload.twitter.com/1.1/media/upload.json"
url_text = "https://api.twitter.com/1.1/statuses/update.json"

#Start an OAuth authentication session
twitter = OAuth1Session(CK, CS, AT, AS)

#Image posting
files = {"media" : open('image.jpg', 'rb')}
req_media = twitter.post(url_media, files = files)

#Check the response
if req_media.status_code != 200:
	print ("Image update failed: %s", req_media.text)
	exit()

#Get Media ID
media_id = json.loads(req_media.text)['media_id']
print ("Media ID: %d" % media_id)

#Post text with Media ID
params = {'status': 'Image posting test', "media_ids": [media_id]}
req_media = twitter.post(url_text, params = params)

#Check the response again
if req_media.status_code != 200:
	print ("Text update failed: %s", req_text.text)
	exit()

print ("OK")

If you tweet with 4 images, do media / upload 4 times and post the text with all media ids in a list. I haven't tried it.

Recommended Posts

Tweet with image in Python
Image format in Python
Image processing with Python
Easy image processing in Python with Pillow
Create an image with characters in python (Japanese)
Working with LibreOffice in Python
Scraping with chromedriver in python
Image editing with python OpenCV
Working with sounds in Python
Scraping with Selenium in Python
Sorting image files with Python (3)
Scraping with Tor in Python
Sorting image files with Python
Combined with permutations in Python
Image processing with Python (Part 3)
Image Processing Collection in Python
[Python] Image processing with scikit-image
Convert the image in .zip to PDF with Python
Cut out an image with python
Testing with random numbers in Python
[Python] Using OpenCV with Python (Image Filtering)
Implemented image segmentation in python (Union-Find)
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
[Python] Using OpenCV with Python (Image transformation)
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Image processing with Python 100 knocks # 3 Binarization
Use rospy with virtualenv in Python3
Let's do image scraping with Python
Use Python in pyenv with NeoVim
I liked the tweet with python. ..
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Image processing with Python 100 knocks # 2 Grayscale
Until dealing with python in Atom
Get started with Python in Blender
Send image with python, save with php
Working with DICOM images in Python
[Memo] Tweet on twitter with python
Gradation image generation with Python [1] | np.linspace
Exploring image filter parameters with blackbox optimization in Python Note
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
Quadtree in Python --2
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Python in optimization
CURL in python
FizzBuzz with Python3
Metaprogramming in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Scraping with Python
Try logging in to qiita with Python