Skip to content

Instantly share code, notes, and snippets.

@dylansm
Created July 28, 2011 18:19
Show Gist options
  • Save dylansm/1112159 to your computer and use it in GitHub Desktop.
Save dylansm/1112159 to your computer and use it in GitHub Desktop.
Ruby Instagram Fetcher
require "rubygems"
require "json"
require "uri"
require "net/https"
class JSONError < StandardError; end
class InstagramFetcher
ROOT_CA = "/etc/ssl/certs"
HOST = "https://api.instagram.com/v1/users/self/media/recent?access_token="
attr_reader :response
def initialize
@access_tokens = {
:user1 => "101653xxxxxxxxxxxx.xxxxxxxxde40bf46df",
:user2 => "109879xxxxxxxxxxxx.xxxxxxxxc865e5f975"
}
end
def execute
access_tokens.each do |name, token|
response = fetch(token)
begin
json_response = response.body.to_json
(`echo $HOME` =~ /dev_environment/) ? working_dir = '' : working_dir = '/home/user/production_env/instagram/'
File.open("#{working_dir}#{name}_json.js", "w") do |f|
f.write(json_response.strip)
end
rescue JSONError => e
File.open("error_log", "w") do |f|
f.write("#{Time.now} #{e.message}")
end
end
end
end
def fetch(token)
url = URI.parse "#{HOST}#{token}"
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
if File.directory? ROOT_CA
http.ca_path = ROOT_CA
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.verify_depth = 5
else
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new("#{url.path}?#{url.query}")
#request.basic_auth url.user, url.password
http.request(request)
end
private
attr_reader :access_tokens
end
i = InstagramFetcher.new
i.execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment