33
44
55class 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
2925class 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
4035class Video (Image ):
41-
4236 def __unicode__ (self ):
4337 return "Video: %s" % self .url
4438
4539
4640class 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
130122class 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
195184class 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
206194class 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