Skip to content

Instantly share code, notes, and snippets.

@tompreston
Last active March 6, 2019 01:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tompreston/425a8b05a47da13f9f63 to your computer and use it in GitHub Desktop.
Save tompreston/425a8b05a47da13f9f63 to your computer and use it in GitHub Desktop.
Upload media with Python Twitter Tools
#!/usr/bin/env python3
from twitter import Twitter, OAuth
from twitter_auth import (CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN,
ACCESS_TOKEN_SECRET)
if __name__ == '__main__':
my_auth = OAuth(token=ACCESS_TOKEN,
token_secret=ACCESS_TOKEN_SECRET,
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET)
t = Twitter(auth=my_auth)
# Send images along with your tweets:
# - first just read images from the web or from files the regular way:
with open("catsonsynths.jpg", "rb") as imagefile:
imagedata = imagefile.read()
# - then upload medias one by one on Twitter's dedicated server
# and collect each one's id:
t_up = Twitter(domain='upload.twitter.com', auth=my_auth)
id_img = t_up.media.upload(media=imagedata)["media_id_string"]
t.statuses.update(status="Testing, testing. Uploading media with "
"Python Twitter Tools, 2, 3, 4.",
media_ids=",".join([id_img]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment