from selenium import webdriver 
from time import sleep
from login_info import username, password

class Instabot:
    def __init__(self,username,pw):
        self.driver = webdriver.Firefox()
        self.username = username
        self.driver.get("https://instagram.com")
        sleep(4)
        
        login_field = self.driver.find_element_by_xpath("//input[@name=\"username\"]").send_keys(username)
        login_field = self.driver.find_element_by_xpath("//input[@name=\"password\"]").send_keys(pw)
        self.driver.find_element_by_xpath('//button[@type="submit"]').click()
        sleep(4)
        self.driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div/div/div/button').click()
        sleep(2)
        self.driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[3]/button[2]').click()
        sleep(2)


    def get_unfollowers(self):
        self.driver.find_element_by_xpath("//a[contains(@href,'/{}')]".format(self.username)).click()
        sleep(2)
        
        #following names 
        self.driver.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[3]/a').click()
        following = self._get_names()

        #followers names
        self.driver.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[2]/a').click()
        followers = self._get_names()
        not_following_back = [person for person in following if person not in followers]
        print(not_following_back)


    def _get_names(self):
        sleep(2)
        scroll_box = self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]")
        last_ht, ht = 0,1
        sleep(1)
        while last_ht != ht:
            last_ht = ht
            sleep(1)
            ht = self.driver.execute_script("""
                arguments[0].scrollTo(0,arguments[0].scrollHeight);
                return arguments[0].scrollHeight;
                """,scroll_box)
        link = scroll_box.find_elements_by_tag_name('a')
        names = [name.text for name in link if name.text != '']
        #close
        sleep(2)
        self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[1]/div/div[2]/button").click()
        sleep(1)
        return names

# Here Goes Nothing!

my_first_bot = Instabot(username,password)
my_first_bot.get_unfollowers()