A quick look at the World Cup final through Instagram

The 2014 World Cup will go down as one of the best in recent history. It featured countless headlines, from Germany asserting themselves as the top team in the world by humiliating the entire nation of Brazil 7-1 on their own turf and eventually winning it all. To the end of Spanish world soccer dominance and Luis Suarez continuing with his animalistic tendencies. And lets not forget James Rodriguez making sure he’s a household name with this gem:

I had wanted to do a project using the Instagram API for a while and thought the world cup final would be great place to start. I hacked together a quick python script to search the Instagram API for a specific term and save the images into a local directory.

import os
import json
import urllib
import urllib2
from instagram.client import InstagramAPI


INSTAGRAM_CLIENT_ID = ''
INSTAGRAM_CLIENT_SECRET = ''

api = InstagramAPI(client_id=INSTAGRAM_CLIENT_ID,client_secret=INSTAGRAM_CLIENT_SECRET)

def find_images(query, path):
    recent = api.tag_recent_media(1,100,query)
    link = recent[1]
    response = urllib2.urlopen(link)
    ig = json.load(response) 
    status=ig['meta']['code']

    while status == 200:
        data = ig['data']
        if not data:
            link = ig['pagination']['next_url']
            response = urllib2.urlopen(link)
            ig = json.load(response)
        else:
            image = ig['data'][0]['images']['standard_resolution']['url']
            unique = str(ig['data'][0]['created_time'])
            str(image)
            urllib.urlretrieve(image, os.path.join(path, '%s.jpg') % unique )
            link = ig['pagination']['next_url']
            response = urllib2.urlopen(link)
            ig = json.load(response)

After you enter your credentials and run the function you will see your directory begin to fill with images. I ran the script for a few minutes (you will get rate limited pretty quickly) during the game and was able to gather around six hundred images. I created a quick collage with a few below. As you can see it consists of mostly TVs, Messi and German flags.

This was a quick and easy project which gave me quite a few ideas for the future. It would be interesting to go through each image individually and analyze them for say most prominent colors. It would also be neat to build a machine learning pipeline to autotag the pictures into categories such as people, landscapes, cars, etc. I would also like to take a look at the real time streaming API and build some sort of web front end on top of it.

In the end I learned quite a bit with this and look forward to using Instagrams API again. If you have any questions, feedback, advice, or corrections please get in touch with me on Twitter or email me at danforsyth1@gmail.com.