@@ -48,6 +48,9 @@ def refresh(self):
4848
4949
5050class ImgurClient :
51+ allowed_album_fields = {
52+ 'ids' , 'title' , 'description' , 'privacy' , 'layout' , 'cover'
53+ }
5154
5255 def __init__ (self , client_id = None , client_secret = None , access_token = None , refresh_token = None ):
5356 self .client_id = client_id
@@ -76,12 +79,18 @@ def make_request(self, method, route, data=None):
7679 header = self .prepare_headers ()
7780 url = API_URL + '3/%s' % route
7881
79- response = method_to_call (url , headers = header , data = data )
82+ if method == 'delete' :
83+ response = method_to_call (url , headers = header , params = data )
84+ else :
85+ response = method_to_call (url , headers = header , data = data )
8086
8187 if response .status_code == 403 and self .auth is not None :
8288 self .auth .refresh ()
8389 header = self .prepare_headers ()
84- response = method_to_call (url , headers = header , data = data )
90+ if method == 'delete' :
91+ response = method_to_call (url , headers = header , params = data )
92+ else :
93+ response = method_to_call (url , headers = header , data = data )
8594
8695 # TODO: Add rate-limit checks
8796
@@ -221,4 +230,53 @@ def get_account_image_ids(self, username, page=0):
221230
222231 def get_account_images_count (self , username , page = 0 ):
223232 self .validate_user_context (username )
224- return self .make_request ('GET' , 'account/%s/images/ids/%d' % (username , page ))
233+ return self .make_request ('GET' , 'account/%s/images/ids/%d' % (username , page ))
234+
235+ def get_album (self , album_id ):
236+ album = self .make_request ('GET' , 'album/%s' % album_id )
237+ return Album (album )
238+
239+ def get_album_images (self , album_id ):
240+ images = self .make_request ('GET' , 'album/%s/images' % album_id )
241+ return [Image (image ) for image in images ]
242+
243+ def create_album (self , fields ):
244+ post_data = {field : fields [field ] for field in set (self .allowed_album_fields ).intersection (fields .keys ())}
245+
246+ if 'ids' in post_data :
247+ self .logged_in ()
248+
249+ return self .make_request ('POST' , 'album' , data = post_data )
250+
251+ def update_album (self , album_id , fields ):
252+ post_data = {field : fields [field ] for field in set (self .allowed_album_fields ).intersection (fields .keys ())}
253+
254+ if isinstance (post_data ['ids' ], list ):
255+ post_data ['ids' ] = ',' .join (post_data ['ids' ])
256+
257+ return self .make_request ('POST' , 'album/%s' % album_id , data = post_data )
258+
259+ def album_delete (self , album_id ):
260+ return self .make_request ('DELETE' , 'album/%s' % album_id )
261+
262+ def album_favorite (self , album_id ):
263+ self .logged_in ()
264+ return self .make_request ('POST' , 'album/%s/favorite' % album_id )
265+
266+ def album_set_images (self , album_id , ids ):
267+ if isinstance (ids , list ):
268+ ids = ',' .join (ids )
269+
270+ return self .make_request ('POST' , 'album/%s/' % album_id , {'ids' : ids })
271+
272+ def album_add_images (self , album_id , ids ):
273+ if isinstance (ids , list ):
274+ ids = ',' .join (ids )
275+
276+ return self .make_request ('POST' , 'album/%s/add' % album_id , {'ids' : ids })
277+
278+ def album_remove_images (self , album_id , ids ):
279+ if isinstance (ids , list ):
280+ ids = ',' .join (ids )
281+
282+ return self .make_request ('DELETE' , 'album/%s/remove_images' % album_id , {'ids' : ids })
0 commit comments