sybrenstuvel / flickrapi

Python Flickr API implementation
https://stuvel.eu/flickrapi
Other
155 stars 33 forks source link

introduce FlickrDuplicate exception #137

Closed vladak closed 2 years ago

vladak commented 2 years ago

The Flickr upload API allows to specify dedup_check parameter with value either 1 or 2. This is actually what the official Flickr Uploadr app is using by default. When a duplicate photo is detected, the response returns the ID of the original photo. The photo being duplicate does not seem like a hard error to me, that's why this change proposes to add FlickrDuplicate exception that stores the photo ID.

This way it is possible to construct a piece of code that always returns photo ID:

    with open(file_path, 'rb') as fp:                                           
        try:                                                                    
            rsp = flickr_handle.upload(file_path, fileobj=fp,                   
                                       **params)                                
            logger.debug(ElementTree.tostring(rsp, 'utf-8'))                    
            photo_id = rsp.find('photoid')                                      
            if photo_id is not None:                                            
                res = photo_id.text                                             
        except flickrapi.exceptions.FlickrDuplicate as e:                       
            logger.info("Duplicate photo '{}' with ID {}".                      
                        format(file_path, e.duplicate_photo_id))                
            res = e.duplicate_photo_id

which is particularly useful when uploading set of photos and adding them to an album.

I was not able to get parse_json() to work with flickr_handle.upload() - even though the flickr handle was constructed with format='parsed-json', the parse_json() still received XML as input. So I left it unchanged.