From bc994d0993f0916b12842ab894d0165de5275d6c Mon Sep 17 00:00:00 2001 From: Harmon Date: Thu, 14 Jan 2021 10:04:43 -0600 Subject: [PATCH] Remove API._pack_image --- tweepy/api.py | 61 --------------------------------------------------- 1 file changed, 61 deletions(-) diff --git a/tweepy/api.py b/tweepy/api.py index eeb7a1d..800bf97 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -1479,64 +1479,3 @@ class API: payload_type='json', require_auth=True ) - - """ 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 5MB 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 OSError 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 -- 2.25.1