Skip to content

Instantly share code, notes, and snippets.

@cjwinchester
Last active November 30, 2015 22:21
Show Gist options
  • Save cjwinchester/6ba45bddbf7e377a434f to your computer and use it in GitHub Desktop.
Save cjwinchester/6ba45bddbf7e377a434f to your computer and use it in GitHub Desktop.
Pythonically tweet an image (fetched from a URL) with your text.
from twitter import *
import requests
"Reference: https://pypi.python.org/pypi/twitter"
t = Twitter(auth=OAuth(
token,
token_secret,
consumer_key,
consumer_secret
))
text = "Hello, here is a gif of John Ritter."
pic = "https://media.giphy.com/media/yGlu7x5jfWGZi/giphy.gif"
r = requests.get(pic, stream=True)
if r.status_code == 200:
r.raw.decode_content = True
imagedata = r.raw.read()
t_up = Twitter(domain='upload.twitter.com', auth=OAuth(
token,
token_secret,
consumer_key,
consumer_secret
))
id_img = t_up.media.upload(media=imagedata)["media_id_string"]
t.statuses.update(status=text, media_ids=id_img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment