-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
104 lines (93 loc) · 4.15 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from secrets import secret_username,secret_password
# options = webdriver.ChromeOptions()
# options.add_experimental_option('excludeSwitches', ['enable-logging'])
# options.add_argument('--log-level=3')
class InstaBot:
def __init__(self, username, pw):
self.driver = webdriver.Chrome()
self.wait = WebDriverWait(self.driver, 7)
print('opening instagram')
self.driver.get("https://instagram.com")
# sleep(2)
# self.driver.find_element_by_xpath("//a[contains(text(), 'Log in')]")\
# .click()
self.wait_find_click("//a[contains(text(), 'Log in')]")
sleep(2)
self.driver.find_element_by_xpath("//input[@name=\"username\"]")\
.send_keys(username)
self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
.send_keys(pw)
self.driver.find_element_by_xpath('//button[@type="submit"]')\
.click()
print('logging in..')
# sleep(1)
# self.is_element_exist("//button[contains(text(), 'Not Now')]")
# self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]")\
# .click()
self.wait_find_click("//button[contains(text(), 'Not Now')]")
print('come on..not now')
def get_unfollowers(self):
sleep(1)
print('open personal profile')
self.driver.find_element_by_xpath("//a[contains(@href,'/{}')]".format(secret_username))\
.click()
# sleep(4)
self.is_element_exist("//a[contains(@href,'/following')]")
print('opening following tab')
self.driver.find_element_by_xpath("//a[contains(@href,'/following')]")\
.click()
print('scanning following users')
following = self._get_users()
self.driver.find_element_by_xpath("//a[contains(@href,'/followers')]")\
.click()
print('scanning followers')
followers = self._get_users()
not_following_back = [user for user in following if user not in followers]
print('===Users not following back...====')
print(not_following_back)
def _get_users(self):
# some code if there are suggestions popping in 'following' tab
# self.is_element_exist("/html/body/div[4]/div/div[2]")
sleep(3)
scroll_box = self.driver.find_element_by_xpath("/html/body/div[4]/div/div[2]")
last_height, height = 0, 1
scroll = 0
temploop = 0
print("scrolling", end='', flush=True)
while last_height != height:
# while temploop < 3:
last_height = height
print(".", end='',flush=True)
sleep(1)
temploop += 1
scroll += 1
height = self.driver.execute_script("""
arguments[0].scrollTo(0, arguments[0].scrollHeight);
return arguments[0].scrollHeight;
""", scroll_box)
print('scrolled ', scroll, 'times')
print('making a list...')
links = scroll_box.find_elements_by_tag_name('a')
names = [name.text for name in links if name.text != '']
# print(names)
self.driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/button").click()
# for link in links:
# print(link.text)
# print(links)
return names
def wait_find_click(self, xpath_text):
elements = self.wait.until(EC.visibility_of_all_elements_located((By.XPATH, xpath_text)))
return self.driver.find_element_by_xpath(xpath_text).click() if elements else False
def is_element_exist(self,xpath_text):
elements = self.wait.until(EC.visibility_of_all_elements_located((By.XPATH, xpath_text)))
return None if elements else False
# the below commented code closes the browser upon completing execution
# hence assigning to variable required
# InstaBot(secret_username, secret_password)
startInsta = InstaBot(secret_username, secret_password)
startInsta.get_unfollowers()