3
3
4
4
5
5
class ApiModel (object ):
6
-
7
6
@classmethod
8
7
def object_from_dictionary (cls , entry ):
9
8
# make dict keys all strings
10
9
if entry is None :
11
10
return ""
11
+
12
12
entry_str_dict = dict ([(str (key ), value ) for key , value in entry .items ()])
13
13
return cls (** entry_str_dict )
14
14
15
15
def __repr__ (self ):
16
16
return str (self )
17
- # if six.PY2:
18
- # return six.text_type(self).encode('utf8')
19
- # else:
20
- # return self.encode('utf8')
21
17
22
18
def __str__ (self ):
23
19
if six .PY3 :
@@ -27,7 +23,6 @@ def __str__(self):
27
23
28
24
29
25
class Image (ApiModel ):
30
-
31
26
def __init__ (self , url , width , height ):
32
27
self .url = url
33
28
self .height = height
@@ -38,13 +33,11 @@ def __unicode__(self):
38
33
39
34
40
35
class Video (Image ):
41
-
42
36
def __unicode__ (self ):
43
37
return "Video: %s" % self .url
44
38
45
39
46
40
class Media (ApiModel ):
47
-
48
41
def __init__ (self , id = None , ** kwargs ):
49
42
self .id = id
50
43
for key , value in six .iteritems (kwargs ):
@@ -62,41 +55,41 @@ def get_low_resolution_url(self):
62
55
else :
63
56
return self .videos ['low_resolution' ].url
64
57
65
-
66
58
def get_thumbnail_url (self ):
67
59
return self .images ['thumbnail' ].url
68
60
69
-
70
61
def __unicode__ (self ):
71
62
return "Media: %s" % self .id
72
63
73
64
@classmethod
74
65
def object_from_dictionary (cls , entry ):
75
66
new_media = Media (id = entry ['id' ])
76
67
new_media .type = entry ['type' ]
77
-
78
68
new_media .user = User .object_from_dictionary (entry ['user' ])
79
69
80
70
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 )
83
74
84
- if new_media .type == 'video' :
75
+ if new_media .type == 'video' and entry . get ( 'videos' ) :
85
76
new_media .videos = {}
86
77
for version , version_info in six .iteritems (entry ['videos' ]):
87
78
new_media .videos [version ] = Video .object_from_dictionary (version_info )
88
79
89
- if 'user_has_liked' in entry :
80
+ if entry . get ( 'user_has_liked' ) :
90
81
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 )
92
84
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 ))
96
89
97
90
new_media .comment_count = entry .get ('comments' , {}).get ('count' , 0 )
91
+ new_media .comments = []
98
92
if new_media .comment_count :
99
- new_media .comments = []
100
93
if entry .get ('comments' , {}).get ('data' ):
101
94
for comment in entry ['comments' ]['data' ]:
102
95
new_media .comments .append (Comment .object_from_dictionary (comment ))
@@ -108,27 +101,25 @@ def object_from_dictionary(cls, entry):
108
101
109
102
new_media .created_time = timestamp_to_datetime (entry ['created_time' ])
110
103
111
- if entry [ 'location' ] and 'id' in entry :
104
+ if entry . get ( 'location' ) and entry . get ( 'id' ) :
112
105
new_media .location = Location .object_from_dictionary (entry ['location' ])
113
106
114
107
new_media .caption = None
115
- if entry [ 'caption' ] :
108
+ if entry . get ( 'caption' ) :
116
109
new_media .caption = Comment .object_from_dictionary (entry ['caption' ])
117
-
110
+
118
111
new_media .tags = []
119
- if entry [ 'tags' ] :
112
+ if entry . get ( 'tags' ) :
120
113
for tag in entry ['tags' ]:
121
114
new_media .tags .append (Tag .object_from_dictionary ({'name' : tag }))
122
115
123
116
new_media .link = entry ['link' ]
124
-
125
117
new_media .filter = entry .get ('filter' )
126
118
127
119
return new_media
128
120
129
121
130
122
class MediaShortcode (Media ):
131
-
132
123
def __init__ (self , shortcode = None , ** kwargs ):
133
124
self .shortcode = shortcode
134
125
for key , value in six .iteritems (kwargs ):
@@ -181,19 +172,16 @@ def __init__(self, id, *args, **kwargs):
181
172
def object_from_dictionary (cls , entry ):
182
173
point = None
183
174
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' , '' ))
189
178
return location
190
179
191
180
def __unicode__ (self ):
192
181
return "Location: %s (%s)" % (self .id , self .point )
193
182
194
183
195
184
class User (ApiModel ):
196
-
197
185
def __init__ (self , id , * args , ** kwargs ):
198
186
self .id = id
199
187
for key , value in six .iteritems (kwargs ):
@@ -204,7 +192,6 @@ def __unicode__(self):
204
192
205
193
206
194
class Relationship (ApiModel ):
207
-
208
195
def __init__ (self , incoming_status = "none" , outgoing_status = "none" , target_user_is_private = False ):
209
196
self .incoming_status = incoming_status
210
197
self .outgoing_status = outgoing_status
0 commit comments