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

fix upload media chunked async method in twitter_utils.parse_media_file #538

Merged
merged 6 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,9 +1098,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 @@ -1202,7 +1206,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 @@ -190,22 +190,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 @@ -240,9 +243,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