Search code examples
pythonapipython-3.xflickr

How to download photos from Flickr by Flickr API in Python 3


noob question series...

I am a new learner of python, recently want to create a small python application that can collect photos from flickr based on different search input. (eg: if i input "dog", it will download all dog images from flickr)

I did some research online and notice that flickr API might be the best way and the method flickr.photos.getSizes should be the one I need to use.

However, I have few stupid questions when coding:

  1. I have applied my key and secret for flickr API, I just don't know what to do next with flickr.photos.getSizes in python to download photos. Like, how to call this method in python? (and I noticed required arguments for this method are keys and photo_id, how to get photo_ids based on search input "dog")

  2. Then I followed a tutorial from https://github.com/alexis-mignon/python-flickr-api/wiki/Tutorial but when I imported flickr_api I got error message:

    Could not load all modules
    <class 'ImportError'> No module named 'objects'
    Traceback (most recent call last):
      File "D:/Agfa/Projects/Image/flickr.py", line 2, in <module>
        import flickr_api
      File "D:\Application\Anaconda3\lib\site-packages\flickr_api\__init__.py", line 32, in <module>
        from auth import set_auth_handler
    ImportError: cannot import name 'set_auth_handler'
    

    Then I took a look at the _ init _.py:

    try:
        from objects import *
        import objects
        import upload as Upload
        from upload import upload, replace
    except Exception as e:
        print "Could not load all modules"
        print type(e), e
    
    from auth import set_auth_handler
    from method_call import enable_cache, disable_cache
    from keys import set_keys
    from _version import __version__
    

    Seems like this library does not support python 3 but I don't know what to do. (I cannot install methond_call, keys, _version on my python 3) guess I will use flickrapi

Thank you so much for your time and again thanks in advance.


Solution

  • I think I finally got the proper way to use FlickrAPI:

    there are many ways but I figured out 2:

    def flickr_walk(keyward):
        count = 0
        photos = flickr.walk(text=keyward,
                     tag_mode='all',
                     tags=keyward,
                     extras='url_c',
                     per_page=100)
    
        for photo in photos:
            try:
                url=photo.get('url_c')
                urllib.request.urlretrieve(url, path+'\\' + str(count) +".jpg")
            except Exception as e:
                print('failed to download image')
    

    flickr.walk uses Photos.search API, I can use the API directly as well:

    def flickr_search(keyward):
        obj = flickr.photos.search(text=keyward,
                               tags=keyward,
                               extras='url_c',
                               per_page=5)
    
        for photo in obj:
            url=photo.get('url_c')
            photos = ET.dump(obj)
            print (photos)
    

    Remember to get the key and secret first:

    api_key = 'xxxxxxxxxxxxxxxx'
    api_secret = 'xxxxxxxxxxxxx'
    
    flickr=flickrapi.FlickrAPI(api_key,api_secret,cache=True)