-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
356 lines (312 loc) · 14.1 KB
/
main.py
File metadata and controls
356 lines (312 loc) · 14.1 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import os
class App:
def __init__( self, username , password, path):
self.username = username
self.password = password
self.path =path
self.url = 'https://www.instagram.com/'
chromedriver = self.path
os.environ["webdriver.chrome.driver"] = chromedriver
self.driver = webdriver.Chrome(chromedriver)
self.error =False
self.following=[]
self.followers=[]
self.unfollow=[]
self.webelem=[]
def open(self):
try:
self.driver.get("https://www.instagram.com/")
sleep(3)
except Exception as e:
error = True
print("Page won't open , you may need to update google chrome")
def LogInPage(self):
if self.error is False:
try:
login_element = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a')
login_element.click()
sleep(5)
except Exception as e:
print('Feature not available')
self.error = True
def LogIn(self ):
if self.error is False:
try:
username_field = self.driver.find_element_by_xpath('//label[@class="f0n8F "]/input[@aria-label="Phone number, username, or email"]')
username_field.clear()
username_field.send_keys(self.username)
password_field = self.driver.find_element_by_xpath('//label[@class="f0n8F "]/input[@aria-label="Password"]')
password_field.clear()
password_field.send_keys(self.password)
password_field.submit()
sleep(5)
except Exception as e:
print(e)
self.error = True
print('Cant log in ')
def closePopUp(self):
if self.error is False:
try:
notNow =self.driver.find_element_by_xpath('//button[@class="aOOlW HoLwm "]')
notNow.click()
except:
self.error = True
print('Something wrong ..')
def openProfile(self):
if self.error is False:
try:
profile =self.driver.find_element_by_xpath('//div[@class="XrOey"]/a[@href="/{}/"]'.format(self.username))
profile.click()
sleep(4)
except Exception as e:
print('Wrong')
print(e)
self.error =True
def openFollowingPage(self):
if self.error is False:
try:
follower = self.driver.find_element_by_xpath('//a[@href="/{}/following/"]'.format(self.username))
follower.click()
sleep(2)
except Exception as e:
print('Wrong with follower page')
self.error = True
def openFollowersPage(self):
if self.error is False:
try:
follower = self.driver.find_element_by_xpath('//a[@href="/{}/followers/"]'.format(self.username))
follower.click()
sleep(2)
except Exception as e:
print('Wrong with follower page')
self.error = True
def getFollowers(self):
count =1
if self.error is False:
while count <2:
try:
self.ScrollDownFoll()
sleep(1)
print('Scrolling complete')
lis = self.driver.find_elements_by_xpath('//div[@class="PZuss"]/li')
# print(div)
# lis = div.find_elements_by_xpath('//li//div[@class=" Igw0E IwRSH eGOV_ ybXk5 _4EzTm "]/a')
# items= div.find_elements_by_tag_name("li")
print(len(lis))
print(lis)
# items = items.
for item in lis:
# i = item.find_element_by_xpath('//div[@class=" Igw0E IwRSH eGOV_ ybXk5 _4EzTm "]')
item = item.find_element_by_xpath('.//a')
# self.following.append(item.get_attribute("href"))
self.followers.append(item.get_attribute("href"))
# print(self.following)
button = self.driver.find_element_by_xpath('//button[@class="dCJp8 afkep"]')
self.driver.execute_script('document.getElementsByClassName(\'dCJp8 afkep\')[1].click();')
# button.click()
sleep(1)
except Exception as e:
print(e)
print('Wrong')
# self.error = True
count+=1
def getFollowing(self):
count =1
if self.error is False:
while count <2:
try:
self.ScrollDownFoll()
sleep(1)
print('Scrolling complete')
lis = self.driver.find_elements_by_xpath('//div[@class="PZuss"]/li')
# print(div)
# lis = div.find_elements_by_xpath('//li//div[@class=" Igw0E IwRSH eGOV_ ybXk5 _4EzTm "]/a')
# items= div.find_elements_by_tag_name("li")
print(len(lis))
print(lis)
# items = items.
for item in lis:
# i = item.find_element_by_xpath('//div[@class=" Igw0E IwRSH eGOV_ ybXk5 _4EzTm "]')
item = item.find_element_by_xpath('.//a')
# self.following.append(item.get_attribute("href"))
self.following.append(item.get_attribute("href"))
# print(self.following)
button =self.driver.find_element_by_xpath('//button[@class="dCJp8 afkep"]')
self.driver.execute_script('document.getElementsByClassName(\'dCJp8 afkep\')[1].click();')
# button.click()
sleep(1)
except Exception as e:
print(e)
print('Wrong')
# self.error = True
count+=1
def ScrollDownFoll(self):
try:
# We 'll scroll down 10 times
# OR CAN BE DONE BY COUNTING THE NO OF IMAGES FIND XPATH OF THE NUMBER.text.replace(",","")
# THEN SCROLLING ACCORDING NO OF IMAGES LOADED IN A SCROLL
js_code ='function sleep(sec){var start =new Date().getTime();for (var i =0; i < 1e7; i++){if ((new Date().getTime()- start) > sec)break;}} ' \
'for ( var i =0 ; i < 1000; ) {' \
'var x = document.querySelector("div[class=\'isgrP\']");' \
'x.scrollTo(0,i+100);' \
'i=i+100;' \
'sleep(1000)}'
js_cc ='var x = document.querySelector("div[class=\'isgrP\']");' \
'x.scrollTo(0,x.scrollHeight);'
js_init = 'var x = document.querySelector("div[class=\'isgrP\']");' \
'x.scrollTo(0,12*40);'
js_start = 'var x = document.querySelector("div[class=\'isgrP\']");' \
'x.scrollTo(0,0);'
js_he = 'var x = document.querySelector("div[class=\'isgrP\']");' \
'return x.scrollHeight;'
# for i in range(15):
# self.driver.execute_script(js_code)
# sleep(4)
#
print('Scrolling ...')
self.driver.execute_script(js_init)
self.driver.implicitly_wait(2)
sleep(2)
# for i in range(10):
# self.driver.execute_script(js_cc)
#
# print('Scrolling {}-th time'.format(i+1))
SCROLL_PAUSE_TIME = 3
# Get scroll height
last_height = self.driver.execute_script(js_he)
while True:
# Scroll down to bottom
self.driver.execute_script(js_cc)
# Wait to load page
self.driver.implicitly_wait(2)
sleep(SCROLL_PAUSE_TIME)
# self.driver.execute_script(js_start)
# Calculate new scroll height and compare with last scroll height
new_height = self.driver.execute_script(js_he)
print(new_height)
if new_height == last_height:
break
last_height = new_height
sleep(3)
self.driver.execute_script(js_cc)
# sleep(3)
# self.driver.execute_script(js_cc)
self.driver.execute_script(js_start)
except Exception as e:
print(e)
print("Can't scroll")
def equal(self , following , follower):
if following == follower:
return 1
return 0
def performUnfollow(self):
pass
def UnFollow(self):
count = 1
if self.error is False:
while count < 2:
try:
self.openFollowingPage()
sleep(1)
self.ScrollDownFoll()
sleep(1)
print('Scrolling complete')
lis = self.driver.find_elements_by_xpath('//div[@class="PZuss"]/li')
# print(div)
# lis = div.find_elements_by_xpath('//li//div[@class=" Igw0E IwRSH eGOV_ ybXk5 _4EzTm "]/a')
# items= div.find_elements_by_tag_name("li")
print(len(lis))
print(lis)
# items = items.
for item in lis:
# i = item.find_element_by_xpath('//div[@class=" Igw0E IwRSH eGOV_ ybXk5 _4EzTm "]')
i = item.find_element_by_xpath('.//a').get_attribute("href")
# self.following.append(item.get_attribute("href"))
print(len(self.unfollow))
if i in self.unfollow:
# self.driver.execute_script('document.getElementByClassName(\'sqdOP L3NKy _8A5w5 \')')
button = item.find_element_by_xpath('.//button[@class="sqdOP L3NKy _8A5w5 "]')
button.click()
self.driver.implicitly_wait(2)
sleep(2)
print('do you want to unfollow {} , press y/n'.format(i))
ans = input()
if ( ans == 'y'):
self.driver.execute_script('document.getElementsByClassName(\'aOOlW -Cab_ \')[0].click();')
#unfollow = self.driver.find_element_by_xpath('//button[@class="aOOlW -Cab_ "]')
# cancel = self.driver.find_element_by_xpath('//button[class="aOOlW HoLwm "]')
else:
self.driver.execute_script('document.getElementsByClassName(\'aOOlW HoLwm \')[0].click();')
sleep(2)
# print(self.following)
button = self.driver.find_element_by_xpath('//button[@class="dCJp8 afkep"]')
self.driver.execute_script('document.getElementsByClassName(\'dCJp8 afkep\')[1].click();')
# button.click()
sleep(1)
except Exception as e:
print(e)
print('Wrong')
self.error = True
count += 1
def write(self , person):
# item = person.find_element_by_xpath('.//a')
print('Need to unfollow ', end="")
print(person)
def iterate(self):
for following in self.following:
check =0
for follower in self.followers:
if self.equal(following , follower):
check =1
print('{} found in followers'.format(following))
break
if check == 0: # need to unfollow
self.write(following)
self.unfollow.append(following)
def check():
if (Inst.error):
exit(0)
def run():
print('Enter username')
username = input()
print('enter password')
password =input()
print('Please download chromedriver version according to your chrome can be found in settings and provide the path to it')
path = input()
Inst = App(username , password , path)
Inst.open()
check()
print('Page opened')
Inst.LogInPage()
check()
print('auth login done .')
Inst.LogIn()
check()
print('Logged in')
Inst.closePopUp()
check()
print('dialog box closed')
Inst.openProfile()
check()
print('Profile opened')
Inst.openFollowingPage()
check()
print('Following page opened')
Inst.getFollowing()
check()
print('following fetched')
Inst.openFollowersPage()
check()
print('Followers page opened')
Inst.getFollowers()
check()
print('Followers fetched')
Inst.iterate()
check()
print('values written')
Inst.UnFollow()
check()
run()