)
@payload('user')
- def update_profile_image(self, filename, file_=None, **kwargs):
+ def update_profile_image(self, filename, file=None, *args, **kwargs):
""" :reference: https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/post-account-update_profile_image
"""
- headers, post_data = API._pack_image(filename, 700, f=file_)
+ if file is not None:
+ files = {'image': (filename, file)}
+ else:
+ files = {'image': open(filename, 'rb')}
return self.request(
- 'POST', 'account/update_profile_image', *args,
- endpoint_parameters=(
+ 'POST', 'account/update_profile_image', endpoint_parameters=(
'include_entities', 'skip_status'
- ), post_data=post_data, headers=headers, **kwargs
+ ), files=files, **kwargs
)
- def update_profile_banner(self, filename, **kwargs):
+ def update_profile_banner(self, filename, file=None, *args, **kwargs):
""" :reference: https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/post-account-update_profile_banner
"""
- f = kwargs.pop('file', None)
- headers, post_data = API._pack_image(filename, 700,
- form_field='banner', f=f)
+ if file is not None:
+ files = {'banner': (filename, file)}
+ else:
+ files = {'banner': open(filename, 'rb')}
return self.request(
- 'POST', 'account/update_profile_banner', endpoint_parameters=(
+ 'POST', 'account/update_profile_banner', *args,
+ endpoint_parameters=(
'width', 'height', 'offset_left', 'offset_right'
- ), post_data=post_data, headers=headers, **kwargs
+ ), files=files, **kwargs
)
@payload('user')
)
@payload('json')
- def supported_languages(self, *args, **kwargs):
+ def supported_languages(self, **kwargs):
""" :reference: https://developer.twitter.com/en/docs/developer-utilities/supported-languages/api-reference/get-help-languages
"""
- return self.request('GET', 'help/languages', *args, **kwargs)
+ return self.request('GET', 'help/languages', **kwargs)
@payload('json')
- def configuration(self, *args, **kwargs):
+ def configuration(self, **kwargs):
""" :reference: https://developer.twitter.com/en/docs/developer-utilities/configuration/api-reference/get-help-configuration
"""
- return self.request('GET', 'help/configuration', *args, **kwargs)
+ return self.request('GET', 'help/configuration', **kwargs)
-
- """ Internal use only """
-
- @staticmethod
- def _pack_image(filename, max_size, form_field='image', f=None, file_type=None):
- """Pack image from file into multipart-formdata post body"""
- # image must be less than 700kb in size
- if f is None:
- try:
- if os.path.getsize(filename) > (max_size * 1024):
- raise TweepError(f'File is too big, must be less than {max_size}kb.')
- except os.error as e:
- raise TweepError(f'Unable to access file: {e.strerror}')
-
- # build the mulitpart-formdata body
- fp = open(filename, 'rb')
- else:
- f.seek(0, 2) # Seek to end of file
- if f.tell() > (max_size * 1024):
- raise TweepError(f'File is too big, must be less than {max_size}kb.')
- f.seek(0) # Reset to beginning of file
- fp = f
-
- # image must be gif, jpeg, png, webp
- if not file_type:
- h = None
- if f is not None:
- h = f.read(32)
- f.seek(0)
- file_type = imghdr.what(filename, h=h) or mimetypes.guess_type(filename)[0]
- if file_type is None:
- raise TweepError('Could not determine file type')
- if file_type in ['gif', 'jpeg', 'png', 'webp']:
- file_type = 'image/' + file_type
- elif file_type not in ['image/gif', 'image/jpeg', 'image/png']:
- raise TweepError(f'Invalid file type for image: {file_type}')
-
- if isinstance(filename, str):
- filename = filename.encode('utf-8')
-
- BOUNDARY = b'Tw3ePy'
- body = []
- body.append(b'--' + BOUNDARY)
- body.append(f'Content-Disposition: form-data; name="{form_field}";'
- f' filename="{filename}"'
- .encode('utf-8'))
- body.append(f'Content-Type: {file_type}'.encode('utf-8'))
- body.append(b'')
- body.append(fp.read())
- body.append(b'--' + BOUNDARY + b'--')
- body.append(b'')
- fp.close()
- body = b'\r\n'.join(body)
-
- # build headers
- headers = {
- 'Content-Type': 'multipart/form-data; boundary=Tw3ePy',
- 'Content-Length': str(len(body))
- }
-
- return headers, body