Skip to content

Instantly share code, notes, and snippets.

@elidickinson
Created August 15, 2013 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elidickinson/6237607 to your computer and use it in GitHub Desktop.
Save elidickinson/6237607 to your computer and use it in GitHub Desktop.
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)
@agusmdev
Copy link

agusmdev commented Jan 9, 2019

Hello! You should use update_profile_banner instead of update_profile_background_image because the latter is deprecated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment