From 5b72ccf5a285a65c1f5a468b714656fa1cc5d27c Mon Sep 17 00:00:00 2001 From: Rafael Ferreira Date: Fri, 19 Aug 2016 15:17:39 +0100 Subject: [PATCH 1/4] handling key exceptions This code is expecting too much from the dictionary structure. If the dictionary if lacking one key, the whole object goes south. I made one correction that get's my project going --- instagram/models.py | 49 +++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/instagram/models.py b/instagram/models.py index d2517ca2..26b4e9aa 100644 --- a/instagram/models.py +++ b/instagram/models.py @@ -75,50 +75,55 @@ def object_from_dictionary(cls, entry): new_media = Media(id=entry['id']) new_media.type = entry['type'] - new_media.user = User.object_from_dictionary(entry['user']) + try: + new_media.user = User.object_from_dictionary(entry['user']) + except: + new_media.user = None new_media.images = {} - for version, version_info in six.iteritems(entry['images']): + for version, version_info in six.iteritems(entry.get('images', [])): new_media.images[version] = Image.object_from_dictionary(version_info) if new_media.type == 'video': new_media.videos = {} - for version, version_info in six.iteritems(entry['videos']): + for version, version_info in six.iteritems(entry.get('videos', [])): new_media.videos[version] = Video.object_from_dictionary(version_info) - if 'user_has_liked' in entry: - new_media.user_has_liked = entry['user_has_liked'] - new_media.like_count = entry['likes']['count'] + + new_media.user_has_liked = entry.get('user_has_liked', False) + + new_media.like_count = entry['likes'].get('count', 0) new_media.likes = [] - if 'data' in entry['likes']: - for like in entry['likes']['data']: - new_media.likes.append(User.object_from_dictionary(like)) + for like in entry['likes'].get('data', []): + new_media.likes.append(User.object_from_dictionary(like)) - new_media.comment_count = entry['comments']['count'] - new_media.comments = [] - for comment in entry['comments']['data']: - new_media.comments.append(Comment.object_from_dictionary(comment)) + if entry.get('comments', False): + new_media.comment_count = entry['comments'].get('count', 0) + new_media.comments = [] + for comment in entry['comments'].get('data', []): + new_media.comments.append(Comment.object_from_dictionary(comment)) new_media.users_in_photo = [] - if entry.get('users_in_photo'): - for user_in_photo in entry['users_in_photo']: - new_media.users_in_photo.append(UserInPhoto.object_from_dictionary(user_in_photo)) + for user_in_photo in entry.get('users_in_photo', []): + new_media.users_in_photo.append(UserInPhoto.object_from_dictionary(user_in_photo)) - new_media.created_time = timestamp_to_datetime(entry['created_time']) + try: + new_media.created_time = timestamp_to_datetime(entry['created_time']) + except: + new_media.created_time = None if entry['location'] and 'id' in entry: new_media.location = Location.object_from_dictionary(entry['location']) new_media.caption = None - if entry['caption']: + if entry.get('caption', False): new_media.caption = Comment.object_from_dictionary(entry['caption']) new_media.tags = [] - if entry['tags']: - for tag in entry['tags']: - new_media.tags.append(Tag.object_from_dictionary({'name': tag})) + for tag in entry.get('tags', []): + new_media.tags.append(Tag.object_from_dictionary({'name': tag})) - new_media.link = entry['link'] + new_media.link = entry.get('link', '') new_media.filter = entry.get('filter') From 404ee609e31bbaef241ced29b5457c6779c6d415 Mon Sep 17 00:00:00 2001 From: Rafael Ferreira Date: Fri, 19 Aug 2016 15:26:00 +0100 Subject: [PATCH 2/4] Update models.py --- instagram/models.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/instagram/models.py b/instagram/models.py index 26b4e9aa..d88b6a49 100644 --- a/instagram/models.py +++ b/instagram/models.py @@ -104,8 +104,11 @@ def object_from_dictionary(cls, entry): new_media.comments.append(Comment.object_from_dictionary(comment)) new_media.users_in_photo = [] - for user_in_photo in entry.get('users_in_photo', []): - new_media.users_in_photo.append(UserInPhoto.object_from_dictionary(user_in_photo)) + try: + for user_in_photo in entry['users_in_photo']: + new_media.users_in_photo.append(UserInPhoto.object_from_dictionary(user_in_photo)) + except KeyError: + pass try: new_media.created_time = timestamp_to_datetime(entry['created_time']) From 0420960554a21f802d12ca38ddaf5a67385ea01b Mon Sep 17 00:00:00 2001 From: Rafael Ferreira Date: Fri, 19 Aug 2016 15:29:56 +0100 Subject: [PATCH 3/4] Update models.py --- instagram/models.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/instagram/models.py b/instagram/models.py index d88b6a49..1ad03d6b 100644 --- a/instagram/models.py +++ b/instagram/models.py @@ -104,11 +104,9 @@ def object_from_dictionary(cls, entry): new_media.comments.append(Comment.object_from_dictionary(comment)) new_media.users_in_photo = [] - try: + if 'users_in_photo' in entry: for user_in_photo in entry['users_in_photo']: new_media.users_in_photo.append(UserInPhoto.object_from_dictionary(user_in_photo)) - except KeyError: - pass try: new_media.created_time = timestamp_to_datetime(entry['created_time']) From 443458299fb310b524cd003eab3e21d38a015bfc Mon Sep 17 00:00:00 2001 From: Rafael Ferreira Date: Fri, 19 Aug 2016 15:40:24 +0100 Subject: [PATCH 4/4] Update models.py --- instagram/models.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/instagram/models.py b/instagram/models.py index 1ad03d6b..a9f6874e 100644 --- a/instagram/models.py +++ b/instagram/models.py @@ -104,9 +104,11 @@ def object_from_dictionary(cls, entry): new_media.comments.append(Comment.object_from_dictionary(comment)) new_media.users_in_photo = [] - if 'users_in_photo' in entry: - for user_in_photo in entry['users_in_photo']: - new_media.users_in_photo.append(UserInPhoto.object_from_dictionary(user_in_photo)) + try: + users_in_photo = [x for x in entry['users_in_photo']] + except: + users_in_photo = [] + new_media.users_in_photo = [UserInPhoto.object_from_dictionary(user_in_photo) for user_in_photo in users_in_photo] try: new_media.created_time = timestamp_to_datetime(entry['created_time'])