Skip to content

Instantly share code, notes, and snippets.

@shakirmengrani
Last active July 13, 2020 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakirmengrani/d6efc0b1b7844e20dc84c96ff719ba48 to your computer and use it in GitHub Desktop.
Save shakirmengrani/d6efc0b1b7844e20dc84c96ff719ba48 to your computer and use it in GitHub Desktop.
from selenium import webdriver
class InstaBot(object):
def __init__(self):
self.postId = []
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(5)
self.browser.get("https://instagram.com")
def delayByUrl(self, url, debug=False):
while self.browser.current_url != url:
if debug:
print(self.browser.current_url)
pass
def pgUp(self):
webdriver.common.action_chains.ActionChains(self.browser).send_keys(webdriver.common.keys.Keys.PAGE_UP)
def pgDown(self):
webdriver.common.action_chains.ActionChains(self.browser).send_keys(webdriver.common.keys.Keys.PAGE_DOWN)
def clickByText(self, text):
button = self.browser.find_element_by_xpath("//button[text()='{}']".format(text))
button.click()
def login(self, username, password):
username_input = self.browser.find_element_by_css_selector("input[name='username']")
password_input = self.browser.find_element_by_css_selector("input[name='password']")
login_button = self.browser.find_element_by_css_selector("button[type='submit']")
username_input.send_keys(username)
password_input.send_keys(password)
login_button.click()
def getAllPosts(self, refresh=False):
posts = self.browser.find_elements_by_tag_name("article")
print(posts)
if posts is None:
posts = []
if len(posts) > 0:
return posts
if refresh:
self.browser.refresh()
self.getAllPosts()
def getPostButtons(self, post):
actionSection = post.find_elements_by_tag_name("section")
buttons = actionSection[0].find_elements_by_tag_name("button")
print("Sections: {}, Buttons: {}".format(len(actionSection), len(buttons)))
return buttons
def setPostLike (self, posts):
for item in posts:
if item.id not in self.postId:
print("post id: {}".format(item.id))
buttons = self.getPostButtons(item)
print("Buttons: {}".format(len(buttons)))
likeButton = buttons[0]
icon = likeButton.find_elements_by_tag_name("svg")
if icon[0].get_attribute("fill") != "#ed4956":
likeButton.click()
self.postId.append(item.id)
from time import sleep
from bot import InstaBot
bot = InstaBot()
bot.login("lazy_engineer_92", "*************")
bot.delayByUrl("https://www.instagram.com/accounts/onetap/?next=%2F")
sleep(2)
bot.clickByText("Not Now")
bot.delayByUrl("https://www.instagram.com/")
sleep(1)
bot.clickByText("Not Now")
posts = bot.getAllPosts(refresh=True)
print("Posts: {}".format(len(posts)))
bot.setPostLike(posts)
sleep(5)
while True:
bot.pgDown()
sleep(30)
posts = bot.getAllPosts()
bot.setPostLike(posts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment