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

4.7k views Asked by At

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.

2

There are 2 answers

0
PIZZA PIZZA On BEST ANSWER

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)
0
krysopath On

I dont have any clue on the why/how. If you want to use the flickr_api module with python3.5+, you need to fix the Imports, like I did below:

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__

After this edit, it fails with another Import Error on:

>>> import flickr_api
<class 'SyntaxError'> invalid syntax (method_call.py, line 50)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/krysopath/.local/lib/python3.5/site-packages/flickr_api/__init__.py", line 32, in <module>
    from .auth import set_auth_handler
  File "/home/krysopath/.local/lib/python3.5/site-packages/flickr_api/auth.py", line 43, in <module>
    import urlparse
ImportError: No module named 'urlparse'

So you can fix this by yourself, if you want to, by just walking along the Import Errors and adding a dot to convert them into absolute Imports, that dont fail.

I guess, if you want to use this modul you have to fix it first... and have an unknown return. So if you didnt already invested heavily, it might be more effective to use that other module.