-
Notifications
You must be signed in to change notification settings - Fork 0
/
LikeStats.py
193 lines (123 loc) · 4.66 KB
/
LikeStats.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
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
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
#Please write gecko drivers path down here
browser = webdriver.Firefox(executable_path=r'gecko drivers path here')
""" login part """
browser.get("https://www.instagram.com/")
time.sleep(2)
idm = browser.find_element_by_name("username")
passw = browser.find_element_by_name("password")
# write your user name and password to log and pas areas.
log = "un here"
pas = "pw here"
idm.send_keys(log)
passw.send_keys(pas)
buton = browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button")
buton.click()
time.sleep(3)
browser.get("https://www.instagram.com/"+log)
def scroll():
global browser
footer = browser.find_element_by_tag_name('footer')
last_height = browser.execute_script('return document.body.scrollHeight')
while True:
footer.location_once_scrolled_into_view
time.sleep(2)
new_height = browser.execute_script('return document.body.scrollHeight')
if new_height == last_height:
break
else :
last_height = new_height
scroll()
classes = browser.find_elements_by_class_name("v1Nh3 ")
links = []
for eleman in classes:
links.append(eleman.find_element_by_tag_name('a'))
time.sleep(3)
links= [elem.get_attribute('href') for elem in links
if '.com/p/' in elem.get_attribute('href')]
#keeps likers for posts in order to add them to wholelikerslist
likersList = []
wholeLikers = []
def clickLikers():
time.sleep(2)
likers = browser.find_element_by_css_selector("a.zV_Nj:nth-child(2)")
time.sleep(1)
likers.click()
time.sleep(1)
def likerListCheck():
global likersList
likersList = list(set(likersList))
time.sleep(1)
for liker in likersList:
wholeLikers.append(liker)
likersList = []
def likeScroll():
exceptioncount = 0
namecount = 0
likerTab = browser.find_element_by_css_selector("body > div.RnEpo.Yx5HN > div > div > div.Igw0E.IwRSH.eGOV_.vwCYk.i0EQd > div")
actions = ActionChains(browser)
likerTab.click()
time.sleep(0.5)
actions.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
exect = """
followers = document.querySelector(".i0EQd > div:nth-child(1)");
var lenOfPage=followers.scrollHeight;
return lenOfPage;"""
lenOfPage = browser.execute_script(exect)
match=False
while(match==False):
likername = browser.find_elements_by_css_selector(".FPmhX.notranslate.MBL3Z")
lastCount = lenOfPage
time.sleep(1)
try:
for eleman in likername:
user = eleman.get_attribute('title')
namecount +=1
print(namecount, " " , user)
likersList.append(user)
except:
exceptioncount +=1
print(exceptioncount, " ***************An exception occurred*****")
time.sleep(0.5)
actions.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
time.sleep(2)
lenOfPage = browser.execute_script(exect)
if lastCount == lenOfPage:
match=True
ct = 0
for post in links:
browser.get(post)
time.sleep(6)
clickLikers()
time.sleep(2)
likeScroll()
time.sleep(1)
likerListCheck()
time.sleep(1)
print(likersList)
ct +=1
# change the current path of Followers below with path of text file that contain your whole followers
Followers = open(r"Followers text file's path here", "r")
followerLines = Followers.readlines()
comparedFollowers = []
for person in followerLines:
isLiked = False
for liker in wholeLikers:
if(person.strip() == liker.strip()):
newline = person.strip()+"-"+ str(wholeLikers.count(liker))
comparedFollowers.append(newline)
isLiked = True
break
if(isLiked == False):
line = person.strip()+"-"+"0"
comparedFollowers.append(line)
#we obtained the list but it is not sorted.
LastList = sorted(comparedFollowers, key = lambda x: x.split('-')[1],reverse=True)
with open("comparedFollowers6.txt","w",encoding = "UTF-8") as file:
for line in LastList:
file.write(line+"\n")