DEV Community

Louis Bertin
Louis Bertin

Posted on

4 1

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

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)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay