Skip to content

Instantly share code, notes, and snippets.

@peterc
Last active March 30, 2018 09:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peterc/0db939d68d1004351e334c3714bfa446 to your computer and use it in GitHub Desktop.
Save peterc/0db939d68d1004351e334c3714bfa446 to your computer and use it in GitHub Desktop.
Instagram post liker
# igliker.rb - An Instagram mass list liker by pet'rc
# ---------------------------------------------------
#
# Takes a list of accounts and likes the latest post
# on each by remote controlling a Firefox instance.
#
# This is quite technical to use and requires you have Ruby
# installed (such as on macOS), as well as Firefox, and
# be happy using the terminal.
#
# To install:
# - Load a terminal emulator
# - Run 'ruby -v'
# .. if you get a response like:
# "ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin15]"
# .. you're good to go!
# - Install Firefox
# - Install Geckodriver
# .. get the latest one for your OS at
# https://github.com/mozilla/geckodriver/releases
# .. unarchive and put 'geckodriver' file into
# /usr/local/bin/ or /usr/bin/
# - Put THIS file into a directory and call it 'igliker.rb'
# - gem install selenium-webdriver dotenv
# .. if you get a permissions error, then try:
# sudo gem install selenium-webdriver dotenv
# .. and put your password in to install as admin.
#
# To run, put a list of IG usernames into names.txt, then:
# IG_USERNAME=yourusername IG_PASSWORD=yourpassword ruby igliker.rb names.txt
#
# OR..
# In .env file put your Instagram credentials:
# IG_USERNAME=yourusername
# IG_PASSWORD=yourpassword
#
# Then run:
# ruby igliker.rb names.txt
require 'selenium-webdriver'
begin
require 'dotenv'
Dotenv.load
rescue
end
DRIVER = Selenium::WebDriver.for :firefox
def wait_for_page_to_load
Selenium::WebDriver::Wait.new(:timeout => 10).until do
DRIVER.execute_script("return document.readyState;") == "complete"
end
sleep 0.5
end
# READ NAMES OF ACCOUNTS TO LIKE PHOTOS ON
names = File.readlines(ARGV.first || "names.txt").map { |n| n.strip.downcase.sub(/^\@/, '') }.uniq
STDERR.puts "#{names.size} accounts to like"
STDERR.puts "Logging in to @#{ENV['IG_USERNAME']}"
# LOG INTO INSTAGRAM
DRIVER.navigate.to "https://www.instagram.com/"
wait_for_page_to_load
DRIVER.find_element(:link, "Log in").click
element = DRIVER.find_element(:name, 'username')
element.send_keys ENV['IG_USERNAME']
element = DRIVER.find_element(:name, 'password')
element.send_keys ENV['IG_PASSWORD']
sleep 1 + rand(3)
DRIVER.find_element(:tag_name, "button").click
sleep 0.5
wait_for_page_to_load
# GO THROUGH EACH ACCOUNT
names.each.with_index do |name, i|
STDERR.print "#{sprintf("%03d/%03d", i + 1, names.size)} @#{name}".ljust(30)
# LOAD PROFILE PAGE
DRIVER.navigate.to "https://www.instagram.com/#{name}/"
wait_for_page_to_load
# CLICK ON PHOTO AND LIKE IT
begin
element = DRIVER.find_element(:css => 'article div div div a')
rescue
STDERR.puts " DIDN'T LOAD - SKIPPING @#{name}"
next
end
STDERR.print "clicking photo.."
element.click
sleep 0.25
STDERR.print " again.."
element.click
wait_for_page_to_load
sleep rand(2)
STDERR.print " hearting.."
begin
element = DRIVER.find_element(:class, 'coreSpriteHeartOpen').click
STDERR.puts " DONE!"
rescue
STDERR.puts " FAIL! Already liked?"
end
# Put in a random sleep to look more natural
sleep rand(2) + 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment