From 4d524fb654aec3d5ae186d49b962d681a4e7ea21 Mon Sep 17 00:00:00 2001 From: Erik Scheffers Date: Wed, 26 Jan 2011 23:31:20 +0800 Subject: [PATCH] Added getProfileImageUrl() to handle "users/profile_image" api call --- twython/twython.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/twython/twython.py b/twython/twython.py index c312b76..b37c99f 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -402,6 +402,22 @@ class Twython(object): return urllib2.urlopen(r).read() except HTTPError, e: raise TwythonError("updateProfileImage() failed with a %d error code." % e.code, e.code) + + def getProfileImageUrl(self, username, size=None, version=1): + url = "http://api.twitter.com/%s/users/profile_image/%s.json" % (version, username) + if size: + url = self.constructApiURL(url, {'size':size}) + + try: + client = httplib2.Http() + client.follow_redirects = False + resp = client.request(url, 'HEAD')[0] + if resp['status'] not in ('301', '302', '303', '307'): + raise TwythonError("getProfileImageUrl() failed to get redirect.") + return resp['location'] + + except HTTPError, e: + raise TwythonError("getProfileImageUrl() failed with a %d error code." % e.code, e.code) @staticmethod def encode_multipart_formdata(fields, files):