Skip to content

Commit a6d83a8

Browse files
authored
Merge pull request #2 from wkoot/safe_object_from_dictionary
Safe object from dictionary
2 parents c5d03af + e40306c commit a6d83a8

File tree

2 files changed

+74
-33
lines changed

2 files changed

+74
-33
lines changed

fixtures/media_search.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,60 @@
648648
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_113603_75sq_1287035206.jpg",
649649
"id": "113603"
650650
}
651+
},
652+
{
653+
"type": "video",
654+
"tags": [],
655+
"location": {
656+
"latitude": 37.775180799999987,
657+
"id": "68841",
658+
"longitude": -122.2270716,
659+
"name": "El Novillo Taco Truck"
660+
},
661+
"comments": {
662+
"count": 0
663+
},
664+
"caption": {
665+
"created_time": "1287585453",
666+
"text": "Type video without having videos in data",
667+
"from": {
668+
"username": "darodriguez",
669+
"first_name": "David",
670+
"last_name": "Rodriguez",
671+
"type": "user",
672+
"id": "113603"
673+
},
674+
"id": "495311"
675+
},
676+
"link": "http://localhost:8000/p/C5Wr/",
677+
"likes": {
678+
"count": 0
679+
},
680+
"created_time": "1287585407",
681+
"images": {
682+
"low_resolution": {
683+
"url": "http://distillery-dev.s3.amazonaws.com/media/2010/10/20/1fde15405b6349ef949c9a8f5498868e_6.jpg",
684+
"width": 480,
685+
"height": 480
686+
},
687+
"thumbnail": {
688+
"url": "http://distillery-dev.s3.amazonaws.com/media/2010/10/20/1fde15405b6349ef949c9a8f5498868e_5.jpg",
689+
"width": 150,
690+
"height": 150
691+
},
692+
"standard_resolution": {
693+
"url": "http://distillery-dev.s3.amazonaws.com/media/2010/10/20/1fde15405b6349ef949c9a8f5498868e_7.jpg",
694+
"width": 612,
695+
"height": 612
696+
}
697+
},
698+
"user_has_liked": false,
699+
"id": "759211",
700+
"user": {
701+
"username": "darodriguez",
702+
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_113603_75sq_1287035206.jpg",
703+
"id": "113603"
704+
}
651705
}
652706
]
653707
}

instagram/models.py

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33

44

55
class ApiModel(object):
6-
76
@classmethod
87
def object_from_dictionary(cls, entry):
98
# make dict keys all strings
109
if entry is None:
1110
return ""
11+
1212
entry_str_dict = dict([(str(key), value) for key, value in entry.items()])
1313
return cls(**entry_str_dict)
1414

1515
def __repr__(self):
1616
return str(self)
17-
# if six.PY2:
18-
# return six.text_type(self).encode('utf8')
19-
# else:
20-
# return self.encode('utf8')
2117

2218
def __str__(self):
2319
if six.PY3:
@@ -27,7 +23,6 @@ def __str__(self):
2723

2824

2925
class Image(ApiModel):
30-
3126
def __init__(self, url, width, height):
3227
self.url = url
3328
self.height = height
@@ -38,13 +33,11 @@ def __unicode__(self):
3833

3934

4035
class Video(Image):
41-
4236
def __unicode__(self):
4337
return "Video: %s" % self.url
4438

4539

4640
class Media(ApiModel):
47-
4841
def __init__(self, id=None, **kwargs):
4942
self.id = id
5043
for key, value in six.iteritems(kwargs):
@@ -62,41 +55,41 @@ def get_low_resolution_url(self):
6255
else:
6356
return self.videos['low_resolution'].url
6457

65-
6658
def get_thumbnail_url(self):
6759
return self.images['thumbnail'].url
6860

69-
7061
def __unicode__(self):
7162
return "Media: %s" % self.id
7263

7364
@classmethod
7465
def object_from_dictionary(cls, entry):
7566
new_media = Media(id=entry['id'])
7667
new_media.type = entry['type']
77-
7868
new_media.user = User.object_from_dictionary(entry['user'])
7969

8070
new_media.images = {}
81-
for version, version_info in six.iteritems(entry['images']):
82-
new_media.images[version] = Image.object_from_dictionary(version_info)
71+
if entry.get('images'):
72+
for version, version_info in six.iteritems(entry['images']):
73+
new_media.images[version] = Image.object_from_dictionary(version_info)
8374

84-
if new_media.type == 'video':
75+
if new_media.type == 'video' and entry.get('videos'):
8576
new_media.videos = {}
8677
for version, version_info in six.iteritems(entry['videos']):
8778
new_media.videos[version] = Video.object_from_dictionary(version_info)
8879

89-
if 'user_has_liked' in entry:
80+
if entry.get('user_has_liked'):
9081
new_media.user_has_liked = entry['user_has_liked']
91-
new_media.like_count = entry['likes']['count']
82+
83+
new_media.like_count = entry.get('likes', {}).get('count', 0)
9284
new_media.likes = []
93-
if 'data' in entry['likes']:
94-
for like in entry['likes']['data']:
95-
new_media.likes.append(User.object_from_dictionary(like))
85+
if new_media.like_count:
86+
if entry.get('likes', {}).get('data'):
87+
for like in entry['likes']['data']:
88+
new_media.likes.append(User.object_from_dictionary(like))
9689

9790
new_media.comment_count = entry.get('comments', {}).get('count', 0)
91+
new_media.comments = []
9892
if new_media.comment_count:
99-
new_media.comments = []
10093
if entry.get('comments', {}).get('data'):
10194
for comment in entry['comments']['data']:
10295
new_media.comments.append(Comment.object_from_dictionary(comment))
@@ -108,27 +101,25 @@ def object_from_dictionary(cls, entry):
108101

109102
new_media.created_time = timestamp_to_datetime(entry['created_time'])
110103

111-
if entry['location'] and 'id' in entry:
104+
if entry.get('location') and entry.get('id'):
112105
new_media.location = Location.object_from_dictionary(entry['location'])
113106

114107
new_media.caption = None
115-
if entry['caption']:
108+
if entry.get('caption'):
116109
new_media.caption = Comment.object_from_dictionary(entry['caption'])
117-
110+
118111
new_media.tags = []
119-
if entry['tags']:
112+
if entry.get('tags'):
120113
for tag in entry['tags']:
121114
new_media.tags.append(Tag.object_from_dictionary({'name': tag}))
122115

123116
new_media.link = entry['link']
124-
125117
new_media.filter = entry.get('filter')
126118

127119
return new_media
128120

129121

130122
class MediaShortcode(Media):
131-
132123
def __init__(self, shortcode=None, **kwargs):
133124
self.shortcode = shortcode
134125
for key, value in six.iteritems(kwargs):
@@ -181,19 +172,16 @@ def __init__(self, id, *args, **kwargs):
181172
def object_from_dictionary(cls, entry):
182173
point = None
183174
if 'latitude' in entry:
184-
point = Point(entry.get('latitude'),
185-
entry.get('longitude'))
186-
location = Location(entry.get('id', 0),
187-
point=point,
188-
name=entry.get('name', ''))
175+
point = Point(entry.get('latitude'), entry.get('longitude'))
176+
177+
location = Location(entry.get('id', 0), point=point, name=entry.get('name', ''))
189178
return location
190179

191180
def __unicode__(self):
192181
return "Location: %s (%s)" % (self.id, self.point)
193182

194183

195184
class User(ApiModel):
196-
197185
def __init__(self, id, *args, **kwargs):
198186
self.id = id
199187
for key, value in six.iteritems(kwargs):
@@ -204,7 +192,6 @@ def __unicode__(self):
204192

205193

206194
class Relationship(ApiModel):
207-
208195
def __init__(self, incoming_status="none", outgoing_status="none", target_user_is_private=False):
209196
self.incoming_status = incoming_status
210197
self.outgoing_status = outgoing_status

0 commit comments

Comments
 (0)