Introduction: Retrieve Recent Instagram Images From Geographic Locations

These are instructions for making a webpage that displays recent Instagram images taken (and geo-tagged) near a specified latitude and longitude. The location used in this example is the campus of UCLA (lat: 34.071; long: -118.445).

These instructions assume you have a working computer with ruby (version 1.9.2) and the following ruby gems installed:

instagram (0.8.4)
sinatra (1.3.2)

See here for instructions on setting up ruby gems: http://docs.rubygems.org/read/chapter/1

Step 1: Register a New Client With Instagram

- If you do not have an Instagram account, you need to create one. 
- In order to use the Instagram API, you need to register a new software client, here:  http://instagr.am/developer/clients/register/
- the application name and description can be whatever you like. use "http://localhost" for the website and the OAuth redirect_url.
- when you have successfully registered your client, you will be given a client_id and and a client_secret. You will need to use these in the steps below.

Step 2: The Code

cut and paste the following code into a text file called "ucla.rb". Add the client id and client secret that you received from the instagram where indicated. 

#-------- begin ucla.rb --------
require "rubygems"
require "sinatra"
require "instagram"

enable :sessions

# set these to correspond with your desired location
lat = "34.075"
lng = "-118.443"

Instagram.configure do |config|
  config.client_id = "INSERT CLIENT ID HERE"
  config.client_secret = "INSERT CLIENT SECRET HERE"
end

get "/" do
  client = Instagram.client()
  location = client.location_search(lat,lng, :distance => 5000, :count => 80)
  html = ""
  location.each do |l|
    puts
    html << "<p><b>#{l.name}</b><br/>"
    media = client.location_recent_media(l.id,:count=>10)
    media.data.each do |m|
      html << "<img src='#{m.images.thumbnail.url}'>"
    end
    html << "<hr/>"
    html << "</p>"
  end
  html
end

# ------- end ucla.rb -------- 

Step 3: Run It

from the command line (the Terminal app on OS X), type:

ruby ucla.rb

and hit enter.

see the webpage here: http://localhost:4567