Get image URL using Flickr API in Python

Thing you want to do

I want to get images related to search words from Flickr.

Advance preparation

--Create a Flickr account and get an API key.

Development environment

--python 2.7.x series

Requirement

pip install poster 

program

I also posted it on GitHub. → getImageFromFlickr.py

getImageFromFlickr.py


#coding: utf-8
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import json
import sys
import urllib2


def getImageUrlFromFlickr(API_KEY, query, N):

    NUM_OF_PHOTO = str(N) #Number of image URLs to get
    option = '&sort=relevance&privacy_filter=1&content_type=1&per_page='+ NUM_OF_PHOTO +'&format=json&nojsoncallback=1'
    url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key='+ API_KEY + option
    
    #Get results in JSON format
    register_openers()
    datagen, headers = multipart_encode({'text': query})
    request = urllib2.Request(url,datagen, headers)
    response = urllib2.urlopen(request)
    res_dat = response.read()
    
    url_list = [] #URL list
    template_url = 'https://farm%s.staticflickr.com/%s/%s_%s.jpg' #URL template
    for i in json.loads(res_dat)['photos']['photo']:
        img_url = template_url % (i['farm'],i['server'],i['id'],i['secret'])
        url_list.append(img_url) #Add image URL to list

    return url_list

if __name__ == '__main__':

    API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'

    query = sys.argv[1]
    #API KEY,Query,Pass the number of acquired images
    url_list = getImageUrlFromFlickr(API_KEY, query, 10)
    
    #Display the acquired image URL
    for url in url_list:
        print url

Execution result

>python getImageFromFlickr.py sushi
https://farm6.staticflickr.com/5043/5203889493_b5a2feacf2.jpg
https://farm5.staticflickr.com/4021/4247841124_fd2f32222e.jpg
https://farm5.staticflickr.com/4082/4801051243_b439902670.jpg
https://farm4.staticflickr.com/3384/3472202216_090d54fb52.jpg
https://farm3.staticflickr.com/2265/2505551269_78f4cb8650.jpg
https://farm6.staticflickr.com/5296/5479083865_4e6ffa9553.jpg
https://farm3.staticflickr.com/2609/4008704322_b2c9a9895f.jpg
https://farm5.staticflickr.com/4117/4801046373_3e492b224d.jpg
https://farm5.staticflickr.com/4119/4801043517_d3bf9e38b9.jpg
https://farm5.staticflickr.com/4087/5025254420_131c78cb35.jpg

Acquired image

https://farm6.staticflickr.com/5043/5203889493_b5a2feacf2.jpg alt

Recommended Posts

Get image URL using Flickr API in Python
Get Youtube data in Python using Youtube Data API
Get LEAD data using Marketo's REST API in Python
Mouse operation using Windows API in Python
Try using the Wunderlist API in Python
Get Suica balance in Python (using libpafe)
Try using the Kraken API in Python
Get mail using Gmail API in Java
Tweet using the Twitter API in Python
Get Google Fit API data in Python
[Python] Get all comments using Youtube Data API
Try using the BitFlyer Ligntning API in Python
Evernote API in Python
Image format in Python
Let's judge emotions using Emotion API in Python
Get date in Python
C API in Python 3
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Upload JPG file using Google Drive API in Python
Initial settings when using the foursquare API in python
[Python] Get insight data using Google My Business API
OpenVINO using Inference Engine Python API in PC environment
Using the National Diet Library Search API in Python
Get the URL of the HTTP redirect destination in Python
Hit Mastodon's API in Python
Get YouTube Comments in Python
Image download with Flickr API
Get last month in python
Tweet with image in Python
Get Terminal size in Python
Explicitly get EOF in python
Image Processing Collection in Python
Blender Python API in Houdini (Python 3)
Get Evernote notes in Python
Relative url handling in python
Translate using googletrans in Python
Using Python mode in Processing
Get Japanese synonyms in Python
Get your heart rate from the fitbit API in Python!
Predict gender from name using Gender API and Pykakasi in Python
Get Python scripts to run quickly in Cloud Run using responder
Call github api in python to get pull request information
Get Leap Motion data in Python.
GUI programming in Python using Appjar
Get Salesforce data using REST API
Getting the arXiv API in Python
Image capture of firefox using python
[Python] Using OpenCV with Python (Image Filtering)
Precautions when using pit in Python
Data acquisition using python googlemap api
Implemented image segmentation in python (Union-Find)
Get data from Quandl in Python
Get reviews with python googlemap api
[Python] Using OpenCV with Python (Image transformation)
[Python3] Google translate google translate without using api
Get the desktop path in Python
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Get the script path in Python
Create Gmail in Python without API