Skip to content

Instantly share code, notes, and snippets.

@cpu
Created October 10, 2011 18:00
Show Gist options
  • Save cpu/1276006 to your computer and use it in GitHub Desktop.
Save cpu/1276006 to your computer and use it in GitHub Desktop.
Python Twitter Avatar Downloader
###############################################################################
# Quick & Dirty script to download the Avatars of your Twitter Followers
# Daniel McCarney -- 10/10/11
#
# Emphasis on the quick & dirty. There's no error handling, and bad things
# are liable to happen if you have Twitter followers with malicious avatars
#
# Requirements: Python 2.6, python-twitter, OAuth keys*
#
# *Twitter requires OAuth authentication for the API calls this script uses.
# You'll need to set up an "App" at: https://dev.twitter.com/ and authorize
# it access to your account. Take the consumer_key, the consumer_secret, the
# access_token_key and the access_token_secret and use them to populate the
# settings dictionary.
###############################################################################
###############################################################################
## YOUR OAUTH SETTINGS GO HERE ##
###############################################################################
settings = {
'consumer_key' : 'REPLACE_ME_WITH_CONSUMER_KEY',
'consumer_secret' : 'REPLACE_ME_WITH_CONSUMER_SECRET',
'access_token_key' : 'REPLACE_ME_WITH_ACCESS_TOKEN_KEY',
'access_token_secret' : 'REPLACE_ME_WITH_TOKEN_SECRET',
}
###############################################################################
import sys, os.path, urllib2
try:
import twitter
except ImportError:
print "You must install the python-twitter library."
sys.exit(1)
api = twitter.Api(
consumer_key = settings['consumer_key'],
consumer_secret = settings['consumer_secret'],
access_token_key = settings['access_token_key'],
access_token_secret = settings['access_token_secret']
)
followers = api.GetFollowers()
imageFetcher = urllib2.build_opener()
for user in followers:
username = user.GetScreenName()
imageURL = user.GetProfileImageUrl()
filename = os.path.split(imageURL)[1]
extension = os.path.splitext(filename)[1]
imageURLConn = imageFetcher.open(imageURL)
avatarData = imageURLConn.read()
filename = "%s%s" %(username, extension)
avatarFile = open(filename, "wb")
avatarFile.write(avatarData)
avatarFile.close()
print "Username: %s File: %s" %(username, filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment