From 04f260c0f233755392057ac9048d1b05b65dab92 Mon Sep 17 00:00:00 2001 From: Harmon Date: Thu, 14 Jan 2021 10:02:55 -0600 Subject: [PATCH] Improve API.update_with_media file uploading Use files parameter of requests Session.request Allow Twitter's API to respond with error code 193 and message "One or more of the uploaded media is too large." instead of checking for file size prior to upload. Allow Twitter's API to respond with HTTP status code 403, error code 189, and error message "Error creating status." instead of checking for file type prior to upload. --- tweepy/api.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tweepy/api.py b/tweepy/api.py index 4793d17..eeb7a1d 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -372,10 +372,10 @@ class API: 'auto_populate_reply_metadata', 'lat', 'long', 'place_id', 'display_coordinates' """ - headers, post_data = API._pack_image(filename, 3072, - form_field='media[]', f=file) - kwargs.update({'headers': headers, 'post_data': post_data}) - + if file is not None: + files = {'media[]': (filename, file)} + else: + files = {'media[]': open(filename, 'rb')} return bind_api( api=self, path='/statuses/update_with_media.json', @@ -387,7 +387,7 @@ class API: 'auto_populate_reply_metadata', 'lat', 'long', 'place_id', 'display_coordinates'], require_auth=True - )(*args, **kwargs) + )(*args, files=files, **kwargs) def get_media_upload_status(self, *args, **kwargs): """ :reference: https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/get-media-upload-status -- 2.25.1