Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #538 from nxexox/master
Browse files Browse the repository at this point in the history
fix upload media chunked async method in twitter_utils.parse_media_file
  • Loading branch information
jeremylow authored Jun 7, 2018
2 parents 3e0e99b + 438be01 commit 85b12e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
10 changes: 7 additions & 3 deletions twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,13 @@ def PostUpdate(self,
else:
_, _, file_size, media_type = parse_media_file(media)
if file_size > self.chunk_size or media_type in chunked_types:
media_ids.append(self.UploadMediaChunked(media, media_additional_owners))
media_ids.append(self.UploadMediaChunked(
media, media_additional_owners, media_category=media_category
))
else:
media_ids.append(self.UploadMediaSimple(media, media_additional_owners))
media_ids.append(self.UploadMediaSimple(
media, media_additional_owners, media_category=media_category
))
parameters['media_ids'] = ','.join([str(mid) for mid in media_ids])

if latitude is not None and longitude is not None:
Expand Down Expand Up @@ -1262,7 +1266,7 @@ def _UploadMediaChunkedInit(self,
"""
url = '%s/media/upload.json' % self.upload_url

media_fp, filename, file_size, media_type = parse_media_file(media)
media_fp, filename, file_size, media_type = parse_media_file(media, async_upload=True)

if not all([media_fp, filename, file_size, media_type]):
raise TwitterError({'message': 'Could not process media file'})
Expand Down
15 changes: 11 additions & 4 deletions twitter/twitter_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,25 @@ def http_to_file(http):
return data_file


def parse_media_file(passed_media):
def parse_media_file(passed_media, async_upload=False):
""" Parses a media file and attempts to return a file-like object and
information about the media file.
Args:
passed_media: media file which to parse.
async_upload: flag, for validation media file attributes.
Returns:
file-like object, the filename of the media file, the file size, and
the type of media.
"""
img_formats = ['image/jpeg',
'image/png',
'image/gif',
'image/bmp',
'image/webp']
long_img_formats = [
'image/gif'
]
video_formats = ['video/mp4',
'video/quicktime']

Expand Down Expand Up @@ -266,9 +269,13 @@ def parse_media_file(passed_media):
if media_type is not None:
if media_type in img_formats and file_size > 5 * 1048576:
raise TwitterError({'message': 'Images must be less than 5MB.'})
elif media_type in video_formats and file_size > 15 * 1048576:
elif media_type in long_img_formats and file_size > 15 * 1048576:
raise TwitterError({'message': 'GIF Image must be less than 15MB.'})
elif media_type in video_formats and not async_upload and file_size > 15 * 1048576:
raise TwitterError({'message': 'Videos must be less than 15MB.'})
elif media_type not in img_formats and media_type not in video_formats:
elif media_type in video_formats and async_upload and file_size > 512 * 1048576:
raise TwitterError({'message': 'Videos must be less than 512MB.'})
elif media_type not in img_formats and media_type not in video_formats and media_type not in long_img_formats:
raise TwitterError({'message': 'Media type could not be determined.'})

return data_file, filename, file_size, media_type
Expand Down

0 comments on commit 85b12e0

Please sign in to comment.