Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

handling key exceptions #237

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 30 additions & 22 deletions instagram/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,50 +75,58 @@ 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))
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]

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')

Expand Down