DEV Community

loading...

Download all Flickr photos of a user with only 20 lines of Python 🐍

louisbertin profile image Louis Bertin ・1 min read

Introduction

Today, a friend wanted to download all photos of a photograph he likes a lot on Flickr.. but that's a long and a boring task.. especially when the user has many photos.

So, I just asking myself how I can automate this task.. and Python is the answer!

Prerequisite

  • Python and Pip (the higher is better!)
  • venv : optional, but I advise you to use it.

Let's jump into the code!

  • At the root of your working directory run the following command to install flickr_api as a dependency
pip install flickr_api
  • Next you have to create an account on Flickr and log you in to get your api_key and your api_secret with the following link
  • Then use this code and replace API_KEY, API_SECRET and USER_NAME with your credentials 🙂
import os
import flickr_api

flickr_api.set_keys(api_key = 'API_KEY', api_secret = 'API_SECRET')
os.makedirs(os.path.join(os.getcwd(), "photos"), exist_ok=True)

user = flickr_api.Person.findByUserName("USER_NAME")
pages_nb = user.getPublicPhotos().info.pages
total = user.getPublicPhotos().info.total
current = 0

for page_nb in range(1, pages_nb+1):
    for index, photo in enumerate(user.getPublicPhotos(page=page_nb)):
        sizes = photo.getSizes()
        biggest_size = list(sizes.keys())[-1]
        filename = photo.title.replace("/", "-") + "_" + photo.id

        current += 1
        try:
            print(f"{current}/{total}", filename)
            photo.save(os.path.join(os.getcwd(), "photos", filename), size_label = biggest_size)
        except Exception as e:
            print(e)

Discussion (0)

pic
Editor guide