This post provides a quick example of how to search the Flickr API for images using the flickrapi Python library.
Setup
Flickr API Keys
First, you need to create a pair of Flickr API keys by visiting https://www.flickr.com/services/api/keys/. Note: A Yahoo! account is required to generate the API keys.
Flickr will generate two keys:
- A public key, which they call
key
- A private key, which they call
secret
flickrapi installation
Create a virtual environment, and then install the flickrapi Python library via
$ pip install flickrapi
Search Flickr Images
Here's an example that searches Flickr for the term "kitten" and retrieves the first 10 results.
1 2 3 4 5 6 7 8 9 10 11 | from flickrapi import FlickrAPI FLICKR_PUBLIC = 'Your Flickr Key' FLICKR_SECRET = 'Your Flickr Secret Key' flickr = FlickrAPI(FLICKR_PUBLIC, FLICKR_SECRET, format='parsed-json') extras='url_sq,url_t,url_s,url_q,url_m,url_n,url_z,url_c,url_l,url_o' cats = flickr.photos.search(text='kitten', per_page=5, extras=extras) photos = cats['photos'] from pprint import pprint pprint(photos) |
The output:
{'page': 1, 'pages': 65078, 'perpage': 5, 'photo': [{'farm': 1, 'height_c': 800, 'height_l': '1024', 'height_m': '500', 'height_n': 320, 'height_o': '1080', 'height_q': '150', 'height_s': '240', 'height_sq': 75, 'height_t': '100', 'height_z': '640', 'id': '23890667535', 'isfamily': 0, 'isfriend': 0, 'ispublic': 1, 'owner': '58844113@N02', 'secret': '2a4d6d2d9e', 'server': '744', 'title': 'This mine now. Go sleep with the dog. #dailyozkitty ' '#oldman #tigertom', 'url_c': 'https://farm1.staticflickr.com/744/23890667535_2a4d6d2d9e_c.jpg', 'url_l': 'https://farm1.staticflickr.com/744/23890667535_2a4d6d2d9e_b.jpg', 'url_m': 'https://farm1.staticflickr.com/744/23890667535_2a4d6d2d9e.jpg', 'url_n': 'https://farm1.staticflickr.com/744/23890667535_2a4d6d2d9e_n.jpg', 'url_o': 'https://farm1.staticflickr.com/744/23890667535_ec66e73bfc_o.jpg', 'url_q': 'https://farm1.staticflickr.com/744/23890667535_2a4d6d2d9e_q.jpg', 'url_s': 'https://farm1.staticflickr.com/744/23890667535_2a4d6d2d9e_m.jpg', 'url_sq': 'https://farm1.staticflickr.com/744/23890667535_2a4d6d2d9e_s.jpg', 'url_t': 'https://farm1.staticflickr.com/744/23890667535_2a4d6d2d9e_t.jpg', 'url_z': 'https://farm1.staticflickr.com/744/23890667535_2a4d6d2d9e_z.jpg', 'width_c': '800', 'width_l': '1024', 'width_m': '500', 'width_n': '320', 'width_o': '1080', 'width_q': '150', 'width_s': '240', 'width_sq': 75, 'width_t': '100', 'width_z': '640'}, { ... } ], 'total': '325388'}
Explanation
If you examine the Flickr API Endpoints, you will see the API methods are listed in the form
flickr.galleries.addPhoto
flickr.galleries.create
flickr.galleries.editMeta
The flickrapi
library we're using allows us to follow the exact same format.
So for the search API endpoint flickr.photos.search
, the method we use
for the Python library is photos.search
.
The extras
argument is actually what's in charge of instructing the Flickr API
to provide the URLs. The Flickr documentation itself does not describe what the
values of the extras parameter actually mean. The only information provided is
Extras: A comma-delimited list of extra information to fetch for each returned record. Currently supported fields are: description, license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media, path_alias, url_sq, url_t, url_s, url_q, url_m, url_n, url_z, url_c, url_l, url_o
However, this Flickcurl C library has documented what these parameters mean.
url_c
: URL of medium 800, 800 on longest size imageurl_m
: URL of small, medium size imageurl_n
: URL of small, 320 on longest side size imageurl_o
: URL of original size imageurl_q
: URL of large square 150x150 size imageurl_s
: URL of small suqare 75x75 size imageurl_sq
: URL of square size imageurl_t
: URL of thumbnail, 100 on longest side size image
If you add the url_m
option in the extra parameter, which represents
small/medium sized images, you will receive back the following in the response:
- The direct url of the image (
url_m
) - The height of the image (
height_m
) - The width of the image (
width_m
)