Download all the photos you've liked on Instagram

Update

I took what I learned from writing this script and wrapped it up in a web app that will keep track of your likes for you. Please email me if you have any feedback.

In addition to using the ‘users/self/media/liked/’ endpoint shown below, any paged endpoint from the api can be used in the for the starting ‘media_url’.

Adam Duke | 06 April 2013

I recently discovered that Instagram will only ever return the last ~300 likes when trying to view the history of the things you’ve liked. I’m not sure if I’m the only one, but finding that out bothered me. I use the like feature to bookmark photos that I want to check out later, as well as letting someone know that I like their photo. I decided to build something that would help me keep track of my likes for the long term.

I broke my effort into a few parts:

Learning how to work with the API

Getting the correct data from Instagram

Parsing the data

Saving the photos

Learning how to work with the API

The Instagram API requires a client_id parameter in most cases, and an access_token parameter in cases where the request is on behalf of a user such as commenting or liking.

A quick check of the documentation at instagram.com shows that there is an API endpoint that will return a list of a signed-in user’s likes. The documentation for that endpoint is here.

Instagram considers the list of a user’s likes to be private, so it will require the additional access_token parameter. The first step is getting a client_id by registering a new application http://instagram.com/developer/clients/manage/. Once you have registered your application, you should see something similar to below. (Note that the application has been deleted so there’s no worry about leaking secrets/tokens)

The second step is getting a valid access_token that can be used to make authenticated API calls.

where CLIENT-ID is the client id you got when registering your new application, and REDIRECT-URI is the redirect uri that you provided when registering your new application. Navigating to that URL will yield a form where you can log into your Instagram account. Upon submitting the form, you will be redirected to the REDIRECT-URI and a valid access token will be given in the form of a URI fragment.

ex.

Getting the correct data from Instagram

Now we have everything we need to get data back from instagram. Let’s create a ruby script so we can keep track of the steps we’re running and make them repeatable. Create a file named ig_downloader.rb and we’ll build from there.

One convenient way to make http requests in ruby is the RestClient library. Try adding the following to your ig_downloader.rb.

Parsing the data

This request above will return a json response. The response will have the “data” key, and its value is the array of likes that we are interested in. If we trim one of the likes down to the keys that will be useful for us, we’re left with the following:

The code above iterates each url and uses RestClient to download the image. It then creates a path that points to the current directory, and writes the image out to a file in the current directory.

At this point, the script should run and download the first page of images from Instagram’s API. In order to download the rest of the pages we need to revisit the json data that comes back in the initial response.

In addition to the “data” key, there is a top level key named “pagination”, which contains the information we need to get subsequent pages from the API.

Let’s modify the script we started earlier to gather all the urls from all the available pages before downloading the images. We’ll set an initial url and use a while loop to fetch it from the API, update the url, and fetch subsequent pages until there aren’t any more. Note: I also added a short sleep during each call to the API because I experienced some http errors during development and slowing the request rate seemed to help.

There are a number of other optimizations/modifications that can be added including not downloading images that we already have, saving the url for the image, etc… A more polished version of the script including some small extras can be found in this gist.

If you find any bugs or have any feedback at all feel free to send me an email.

Contact us

We are always interested talking to developers and learning about what you're working on.