Search code examples
rubyjsoninstagram-apidashing

Customize Instagram widget on a Dashing.io dashboard


I have set up a dashboard using dashing with a number of (mostly) existing widgets. That worked so far - see production dashboard here (work in progress).

Now I would like to have an Instagram widget that displays the n lastest images taken by username. I have found a widget that will display images by long and lat and also was able to get my tokens configured, so I can talk to the Instagram API.

Here's the code of my current widget originally from @mjamieson's gist on github.

require 'instagram'
require 'rest-client'
require 'json'

# Instagram Client ID from http://instagram.com/developer
Instagram.configure do |config|
  config.client_id = ENV['INSTAGRAM_CLIENT_ID']
  config.client_secret = ENV['INSTAGRAM_CLIENT_SECRET']
end

# Latitude, Longitude for location
instadash_location_lat = '45.429522'
instadash_location_long = '-75.689613'


SCHEDULER.every '10m', :first_in => 0 do |job|
  photos = Instagram.media_search(instadash_location_lat,instadash_location_long)
  if photos
    photos.map do |photo|
      { photo: "#{photo.images.low_resolution.url}" }
    end
  end
  send_event('instadash', photos: photos)
end

I got this to work, but would like to modify the given API call to only display images taken by me / a user of my choice. Unfortunately I don't understand ruby or json enough to figure out what the Instagram API documentation wants me to do.

I found the following url

https://api.instagram.com/v1/users/{user-id}/media/recent/?access_token={acces-token}

and tried it (with my credentials filled in). It returned json data correctly including my images (among other data).

How can I modify the given code to display images by username instead of location?

Any help is greatly appreciated.


Solution

  • Solution

    require 'sinatra'
    require 'instagram'
    
    # Instagram Client ID from http://instagram.com/developer
    Instagram.configure do |config|
      config.client_id = ENV['INSTAGRAM_CLIENT_ID']
      config.client_secret = ENV['INSTAGRAM_CLIENT_SECRET']
      config.access_token = ENV['INSTAGRAM_ACCESS_TOKEN']
    end
    
    user_id = ENV['INSTAGRAM_USER_ID']
    
    SCHEDULER.every '2m', :first_in => 0 do |job|
      photos = Instagram.user_recent_media("#{user_id}")
      if photos
        photos.map! do |photo|
          { photo: "#{photo.images.low_resolution.url}" }
        end
      end
      send_event('instadash', photos: photos)
    end
    

    Explaination

    1.) In addition to the client_id and client_secret I had defined before, I just needed to add my access_token to the Instagram.configure section.

    2.) The SCHEDULER was correctly working, but needed to call Instagram.user_recent_media("#{user_id}") instead of Instagram.media_search(instadash_location_lat,instadash_location_long)

    3.) To do that I had to set a second missing variable for user_id

    Now the call gets recent media filtered by user ID and outputs it into the dashing widget.

    Thanks for the participation and hints! That pointed me into the right direction of the documentation and helped me to figure it out myself.