Skip to content

Instantly share code, notes, and snippets.

@cameronbarker
Last active August 29, 2015 14:08
Show Gist options
  • Save cameronbarker/54626f95cba5ffa4e78b to your computer and use it in GitHub Desktop.
Save cameronbarker/54626f95cba5ffa4e78b to your computer and use it in GitHub Desktop.
require "curb"
require "json"
=begin
This snippet will allow a user to like the latest 100 images on a hashtag or on their feed.
Instagram internally limits to 100 likes/hour.
To run copy code to a file named instagram_like.rb
In your terminal move to where that code is and run 'ruby instagram_like.rb'
Trouble Shoot:
If you have an issue with curb please run the below command in terminal with in the folder
where the instagram_like.rb file is.
'sudo gem install curb'
=end
class InstagramLike
# attr_reader :liked_data
def initialize(token, hashtag=nil)
@liked_data = []
@access_token = token
@hashtag = hashtag
get_likes
end
#like images
def set_likes
feed_data = []
#build url for hashtags or for your own feed
if @hashtag
url = "https://api.instagram.com/v1/tags/#{@hashtag}/media/recent?access_token=#{@access_token}"
else
url = "https://api.instagram.com/v1/users/self/feed?access_token=#{@access_token}"
end
raw_feed = Curl.get(url)
parsed_feed = JSON.parse(raw_feed.body_str)
feed_data << parsed_feed["data"]
while !parsed_feed["pagination"].nil? and !parsed_feed["pagination"]["next_url"].nil?
puts parsed_feed["pagination"]["next_url"]
raw_feed = Curl.get(parsed_feed["pagination"]["next_url"])
parsed_feed = JSON.parse(raw_feed.body_str)
feed_data << parsed_feed["data"]
end
ids = feed_data.flatten.map{ |t| t["id"] }
ids.each do |id|
unless @liked_data.include? id
post = Curl.post("https://api.instagram.com/v1/media/#{id}/likes?access_token=#{@access_token}")
#429 status means you've reached your instagram limit for the hour
if post.status.include? "429"
puts "You have reached your limit for this hour"
puts "Try back soon."
break
else
puts "You like it, you really like it."
#sleep helps instagram think you're real
#this might be a lie
sleep 2
end
else
puts "Already liked that one."
end
end
end
#get all the likes in the past hour
#takes around 11 seconds
def get_likes
puts "GET LATEST LIKED DATA"
liked_data = []
raw_likes = Curl.get("https://api.instagram.com/v1/users/self/media/liked?access_token=#{@access_token}")
parsed_likes = JSON.parse(raw_likes.body_str)
liked_data << parsed_likes["data"]
while !parsed_likes["pagination"].nil? and !parsed_likes["pagination"]["next_url"].nil?
puts parsed_likes["pagination"]["next_url"]
raw_likes = Curl.get(parsed_likes["pagination"]["next_url"])
parsed_likes = JSON.parse(raw_likes.body_str)
liked_data << parsed_likes["data"]
end
@liked_data = liked_data.flatten.map{ |t| t["id"] }
end
end
#
# Console code
#
puts "----------------------------------"
puts "Welcome to Instagram like automater!"
puts "This process takes roughly 30 seconds and can be run hourly to like the latest 100 posts."
puts "You will need an auth token from instagram, follow the steps found below to aquire it."
puts "Provide LINK to INSTAGRAM AUTH"
puts "-----------------------------------"
print "Please input your auth token: "
auth_token = gets.strip()
print "Please input a hash tag or type feed to like your own feed: "
hash_tag = gets.strip()
if hash_tag.downcase == "feed"
inst = InstagramLike.new(auth_token)
inst.set_likes
else
inst = InstagramLike.new(auth_token, hash_tag)
inst.set_likes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment