[PYTHON] Hit the Web API using requests Example: Flickr

You can easily hit the Web API using Python requests. This time, I will search on Flickr as an example.

Install Since it is registered on PyPi, install it using pip or easy_install.

$ pip install requests

or

$ easy_install -U requests

Get API Key

If you don't have a Flickr account, get one as needed.

Access the following address, register the application, and obtain the API Key and SECRET. For non-commercial registration, register with Non-Commercial. https://www.flickr.com/services/apps/create/apply/

Search on Flickr

Sample code

Once you have the API key and Secret, you can get the data in JSON format by setting some parameters as shown below and GET with requests.

# -*- coding: utf-8 -*-

import json
import requests

url = 'https://api.flickr.com/services/rest/'
API_KEY = 'YOUR_API_KEY'
SECRET_KEY = 'YOUR_SECRET_KEY'


query = {
        'method': 'flickr.photos.search',
        'api_key': API_KEY,
        'text': 'sky',  #Search word
        'per_page': '5',  #Number of data per page
        'format': 'json',
        'nojsoncallback': '1'
        }

r = requests.get(url, params=query)

print r
print json.dumps(r.json(), sort_keys=True, indent=2)

See below for configurable parameters on flickr.photos.search flickr.photos.search

response

The response of the above program is as follows (Response data does not always match because it depends on the Flickr situation)

<Response [200]>
{
  "photos": {
    "page": 1,
    "pages": 13568371,
    "perpage": 2,
    "photo": [
      {
        "farm": 6,
        "id": "14914864943",
        "isfamily": 0,
        "isfriend": 0,
        "ispublic": 1,
        "owner": "124218704@N03",
        "secret": "593dc8728a",
        "server": "5606",
        "title": "P1010144"
      },
      {
        "farm": 6,
        "id": "15349495280",
        "isfamily": 0,
        "isfriend": 0,
        "ispublic": 1,
        "owner": "24213796@N02",
        "secret": "ef0aa50b3f",
        "server": "5603",
        "title": "Hot Air Balloon"
      }
    ],
    "total": "27136741"
  },
  "stat": "ok"
}

Image acquisition

The actual image can be obtained by creating the following format URL with the parameters obtained in the response below.

http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg

reference

Search photos using Flickr API in Python

Recommended Posts

Hit the Web API using requests Example: Flickr
OAuth authentication using requests Example: Flickr
Hit the web API in Python
Try using the Twitter API
Try using the Twitter API
Try using the PeeringDB 2.0 API
View the contents of the queue using the RabbitMQ Management Web API
Image Optimize on the server side using TinyPNG's Web API
Proxy measures when using WEB API
Hit the Sesami API in Python
[Python] Hit the Google Translation API
Hit the Etherpad-lite API with Python
Use the Flickr API from Python
I tried using the checkio API
Access the Docker Remote API with Requests
Try using the Wunderlist API in Python
Tweet using the Twitter API in Python
Hit a method of a class instance with the Python Bottle Web API
Create an application using the Spotify API
Play with puns using the COTOHA API
Record custom events using the Shotgun API
I tried using the BigQuery Storage API
I made a LINE BOT that returns a terrorist image using the Flickr API
I checked the library for using the Gracenote API
Try using the BitFlyer Ligntning API in Python
Get image URL using Flickr API in Python
I tried using the Google Cloud Vision API
Try using the Python web framework Tornado Part 1
Try using the Python web framework Tornado Part 2
Hit the Firebase Dynamic Links API in Python
Try using the DropBox Core API in Python
Hit Zabbix API (1.8, 2.0)
Hit the Twitter API after Oauth authentication with Django
Initial settings when using the foursquare API in python
Send and receive Gmail via the Gmail API using Python
Register a ticket with redmine API using python requests
PHP and Python samples that hit the ChatWork API
Write data to KINTONE using the Python requests module
Post to your account using the API on Twitter
Learning neural networks using Chainer-Creating a Web API server
I tried using the API of the salmon data project
Using the National Diet Library Search API in Python
A little bit from Python using the Jenkins API