From: RouxRC Date: Tue, 22 Jul 2014 22:24:37 +0000 (+0200) Subject: fix image sending to handle both base64 images and plain bytes X-Git-Tag: twitter-1.15.0~14 X-Git-Url: https://jfr.im/git/z_archive/twitter.git/commitdiff_plain/525c9c31eb8a3f10834832898f20931d55daa794 fix image sending to handle both base64 images and plain bytes --- diff --git a/twitter/api.py b/twitter/api.py index be0ef43..28979b5 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -205,6 +205,13 @@ class TwitterCall(object): for arg in ['media[]', 'banner', 'image']: if arg in kwargs: media = kwargs.pop(arg) + # Check if argument tells whether img is already base64 encoded + b64_convert = True + if "_base64" in kwargs: + b64_convert = not kwargs.pop("_base64") + if b64_convert: + import base64 + media = base64.b64encode(media) mediafield = arg break @@ -229,6 +236,7 @@ class TwitterCall(object): bod.append('--' + BOUNDARY) bod.append( 'Content-Disposition: form-data; name="%s"' % mediafield) + bod.append('Content-Transfer-Encoding: base64') bod.append('') bod.append(media) for k, v in kwargs.items():