Use the Flickr API from Python

Introduction

Overview

A memo when I made a Python script to read Flickr photos in Photos | PyCon JP 2014 in TOKYO. According to the table below

What you want to do Flickr API URI Flickr API arguments
Get a list of albums flickr.photosets.getList user_id
Get the photos in the album flickr.photosets.getPhotos photoset_id
Get the URL of the photo flickr.photos.getSizes photo_id
  1. Throw an HTTP request with requests
  2. Parse the returned XML with xml.dom.minidom

I'm just doing two things. I think it was a little surprising that flickr.photos.getSizes contains the URL of the photo for each size.

There were already some articles doing something similar. Unlike this article, it is helpful to get the result in JSON and directly configure the URL instead of flickr.photos.getSizes. If the URL configuration rule is open, it is wise to configure it yourself rather than hitting the API.

Premise

The prerequisites are as follows:

In addition, the following article will be helpful for the introduction and usage of requests.

The following article will be helpful for getting the API key for Flickr.

code

At first I did it with Python2 and it didn't work, so I'm taking some first aid. It works with Python2 for the time being, but I may fix it later.

flickr.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import requests
import xml.dom.minidom as md

class Flickr(object):
    u"""
Basic usage

    #Please correct accordingly
    api_key = 'ENTER_YOUR_API_KEY'
    
    f = Flickr(api_key)

    #pyconjp user_id
    print(f.get_photoset_ids_from_user_id('102063383@N02'))
    #PyCon JP 2014 day4 Sprint photoset photoset_id
    print(f.get_photos_from_photoset_id('72157647739640505'))
    #One of the photos in the above photo set
    print(f.get_url_from_photo_id('15274792845'))
    """

    def __init__(self, api_key):
        self.api_url = 'https://api.flickr.com/services/rest/'
        self.api_key = api_key

    def get_photoset_ids_from_user_id(self, user_id):
        u"""
user(user_id)List of album ids owned by(photset_List of ids)return it
Not necessary for this purpose
        """
        #Send request
        r = requests.post(self.api_url, {'api_key': self.api_key,
                                         'method': 'flickr.photosets.getList',
                                         'user_id': user_id
                                         })        

        #Parse xml into a dom object
        dom = md.parseString(r.text.encode('utf-8'))

        #photoset from dom object_Find id
        result = []        
        for elem in dom.getElementsByTagName('photoset'):
            result.append(elem.getAttribute('id'))            
        return result

    def get_photos_from_photoset_id(self, photoset_id):
        u"""
Album id(photset_id)List of photos in(photo_List of ids)return it
        """
        #Send request
        r = requests.post(self.api_url, {'api_key': self.api_key,
                                         'method': 'flickr.photosets.getPhotos',
                                         'photoset_id': photoset_id
                                         })  

        #Parse xml into a dom object
        dom = md.parseString(r.text.encode('utf-8'))

        #photo from dom object_Find id
        result = []
        for elem in dom.getElementsByTagName('photo'):
            result.append(elem.getAttribute('id'))            
        return result

    def get_url_from_photo_id(self, photo_id):
        u"""
Photo(photo_id)Returns the URL that is actually stored
        """
        #Send request
        r = requests.post(self.api_url, {'api_key': self.api_key,
                                         'method': 'flickr.photos.getSizes',
                                         'photo_id': photo_id
                                         })        

        #Parse xml into a dom object
        dom = md.parseString(r.text.encode('utf-8'))

        #Find the URL from the dom object
        result = None
        for elem in dom.getElementsByTagName('size'):
            #Only the original size
            if elem.getAttribute('label') == 'Original':
                result = elem.getAttribute('source')
                #Think of the original as one and skip the others
                break
            else:
                #None if nothing
                pass
        return result

if __name__ == '__main__':
    #Operation check
    
    #Please correct accordingly
    api_key = 'ENTER_YOUR_API_KEY'

    f = Flickr(api_key)
    
    #pyconjp user_id
    print(f.get_photoset_ids_from_user_id('102063383@N02'))
    #PyCon JP 2014 day4 Sprint photoset photoset_id
    print(f.get_photos_from_photoset_id('72157647739640505'))
    #One of the photos in the above photo set
    print(f.get_url_from_photo_id('15274792845'))

Execution result

Code execution result

Rewrite'ENTER_YOUR_API_KEY'in main to your API key

python flickr.py

You can get the URL of the following photo by executing.

Example of use

To get the URLs in bulk, you can either import or copy flickr.py and do the following: Also here, change the ENTER_YOUR_API_KEY part to your own API key. Also, break is attached so that it does not take time when executing the test, so take break when actually executing.

If you don't want to specify targets, you can use get_photoset_ids_from_user_id to get all the albums of a specific user, and you may use that. This makes it look like crawling.

>>> from collections import defaultdict

>>> api_key = 'ENTER_YOUR_API_KEY'     #Please correct accordingly

>>> f = Flickr(api_key)

>>> targets = ['72157641092641983',    #PyCon JP 2014 preview- an album on Flickr
...            '72157647111767068',    # PyCon JP 2014 day1 Tutorial - an album on Flickr
...            '72157647184237569',    # PyCon JP 2014 day2 Conference - an album on Flickr
...            '72157647216509890',    # PyCon JP 2014 day3 Conference - an album on Flickr
...            '72157647739640505'     # PyCon JP 2014 day4 Sprint - an album on Flickr
...           ]

>>> d = {}
>>> for elem in targets:               #Create a list of photos in the photoset
...     d[elem] = f.get_photos_from_photoset_id(elem)
...     break    #take
    
>>> d2 = defaultdict(list)
>>> for k,v in d.items():              #Create url list of data
...     for elem in v:
...         d2[k].append(f.get_url_from_photo_id(elem))
...         break    #take
...     break    #take
    
>>> for k,v in d2.items():             #Get files using url list
...     if not os.path.exists(k):
...         os.mkdir(k)
...     for elem in v:
...         r = requests.get(elem)     #Get data from url
...         # photoset_id/file name.Save as jpg
...         with open("{0}/{1}".format(k, elem.split("/")[-1]), 'wb') as g:
...             g.write(r.content)

from now on

Recommended Posts

Use the Flickr API from Python
Use e-Stat API from Python
Use kabu StationĀ® API from Python
Use Google Analytics API from Python
Use Google Cloud Vision API from Python
Use thingsspeak from python
Use fluentd from python
Use MySQL from Python
Use MySQL from Python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
Use the nghttp2 Python module from Homebrew from pyenv's Python
Try accessing the YQL API directly from Python 3
Use Trello API with python
Use Twitter API with Python
Use MySQL from Anaconda (python)
Call the API with python3.
Use subsonic API with python3
[Python] Get the text of the law from the e-GOV Law API
I wanted to use the Python library from MATLAB
Let's use the Python version of the Confluence API module.
[Python] Use the Face API of Microsoft Cognitive Services
A little bit from Python using the Jenkins API
Use Stanford Core NLP from Python
Getting the arXiv API in Python
Get your heart rate from the fitbit API in Python!
Hit the Sesami API in Python
Existence from the viewpoint of Python
Read and use Python files from Python
Forcibly use Google Translate from python
Hit the Etherpad-lite API with Python
Hit the web API in Python
Use Azure Blob Storage from Python
Get upcoming weather from python weather api
Run Ansible from Python using API
Sakura Use Python on the Internet
Use fastText trained model from Python
Access the Twitter API in Python
[Python] How to use Typetalk API
Handle SOAP API from Python (Zeep)
[September 2020 version] Explains the procedure to use Gmail API with Python
You can also use virtualenv from the IntelliJ IDEA Python plugin
Let's touch Google's Vision API from Python for the time being
Collecting information from Twitter with Python (Twitter API)
[Python] Web application from 0! Hands-on (3) -API implementation-
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Use PostgreSQL data type (jsonb) from Python
Python: Reading JSON data from web API
Use machine learning APIs A3RT from Python
Learning notes from the beginning of Python 1
Tweet using the Twitter API in Python
I want to use jar from python
I tried using UnityCloudBuild API from Python
[Blender] Use OpenGL from inside the script
Launch the Python interpreter from Git bash
Use Django from a local Python script
From Python 3.4, pip becomes the standard installer! ??
Use C ++ functions from python with pybind11
Try hitting the YouTube API in Python
API explanation to touch mastodon from python