text: from time import sleep
  from selenium import webdriver
  from selenium.webdriver.firefox.options import Options
  import random
  
  
  class igbot:
  
      def __init__(self, headless, gecko_path=None):
          self.login_status = False
          self.notification_status = None
          self.followers = None
          self.following = None
          self.username = None
          if headless is True:
              self.options = Options()
              self.options.headless = True
              if gecko_path is None:
                  self.browser = webdriver.Firefox(options=self.options)
              else:
                  self.browser = webdriver.Firefox(options=self.options,
                                                   executable_path=r'{path}'.format(path=gecko_path))
          else:
              self.login_status = False
              self.notification_status = None
              if gecko_path is None:
                  self.browser = webdriver.Firefox()
              else:
                  self.browser = webdriver.Firefox(executable_path=r'{path}'.format(path=gecko_path))
  
      def login(self, username, password):
          self.browser.implicitly_wait(5)
          self.browser.get('https://www.instagram.com/')
          username_input = self.browser.find_element_by_css_selector("input[name='username']")
          password_input = self.browser.find_element_by_css_selector("input[name='password']")
          self.username = username
          username_input.send_keys(username)
          password_input.send_keys(password)
          login_button = self.browser.find_element_by_xpath("//button[@type='submit']")
          login_button.click()
          sleep(5)
          two_fact_status = self.two_factor_auth()
          sleep(5)
          self.login_status = self.login_confirmation()
          if self.login_status is True:
              print("Logged In!")
          else:
              print("Unable to Login!")
  
      def two_factor_auth(self):
          try:
              if self.browser.find_element_by_xpath("//div[text()='Enter the 6-digit code generated by your "
                                                    "authentication app.']"):
                  print("2FA Detected")
                  otp = str(input("Enter 2 factor Code - "))
                  otp_textfield = self.browser.find_element_by_xpath("//input[@aria-label='Security Code']")
                  otp_textfield.send_keys(otp)
                  otp_login_button = self.browser.find_element_by_xpath("//button[text()='Confirm']")
                  otp_login_button.click()
                  sleep(5)
                  try:
                      if self.browser.find_element_by_xpath(
                              "//p[text()='Please check the security code and try again.']"):
                          print("Wrong Code, Try again!")
                          self.two_factor_auth()
                      else:
                          return True
                  except:
                      return True
          except:
              print("2FA Not Detected")
              return False
  
      def logout(self):
          if self.login_status is True:
              sleep(3)
              self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=self.username))
              sleep(2)
              try:
                  settings_button = self.browser.find_element_by_xpath("//button[@class='wpO6b ']")
                  settings_button.click()
                  logout_button = self.browser.find_element_by_xpath("//button[text()='Log Out']")
                  logout_button.click()
                  self.login_status = False
                  print("Successfully Logged out")
              except:
                  print("Unable to Logout!")
  
      def login_confirmation(self):
          self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=self.username))
          sleep(5)
          try:
              if self.browser.find_element_by_xpath("//a[text()='Edit Profile']"):
                  return True
              else:
                  return False
          except:
              return False
  
      def open_inbox(self):
          if self.login_status is True:
              self.browser.get('https://www.instagram.com/direct/inbox/')
              try:
                  if self.browser.find_element_by_xpath("//div[text()='Direct']"):
                      print("Opened Inbox")
                      return True
                  else:
                      print("Could not Open Inbox")
                      return False
              except:
                  print("Could not Open Inbox")
                  return False
          else:
              print("Your Are not Logged In!")
  
      def turn_on_notification_state(self):
          if self.login_status is True:
              try:
                  if self.browser.find_element_by_xpath("//button[text()='Turn On']"):
                      try:
                          if self.browser.find_element_by_xpath("//button[text()='Not Now']"):
                              return True
                      except:
                          return False
              except:
                  return False
          else:
              print("Your Are not Logged In!")
  
      def turn_on_notification_action(self, value):
          if self.login_status is True:
              if self.turn_on_notification_state() is True:
                  if value is True:
                      turn_on_notification = self.browser.find_element_by_xpath("//button[text()='Turn On']")
                      turn_on_notification.click()
                      self.notification_status = True
                      print('Disabled Notification Popup!')
                  else:
                      turn_off_notification = self.browser.find_element_by_xpath("//button[text()='Not Now']")
                      turn_off_notification.click()
                      self.notification_status = False
                      print('Disabled Notification Popup!')
              else:
                  print("Notification Popup Not Detected!")
          else:
              print("Your Are not Logged In!")
  
      def open_inbox(self):
          if self.login_status is True:
              self.browser.get('https://www.instagram.com/direct/inbox/')
              try:
                  if self.browser.find_element_by_xpath("//div[text()='Direct']"):
                      print("Opened Inbox")
                      return True
                  else:
                      print("Could not Open Inbox")
                      return False
              except:
                  print("Could not Open Inbox")
                  return False
          else:
              print("Your Are not Logged In!")
  
      def pre_checklist(self):
          if self.login_status is True:
              self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=self.username))
              sleep(3)
              self.following = self.__get_amount_self_following()
              self.followers = self.__get_amount_self_followers()
              inbox_status = self.open_inbox()
              if inbox_status is True:
                  self.turn_on_notification_action(False)
                  if self.notification_status is False:
                      print("Pre-Check Done!")
                  elif self.notification_status is True:
                      print("Pre-Check Done!")
              else:
                  print("Pre-Check Failed!")
          else:
              print("Your Are not Logged In!")
  
      def chat_search_opener(self):
          if self.login_status is True:
              state = self.open_inbox()
              if state is True:
                  try:
                      search_button = self.browser.find_element_by_xpath("//button[@class='wpO6b ZQScA']")
                      search_button.click()
                      return True
                  except:
                      return False
              else:
                  return False
          else:
              print("Your Are not Logged In!")
  
      def chat_search_and_open(self, username):
          if self.login_status is True:
              try:
                  state = self.chat_search_opener()
                  if state is True:
                      seach_input = self.browser.find_element_by_xpath("//input[@placeholder='Search...']")
                      seach_input.send_keys(username)
                      sleep(5)
                      seach_result = self.browser.find_element_by_xpath(
                          "//div[@class='                    Igw0E   rBNOH        eGOV_    "
                          " ybXk5    _4EzTm                                                "
                          "                                   XfCBB          HVWg4         "
                          "        ']")
                      seach_result.click()
                      next_button = self.browser.find_element_by_xpath("/html/body/div[4]/div/div/div[1]/div/div[2]/div/button/div")
                      next_button.click()
                      return True
              except:
                  return False
          else:
              print("Your Are not Logged In!")
  
      def send_dm(self, username, message):
          if self.login_status is True:
              state = self.chat_search_and_open(username)
              if state is True:
                  type_message = self.browser.find_element_by_xpath("//textarea[@placeholder='Message...']")
                  type_message.send_keys(message)
                  send_button = self.browser.find_element_by_xpath("//button[text()='Send']")
                  send_button.click()
                  print("DM Sent!")
              else:
                  print("An Error Occurred sending DM")
          else:
              print("Your Are not Logged In!")
  
      def open_profile(self, username):
          if self.login_status is True:
              self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=username))
              sleep(5)
              try:
                  if self.browser.find_element_by_xpath("//h2[text()='{usrname}']".format(usrname=username)):
                      print("Opened Profile!")
                      return True
                  else:
                      print("Could not Open Profile!")
                      return False
              except:
                  print("Could not Open Profile!")
                  return False
          else:
              print("Your Are not Logged In!")
  
      def follow_user(self, username):
          if self.login_status is True:
              profile_status = self.open_profile(username)
              if profile_status is True:
                  try:
                      follow_button = self.browser.find_element_by_xpath(
                          "/html/body/div[1]/section/main/div/header/section/div[1]/div[1]/span/span[1]/button")
                      follow_button.click()
                      sleep(2)
                      print("User Followed!")
                  except:
                      try:
                          follow_button = self.browser.find_element_by_xpath(
                              "/html/body/div[1]/section/main/div/header/section/div[1]/button")
                          follow_button.click()
                          sleep(2)
                          print("User Followed!")
                      except:
                          try:
                              if self.browser.find_element_by_xpath(
                                      "//button[text()='Requested'][@class='BY3EC  sqdOP  L3NKy   "
                                      " _8A5w5    ']"):
                                  print("User Has already been send a follow request!")
                          except:
                              try:
                                  if self.browser.find_element_by_xpath(
                                          "//button[text()='Message'][@class='fAR91 sqdOP  L3NKy "
                                          "_4pI4F   _8A5w5    ']"):
                                      print("User is already followed!")
                              except:
                                  print("Could not follow User!")
  
              else:
                  print("Could not follow User!")
          else:
              print("Your Are not Logged In!")
  
      def unfollow_user(self, username):
          if self.login_status is True:
              profile_status = self.open_profile(username)
              if profile_status is True:
                  try:
                      unfollow_prompt = self.browser.find_element_by_xpath("//span[@aria-label='Following']["
                                                                           "@class='glyphsSpriteFriend_Follow u-__7']")
                      unfollow_prompt.click()
                      unfollow_button = self.browser.find_element_by_xpath("//button[text()='Unfollow']")
                      unfollow_button.click()
                      print("User Unfollowed")
                  except:
                      try:
                          if self.browser.find_element_by_xpath("//button[text()='Follow'][@class='BY3EC  sqdOP  L3NKy  "
                                                                " y3zKF     ']"):
                              print("User is not being Followed already!")
                      except:
                          try:
                              unfollow_prompt = self.browser.find_element_by_xpath(
                                  "//button[text()='Requested'][@class='BY3EC  sqdOP  L3NKy   "
                                  " _8A5w5    ']")
                              unfollow_prompt.click()
                              unfollow_button = self.browser.find_element_by_xpath("//button[text()='Unfollow']")
                              unfollow_button.click()
                              print("User Has already been send a follow request! Cancelled it hehe!")
                          except:
                              print("Could Not Unfollow User!")
              else:
                  print("Could Not Unfollow User!")
          else:
              print("You are not logged In!")
  
      def __get_amount_self_followers(self):
          if self.login_status is True:
              try:
                  followers_elem = self.browser.find_element_by_xpath(
                      "//a[@class='-nal3 '][@href='/{usrname}/followers/']/span[@class='g47SY ']".format(
                          usrname=self.username))
                  return int(followers_elem.text)
              except:
                  print("Error Occoured!")
          else:
              print("You are not Logged in!")
  
      def __get_amount_self_following(self):
          if self.login_status is True:
              try:
                  following_elem = self.browser.find_element_by_xpath(
                      "//a[@class='-nal3 '][@href='/{usrname}/following/']/span[@class='g47SY ']".format(
                          usrname=self.username))
                  return int(following_elem.text)
              except:
                  print("Error Occoured!")
          else:
              print("You are not logged In!")
  
      def get_amount_followers(self, username):
          if self.login_status is True:
              self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=username))
              try:
                  followers_elem = self.browser.find_element_by_xpath(
                      "//a[@class='-nal3 '][@href='/{usrname}/followers/']/span[@class='g47SY ']".format(
                          usrname=username))
                  return int(followers_elem.text)
              except:
                  print("Error Occoured!")
          else:
              print("You are not Logged in!")
  
      def get_amount_following(self, username):
          if self.login_status is True:
              self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=username))
              try:
                  following_elem = self.browser.find_element_by_xpath(
                      "/html/body/div[1]/section/main/div/header/section/ul/li[3]/a/span").text
                  return int(following_elem.replace(",", ""))
              except:
                  print("Error Occoured!")
          else:
              print("You are not logged In!")
  
      def __scrolly_for_followers(self, followers=None):
          i = 0
          if followers is None:
              total_followers = self.following
          else:
              total_followers = followers
          try:
              dialog = self.browser.find_element_by_xpath('/html/body/div[4]/div/div[2]')
              for x in range(int(total_followers / 10)):
                  self.browser.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", dialog)
                  sleep(random.randint(500, 1000) / 1000)
                  if i == 8:
                      i = 0
                      per = '{0:.2f}'.format((x / (total_followers / 10)) * 100)
                      print("{cl}% Fetched".format(cl=per))
                  else:
                      i = i + 1
          except:
              print("Unable to Scroll!")
  
      def __scrolly_for_following(self, following=None):
          i = 0
          if following is None:
              total_following = self.following
          else:
              total_following = following
          try:
              dialog = self.browser.find_element_by_xpath('/html/body/div[4]/div/div[2]')
              for x in range(int(total_following / 8)):
                  self.browser.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", dialog)
                  sleep(random.randint(500, 1000) / 1000)
                  if i == 8:
                      i = 0
                      per = '{0:.2f}'.format((x / (total_following / 8)) * 100)
                      print("{cl}% Fetched".format(cl=per))
                  else:
                      i = i + 1
          except:
              print("Unable to Scroll!")
  
      def fetch_self_followers(self):
          if self.login_status is True:
              self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=self.username))
              sleep(3)
              followers_button = self.browser.find_element_by_xpath(
                  "//a[@class='-nal3 '][@href='/{usrname}/followers/']".format(usrname=self.username))
              followers_button.click()
              sleep(2)
              usernames = []
              self.__scrolly_for_followers()
              try:
                  print("100% Fetched! Processing...")
                  for i in range(1, self.followers + 1):
                      usr = self.browser.find_element_by_xpath(
                          "/html/body/div[4]/div/div[2]/ul/div/li[{n}]/div/div[1]/div[2]/div[1]/a".format(n=i))
                      usernames.append(usr.text)
                  print("Followers Processed!")
              except:
                  print("IG did not give all followers! {f} Followers Scanned.".format(f=len(usernames)))
                  return usernames
  
              return usernames
          else:
              print("You are not Logged In!")
              return 0
  
      def fetch_self_following(self):
          if self.login_status is True:
              self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=self.username))
              sleep(3)
              followers_button = self.browser.find_element_by_xpath(
                  "//a[@class='-nal3 '][@href='/{usrname}/following/']".format(usrname=self.username))
              followers_button.click()
              sleep(2)
              usernames = []
              self.__scrolly_for_following()
              try:
                  print("100% Fetched! Processing...")
                  for i in range(1, self.following + 1):
                      usr = self.browser.find_element_by_xpath(
                          "/html/body/div[4]/div/div[2]/ul/div/li[{n}]/div/div[1]/div[2]/div[1]/a".format(n=i))
                      usernames.append(usr.text)
                  print("Followings Processed!")
              except:
                  print("IG did not give all followers! {f} Following Scanned.".format(f=len(usernames)))
                  return usernames
  
              return usernames
          else:
              print("You are not Logged In!")
              return 0
  
      def fetch_following(self, username):
          if self.login_status is True:
              if self.is_accessible(username):
                  self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=username))
                  sleep(3)
                  followers_button = self.browser.find_element_by_xpath(
                      "//a[@class='-nal3 '][@href='/{usrname}/following/']".format(usrname=username))
                  followers_button.click()
                  sleep(2)
                  usernames = []
                  following = self.get_amount_following(username)
                  self.__scrolly_for_following()
                  try:
                      print("100% Fetched! Processing...")
                      for i in range(1, following + 1):
                          usr = self.browser.find_element_by_xpath(
                              "/html/body/div[4]/div/div[2]/ul/div/li[{n}]/div/div[1]/div[2]/div[1]/a".format(n=i))
                          usernames.append(usr.text)
                      print("Followings Processed!")
                  except:
                      print("IG did not give all followers! {f} Following Scanned.".format(f=len(usernames)))
                      return usernames
  
                  return usernames
              else:
                  print("Account is not accessible. Are you sure you are following it?")
                  return 0
          else:
              print("You are not Logged In!")
              return 0
  
      def fetch_followers(self, username):
          if self.login_status is True:
              if self.is_accessible(username):
                  followers_button = self.browser.find_element_by_xpath(
                      "//a[@class='-nal3 '][@href='/{usrname}/followers/']".format(usrname=username))
                  followers_button.click()
                  sleep(2)
                  usernames = []
                  followers = self.get_amount_followers(username)
                  self.__scrolly_for_followers(followers)
                  try:
                      print("100% Fetched! Processing...")
                      for i in range(1, followers + 1):
                          usr = self.browser.find_element_by_xpath(
                              "/html/body/div[4]/div/div[2]/ul/div/li[{n}]/div/div[1]/div[2]/div[1]/a".format(n=i))
                          usernames.append(usr.text)
                      print("Followers Processed!")
                  except:
                      print("IG did not give all followers! {f} Followers Scanned.".format(f=len(usernames)))
                      return usernames
  
                  return usernames
              else:
                  print("Account is not accessible. Are you sure you are following it?")
                  return 0
          else:
              print("You are not Logged In!")
              return 0
  
      def is_accessible(self, username):
          self.browser.get('https://www.instagram.com/{usrname}/'.format(usrname=username))
          sleep(3)
          try:
              if self.browser.find_element_by_xpath(
                      "/html/body/div[1]/section/main/div/div/article/div[1]/div/h2[text()='This Account is Private']"):
                  return True
              else:
                  return False
          except:
              return True
  
      def search_hashtag_and_get_links(self, search_string, number_of_posts_required):
          if self.login_status is True:
              self.browser.get('https://www.instagram.com/explore/tags/{ss}/'.format(ss=search_string))
              sleep(4)
              self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
              oolumn = 1
              row = 1
              element_refresh_status = 0
              loop = 0
              progress = 0
              links = set()
              while True:
                  i = len(links)
                  try:
                      if row == 3:
                          oolumn = oolumn + 1
                          row = 1
                      elif oolumn > 10:
                          oolumn = 1
                      else:
                          row = row + 1
                      if element_refresh_status == 10:
                          self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                          self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                          sleep(0.5)
                          element_refresh_status = 0
                      else:
                          element_refresh_status = element_refresh_status + 1
                          link = self.browser.find_element_by_xpath(
                              "/html/body/div[1]/section/main/article/div[2]/div/div[{c}]/div[{r}]/a".format(r=row,
                                                                                                             c=oolumn))
                          links.add(link.get_attribute("href"))
                          if i == len(links):
                              self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                              self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                              sleep(0.5)
                  except:
                      self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                      self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                  if len(links) == number_of_posts_required:
                      print("Followers Processed! Got {f} Links!".format(f=len(links)))
                      return set(links)
                  loop = loop + 1
                  if loop > number_of_posts_required * 10:
                      return links
                  if progress == 15:
                      progress = 0
                      per = '{0:.2f}'.format((len(links) / number_of_posts_required) * 100)
                      print("{cl}% Processed".format(cl=per))
                  else:
                      progress = progress + 1
          else:
              print("You are not Logged In!")
  
      def interact_with_post_by_link(self, url, like=False, comment=False, comment_body=None, save=False):
          global time_posted, username, description
          if self.login_status is True:
              self.browser.get(url)
              sleep(2)
              print("Opened post!")
              liked = False
              commented = False
              commented_body = ""
              saved = False
              if like is True:
                  try:
                      like_button = self.browser.find_element_by_xpath(
                          "/html/body/div[1]/section/main/div/div/article/div/div[3]/section[1]/span[1]/button")
                      like_button.click()
                      liked = True
                      print("Post Liked!")
                  except:
                      print("Could not Like Post!")
                      liked = False
              if comment is True:
                  if comment_body is None:
                      print("Did not get Comment Body")
                  else:
                      try:
                          comment_button = self.browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div/article/div/div[3]/section[1]/span[2]/button")
                          comment_button.click()
                          comment_input = self.browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div/article/div/div[3]/section[3]/div[1]/form/textarea")
                          comment_input.send_keys(comment_body)
                          post_button = self.browser.find_element_by_xpath("//button[text()='Post']")
                          post_button.click()
                          commented_body = comment_body
                          print("Commented!")
                          commented = True
                          sleep(3)
                      except:
                          print("Could not comment")
                          commented = False
              if save is True:
                  try:
                      saved_button = self.browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div/article/div/div[3]/section[1]/span[3]/div/div/button")
                      saved_button.click()
                      print("Post Saved!")
                      saved = True
                  except:
                      print("Could Save Post!")
                      saved = False
              try:
                  description = self.browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div/div[3]/div[1]/ul/div/li/div/div/div[2]/span").text
              except:
                  print("Could not get Description")
              try:
                  username = self.browser.find_element_by_xpath(
                      "/html/body/div[1]/section/main/div/div[1]/article/div/header/div[2]/div[1]/div[1]/span/a").text
              except:
                  print("Could Not get username")
              try:
                  time_posted = self.browser.find_element_by_xpath(
                      "/html/body/div[1]/section/main/div/div[1]/article/div/div[3]/div[2]/a/time").text
              except:
                  print("Could not get time posted")
              status = {"username": username, "url": url, "liked": liked, "commented": commented,
                        "commented_body": commented_body, "saved": saved, "description": description,
                        "time_posted": time_posted}
              return status
          else:
              print("You are not Logged in!")
  
      def check_all_modules(self, username):
          if self.login_status is True:
              print("You Are Logged In")
              print("This process might take some time")
              self.pre_checklist()
              self.send_dm(username,"bruh FU") #working
              self.get_amount_following(username) #not working
              self.get_amount_followers(username) #not working
              self.fetch_self_followers() #not working
              self.fetch_self_following() #not working
              self.fetch_following(username) #not working
              self.fetch_followers(username) #not working
  
  
      def like_comment_post_by_url(self, url, limit=0, random=False):
          if self.login_status is True:
              result_arr = {}
              if limit is 0 and random is False:
                  print("No Limit has been set, will like some few comments!")
              elif limit is 0 and random is True:
                  print("No Limit has been set, will like some few comments randomly!")
              elif limit > 0 and random is False:
                  print("Limit has been set, will like the top comments until limit is reached!")
              elif limit > 0 and random is True:
                  print("Limit has been set, will like the top comments randomly until limit is reached!")
              elif limit < 0:
                  print("Bottom Comments are not yet supported!")
                  return result_arr
              self.browser.get(url)
              comment_box = self.browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div[2]/div[1]/ul")
              load_comments_button = self.browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div[2]/div[1]/ul/li/div/button/span")
  
  
  