Last active
August 29, 2015 14:23
-
-
Save sts10/2e52623b95b3e856d6e1 to your computer and use it in GitHub Desktop.
Count number of Instagram posts in last 24 hours from a given user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "sinatra" | |
require "instagram" | |
enable :sessions | |
CALLBACK_URL = "http://localhost:4567/oauth/callback" | |
Instagram.configure do |config| | |
config.client_id = "CLIENT_ID_THAT_YOU_HAVE_TO_GIVE_YOUR_PHONE_NUMBER_TO_GET_BUT_AFTER_THAT_IT_WAS_BASICALLY_INSTANT" | |
config.client_secret = "CLIENT_SECRET" | |
# For secured endpoints only | |
#config.client_ips = '<Comma separated list of IPs>' | |
end | |
get "/" do | |
'<a href="/oauth/connect">Connect with Instagram</a>' | |
end | |
get "/oauth/connect" do | |
redirect Instagram.authorize_url(:redirect_uri => CALLBACK_URL) | |
end | |
get "/oauth/callback" do | |
response = Instagram.get_access_token(params[:code], :redirect_uri => CALLBACK_URL) | |
session[:access_token] = response.access_token | |
redirect "/nav" | |
end | |
get "/nav" do | |
client = Instagram.client(:access_token => session[:access_token]) | |
# user = client.user_search("USERNAME") # To find your desired user's id | |
media_search = client.user_recent_media(USER_ID) | |
last_twenty_four = [] | |
media_search.each do |post| | |
puts "this one is #{post.created_time.to_i} vs #{(Time.now - 60*60*24*1).to_i}" | |
puts post.link | |
if post.created_time.to_i > (Time.now - 60*60*24*1).to_i | |
last_twenty_four << post | |
end | |
end | |
posted_videos = [] | |
last_twenty_four.each do |post| | |
if post.type == "video" | |
posted_videos << post | |
end | |
end | |
posts_count = last_twenty_four.count | |
video_posts_counts = posted_videos.count | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment