Skip to content

Commit 0b67a18

Browse files
committed
Safely fetch comment data, by checking availability before calling
1 parent 6522cff commit 0b67a18

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

instagram/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ def object_from_dictionary(cls, entry):
9494
for like in entry['likes']['data']:
9595
new_media.likes.append(User.object_from_dictionary(like))
9696

97-
new_media.comment_count = entry['comments']['count']
98-
new_media.comments = []
99-
for comment in entry['comments']['data']:
100-
new_media.comments.append(Comment.object_from_dictionary(comment))
97+
new_media.comment_count = entry.get('comments', {}).get('count', 0)
98+
if new_media.comment_count:
99+
new_media.comments = []
100+
if entry.get('comments', {}).get('data'):
101+
for comment in entry['comments']['data']:
102+
new_media.comments.append(Comment.object_from_dictionary(comment))
101103

102104
new_media.users_in_photo = []
103105
if entry.get('users_in_photo'):

0 commit comments

Comments
 (0)