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

Commit 85b12e0

Browse files
authored
Merge pull request #538 from nxexox/master
fix upload media chunked async method in twitter_utils.parse_media_file
2 parents 3e0e99b + 438be01 commit 85b12e0

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

twitter/api.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,9 +1158,13 @@ def PostUpdate(self,
11581158
else:
11591159
_, _, file_size, media_type = parse_media_file(media)
11601160
if file_size > self.chunk_size or media_type in chunked_types:
1161-
media_ids.append(self.UploadMediaChunked(media, media_additional_owners))
1161+
media_ids.append(self.UploadMediaChunked(
1162+
media, media_additional_owners, media_category=media_category
1163+
))
11621164
else:
1163-
media_ids.append(self.UploadMediaSimple(media, media_additional_owners))
1165+
media_ids.append(self.UploadMediaSimple(
1166+
media, media_additional_owners, media_category=media_category
1167+
))
11641168
parameters['media_ids'] = ','.join([str(mid) for mid in media_ids])
11651169

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

1265-
media_fp, filename, file_size, media_type = parse_media_file(media)
1269+
media_fp, filename, file_size, media_type = parse_media_file(media, async_upload=True)
12661270

12671271
if not all([media_fp, filename, file_size, media_type]):
12681272
raise TwitterError({'message': 'Could not process media file'})

twitter/twitter_utils.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,25 @@ def http_to_file(http):
216216
return data_file
217217

218218

219-
def parse_media_file(passed_media):
219+
def parse_media_file(passed_media, async_upload=False):
220220
""" Parses a media file and attempts to return a file-like object and
221221
information about the media file.
222222
223223
Args:
224224
passed_media: media file which to parse.
225+
async_upload: flag, for validation media file attributes.
225226
226227
Returns:
227228
file-like object, the filename of the media file, the file size, and
228229
the type of media.
229230
"""
230231
img_formats = ['image/jpeg',
231232
'image/png',
232-
'image/gif',
233233
'image/bmp',
234234
'image/webp']
235+
long_img_formats = [
236+
'image/gif'
237+
]
235238
video_formats = ['video/mp4',
236239
'video/quicktime']
237240

@@ -266,9 +269,13 @@ def parse_media_file(passed_media):
266269
if media_type is not None:
267270
if media_type in img_formats and file_size > 5 * 1048576:
268271
raise TwitterError({'message': 'Images must be less than 5MB.'})
269-
elif media_type in video_formats and file_size > 15 * 1048576:
272+
elif media_type in long_img_formats and file_size > 15 * 1048576:
273+
raise TwitterError({'message': 'GIF Image must be less than 15MB.'})
274+
elif media_type in video_formats and not async_upload and file_size > 15 * 1048576:
270275
raise TwitterError({'message': 'Videos must be less than 15MB.'})
271-
elif media_type not in img_formats and media_type not in video_formats:
276+
elif media_type in video_formats and async_upload and file_size > 512 * 1048576:
277+
raise TwitterError({'message': 'Videos must be less than 512MB.'})
278+
elif media_type not in img_formats and media_type not in video_formats and media_type not in long_img_formats:
272279
raise TwitterError({'message': 'Media type could not be determined.'})
273280

274281
return data_file, filename, file_size, media_type

0 commit comments

Comments
 (0)