Skip to content

Commit

Permalink
Make invalid parameters consistently raise ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
ping committed Mar 23, 2017
1 parent f8878d8 commit 146a84b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion instagram_private_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def __init__(self, username, password, **kwargs):
proxy_address = '%s://%s' % (parsed_url.scheme, parsed_url.netloc)
proxy_handler = compat_urllib_request.ProxyHandler({'https': proxy_address})
else:
raise ClientError('Invalid proxy argument: %s' % proxy)
raise ValueError('Invalid proxy argument: %s' % proxy)
handlers = []
if proxy_handler:
handlers.append(proxy_handler)
Expand Down
42 changes: 21 additions & 21 deletions instagram_private_api/endpoints/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def configure(self, upload_id, size, caption='', location=None,
:return:
"""
if not self.compatible_aspect_ratio(size):
raise ClientError('Incompatible aspect ratio.')
raise ValueError('Incompatible aspect ratio.')

endpoint = 'media/configure/'
width, height = size
Expand Down Expand Up @@ -183,7 +183,7 @@ def configure_video(self, upload_id, size, duration, thumbnail_data, caption='',
:return:
"""
if not self.compatible_aspect_ratio(size):
raise ClientError('Incompatible aspect ratio.')
raise ValueError('Incompatible aspect ratio.')

# upload video thumbnail
self.post_photo(thumbnail_data, size, caption, upload_id, location=location,
Expand Down Expand Up @@ -248,7 +248,7 @@ def configure_to_reel(self, upload_id, size):
:return:
"""
if not self.reel_compatible_aspect_ratio(size):
raise ClientError('Incompatible aspect ratio.')
raise ValueError('Incompatible aspect ratio.')

endpoint = 'media/configure_to_story/'
width, height = size
Expand Down Expand Up @@ -293,7 +293,7 @@ def configure_video_to_reel(self, upload_id, size, duration, thumbnail_data):
:return:
"""
if not self.reel_compatible_aspect_ratio(size):
raise ClientError('Incompatible aspect ratio.')
raise ValueError('Incompatible aspect ratio.')

res = self.post_photo(thumbnail_data, size, '', upload_id=upload_id, to_reel=True)

Expand Down Expand Up @@ -357,12 +357,12 @@ def post_photo(self, photo_data, size, caption='', upload_id=None, to_reel=False

if not for_video:
if not to_reel and not self.compatible_aspect_ratio(size):
raise ClientError('Incompatible aspect ratio.')
raise ValueError('Incompatible aspect ratio.')
if to_reel and not self.reel_compatible_aspect_ratio(size):
raise ClientError('Incompatible reel aspect ratio.')
raise ValueError('Incompatible reel aspect ratio.')
if not 320 <= size[0] <= 1080:
# range from https://help.instagram.com/1631821640426723
raise ClientError('Invalid image width.')
raise ValueError('Invalid image width.')

location = kwargs.pop('location', None)
if location:
Expand Down Expand Up @@ -454,26 +454,26 @@ def post_video(self, video_data, size, duration, thumbnail_data, caption='', to_
warnings.warn('This endpoint has not been fully tested.', UserWarning)

if not to_reel and not self.compatible_aspect_ratio(size):
raise ClientError('Incompatible aspect ratio.')
raise ValueError('Incompatible aspect ratio.')

if to_reel and not self.reel_compatible_aspect_ratio(size):
raise ClientError('Incompatible reel aspect ratio.')
raise ValueError('Incompatible reel aspect ratio.')

if not 612 <= size[0] <= 1080:
# range was determined through sampling of video uploads
raise ClientError('Invalid video width.')
raise ValueError('Invalid video width.')

if duration < 3.0:
raise ClientError('Duration is less than 3s')
raise ValueError('Duration is less than 3s')

if not to_reel and duration > 60.0:
raise ClientError('Duration is more than 60s')
raise ValueError('Duration is more than 60s')

if to_reel and duration > 15.0:
raise ClientError('Duration is more than 15s')
raise ValueError('Duration is more than 15s')

if len(video_data) > 50 * 1024 * 1000:
raise ClientError('Video file is too big')
raise ValueError('Video file is too big')

location = kwargs.pop('location', None)
if location:
Expand Down Expand Up @@ -619,19 +619,19 @@ def post_album(self, medias, caption='', location=None, **kwargs):
if len(children_metadata) >= 10:
continue
if media.get('type', '') not in ['image', 'video']:
raise ClientError('Invalid media type: %s' % media.get('type', ''))
raise ValueError('Invalid media type: %s' % media.get('type', ''))
if not media.get('data'):
raise ClientError('Data not specified.')
raise ValueError('Data not specified.')
if not media.get('size'):
raise ClientError('Size not specified.')
raise ValueError('Size not specified.')
if media['type'] == 'video':
if not media.get('duration'):
raise ClientError('Duration not specified.')
raise ValueError('Duration not specified.')
if not media.get('thumbnail'):
raise ClientError('Thumbnail not specified.')
raise ValueError('Thumbnail not specified.')
aspect_ratio = (media['size'][0] * 1.0) / (media['size'][1] * 1.0)
if aspect_ratio > 1.0 or aspect_ratio < 1.0:
raise ClientError('Invalid media aspect ratio')
raise ValueError('Invalid media aspect ratio')

if media['type'] == 'video':
metadata = self.post_video(
Expand All @@ -654,7 +654,7 @@ def post_album(self, medias, caption='', location=None, **kwargs):
children_metadata.append(metadata)

if len(children_metadata) <= 1:
raise ClientError('Invalid number of media objects: %d' % len(children_metadata))
raise ValueError('Invalid number of media objects: %d' % len(children_metadata))

# configure as sidecar
endpoint = 'media/configure_sidecar/'
Expand Down
2 changes: 1 addition & 1 deletion instagram_web_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, user_agent=None, **kwargs):
proxy_address = '%s://%s' % (parsed_url.scheme, parsed_url.netloc)
proxy_handler = compat_urllib_request.ProxyHandler({'https': proxy_address})
else:
raise ClientError('Invalid proxy argument: %s' % proxy)
raise ValueError('Invalid proxy argument: %s' % proxy)
handlers = []
if proxy_handler:
handlers.append(proxy_handler)
Expand Down

0 comments on commit 146a84b

Please sign in to comment.