From 4fd1096da707536c9e2226f70756c1ebc782042d Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Tue, 18 Aug 2009 15:19:44 -0500 Subject: [PATCH] Fix issues with update profile images causing 500 error. --- ROADMAP | 1 - tweepy/api.py | 17 +++++++++++++---- tweepy/binder.py | 14 ++++++++------ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/ROADMAP b/ROADMAP index 7f69a97..3a30a6a 100644 --- a/ROADMAP +++ b/ROADMAP @@ -7,7 +7,6 @@ The plan of attack for the next version of Tweepy. + prepare for social graph changes mentioned on mailinglist + finish search api + autodetect authenticated user's ID [DONE] -+ timeline paging Future... ========= diff --git a/tweepy/api.py b/tweepy/api.py index 1e9a603..03cd2dc 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -247,22 +247,24 @@ class API(object): """Update profile image""" def update_profile_image(self, filename): + headers, post_data = _pack_image(filename, 700) bind_api( path = '/account/update_profile_image.json', method = 'POST', parser = parse_none, require_auth = True - )(self, post_data = _pack_image(filename, 700)) + )(self, post_data=post_data, headers=headers) """Update profile background image""" def update_profile_background_image(self, filename, *args, **kargs): + headers, post_data = _pack_image(filename, 800) bind_api( path = '/account/update_profile_background_image.json', method = 'POST', parser = parse_none, allowed_param = ['tile'], require_auth = True - )(self, post_data = _pack_image(filename, 800)) + )(self, post_data=post_data, headers=headers) """Update profile""" update_profile = bind_api( @@ -438,7 +440,7 @@ def _pack_image(filename, max_size): # build the mulitpart-formdata body fp = open(filename, 'rb') - BOUNDARY = '--Tw3ePy' + BOUNDARY = 'Tw3ePy' body = [] body.append('--' + BOUNDARY) body.append('Content-Disposition: form-data; name="image"; filename="%s"' % filename) @@ -448,6 +450,13 @@ def _pack_image(filename, max_size): body.append('--' + BOUNDARY + '--') body.append('') fp.close() + body = '\r\n'.join(body) - return '\r\n'.join(body) + # build headers + headers = { + 'Content-Type': 'multipart/form-data; boundary=Tw3ePy', + 'Content-Length': len(body) + } + + return headers, body diff --git a/tweepy/binder.py b/tweepy/binder.py index 5b3fe62..9fe2432 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -23,6 +23,13 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False, else: post_data = None + # check for headers + if 'headers' in kargs: + headers = dict(kargs['headers']) + del kargs['headers'] + else: + headers = {} + # build parameter dict if allowed_param: parameters = {} @@ -42,11 +49,6 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False, raise TweepError('This method takes no parameters!') parameters = None - # Assemble headers - headers = { - 'User-Agent': 'tweepy' - } - # Build url with parameters if parameters: url = '%s?%s' % (api.api_root + path, urllib.urlencode(parameters)) @@ -79,7 +81,7 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False, conn = httplib.HTTPConnection(_host, timeout=10.0) # Build request - conn.request(method, url, headers=headers) + conn.request(method, url, headers=headers, body=post_data) # Get response resp = conn.getresponse() -- 2.25.1