Create a gist now

Instantly share code, notes, and snippets.

Update twitter profile background image
import tweepy
import tempfile
import urllib
from time import sleep
import os
url = 'http://wwc.instacam.com/instacamimg/KDCA/KDCA_l.jpg'
# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
auth = tweepy.OAuthHandler(os.environ.get('CONSUMER_KEY'), os.environ.get('CONSUMER_SECRET'))
# The access tokens can be found on your applications's Details
# page located at https://dev.twitter.com/apps (located
# under "Your access token")
auth.set_access_token(os.environ.get('ACCESS_TOKEN'), os.environ.get('ACCESS_SECRET'))
api = tweepy.API(auth)
try:
(filename, headers) = urllib.urlretrieve(url)
for attempt in range(10):
try:
# The update profile API randomly throws error 131 like half the time
api.update_profile_background_image(filename, use=1, tile=0)
except tweepy.error.TweepError as err:
if err[0][0]['code'] == 131:
print "attempt %d, resulted in %s" % (attempt, err)
sleep(1)
continue
else:
raise
break
finally:
os.remove(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment