-
Notifications
You must be signed in to change notification settings - Fork 0
/
botCommands.py
297 lines (261 loc) · 13.6 KB
/
botCommands.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
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
# {Modules:
from selenium import webdriver
from time import sleep
import os
import time
from platform import system
import sys
import colorama
from colorama import Fore, Style
# }
# {Classes:
class InstaBot:
task = 0
count = 0
def __init__(self, username, pw):
log = open("log.txt", 'a')
log.write("__Session Start__\n\t")
self.driver = webdriver.Chrome("chromedriver.exe")
self.driver.maximize_window()
self.username = username
self.driver.get("https://www.instagram.com/")
sleep(5)
self.driver.find_element_by_xpath("//input[@name=\"username\"]")\
.send_keys(username)
sleep(0.5)
self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
.send_keys(pw)
sleep(0.5)
self.driver.find_element_by_xpath('//button[@type="submit"]')\
.click()
sleep(5)
self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
sleep(5)
self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
sleep(5)
# { Clear Function:
def clear():
if system() == 'Windows':
os.system('cls')
else:
os.system('clear')
# }
# {slowprint:
def slowprint(s):
for c in s + '\n':
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(2.5 / 100)
# }
# {GUI:
sys.path.insert(1, 'E:\\~Salman\\programming\\python\\igSuck-New\\banner.py')
from banner import banner as bn
clear()
print(bn)
slowprint (Fore.RED+" ["+Fore.LIGHTGREEN_EX+"login"+Fore.BLUE+"~"+Fore.WHITE+"@Successful"+Fore.RED+']')
slowprint (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"bot"+Fore.BLUE+"~"+Fore.WHITE+"@Alive"+Fore.RED+']')
str = time.asctime(time.localtime(time.time())) + ' -> Login Successful ('+username+'):'
log.write(str)
log.close()
def scroll_followers_list(self, target):
print(Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"scrolling"+Fore.BLUE+"~"+Fore.WHITE+f"@{target}"+Fore.RED+']✘'+Fore.WHITE+' users.'+Fore.RED+' Dont'+Fore.WHITE+' close the window'+Fore.RED+'.'+Fore.WHITE)
sleep(5)
scroll_element = self.driver.find_element_by_xpath(
"//div[@class='isgrP']")
length = 0
sleep(3)
self.driver.execute_script(
"return arguments[0].scrollIntoView();", scroll_element)
while length < target-1:
sleep(1)
elements = scroll_element.find_elements_by_tag_name('button')[-10:]
for element in elements:
try:
self.driver.execute_script(
"return arguments[0].scrollIntoView();", element)
sleep(0.15)
except:
continue
while len(scroll_element.find_elements_by_xpath("//div[@class='By4nA']")) > 0:
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"bot"+Fore.BLUE+"~"+Fore.WHITE+"@Sleeping"+Fore.RED+']')
sleep(1)
sleep(1)
length = len(scroll_element.find_elements_by_tag_name('button'))
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"scroll"+Fore.BLUE+"~"+Fore.WHITE+"@Complete"+Fore.RED+']')
def follow_button_click(self, target, type_='followers', scroll_element="//div[@class='isgrP']"):
count = 0
scroll_element = self.driver.find_element_by_xpath(scroll_element)
buttons = scroll_element.find_elements_by_tag_name('button')
for button in buttons:
try:
self.driver.execute_script(
"return arguments[0].scrollIntoView();", button)
if button.text == 'Follow':
self.focus_and_click(button)
count += 1
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"followed"+Fore.BLUE+"~"+Fore.WHITE+f"@{count}"+Fore.RED+']', end='\r')
if count >= target:
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"target"+Fore.BLUE+"~"+Fore.WHITE+f"@Met"+Fore.RED+']')
break
elif count == 25:
sleep(10)
elif count == 50:
sleep(20)
elif count == 100:
sleep(30)
sleep(6)
tries = 0
while tries < 20:
if button.text == 'Follow':
sleep(6)
if button.text == 'Follow':
self.focus_and_click(button)
tries += 1
elif button.text == 'Following' or button.text == 'Requested':
break
sleep(1)
try:
if scroll_element.find_element_by_tag_name('button').text == 'Unfollow':
scroll_element.find_element_by_tag_name(
'button').click()
break
except:
pass
if tries == 20:
sleep(60)
except:
pass
return count
def focus_and_click(self, element):
self.driver.execute_script(
"return arguments[0].scrollIntoView();", element)
self.driver.execute_script("arguments[0].click();", element)
def get_followers_of(self, username = ""):
log = open("log.txt", 'a')
if not username: username=self.username
file = username + "_followers"
list_ = open(file, "w+")
sleep(2)
self.driver.get('https://www.instagram.com/' + username + '/')
sleep(4)
fol = int((self.driver.find_element_by_xpath(
"/html/body/div[1]/section/main/div/header/section/ul/li[2]").text).split()[0])
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"followers"+Fore.BLUE+"~"+Fore.WHITE+f"@{fol}"+Fore.RED+']')
sleep(2)
self.driver.find_element_by_xpath("//a[contains(@href,'/followers')]")\
.click()
self.scroll_followers_list(int(fol))
scroll_box = self.driver.find_element_by_xpath("//div[@class='isgrP']")
links = scroll_box.find_elements_by_tag_name('a')
usernames = [name.text for name in links if name.text != '']
list_.writelines(["%s\n" % follower for follower in usernames])
list_.close()
str_ = "\n\tTask "+str(self.task)+": Fetched " + str(len(usernames)) + '/' + str(fol) + " followers of " + username
log.write(str_)
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"done"+Fore.BLUE+"~"+Fore.WHITE+f"@Extracting"+Fore.RED+']')
log.close()
def follow_followers_of(self, username, target=0):
log = open("log.txt", 'a')
self.task += 1
self.driver.get('https://www.instagram.com/' + username + '/')
sleep(5)
no_f = self.driver.find_element_by_xpath(
"/html/body/div[1]/section/main/div/header/section/ul/li[2]").text
if "k" or "m" in no_f:
fol = no_f.replace(" followers", "")
fol = fol.replace(",", "")
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"followers"+Fore.BLUE+"~"+Fore.WHITE+f"@{fol}"+Fore.RED+']')
self.driver.find_element_by_xpath("//a[contains(@href,'/followers')]")\
.click()
sleep(2)
count = 0
if target == 0:
target = int(input (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+f"{username}"+Fore.BLUE+"~"+Fore.WHITE+f"@Home"+Fore.RED+']✗ '+Fore.WHITE+'Enter max people to follow: '))
log.write("\n\tTask "+str(self.task)+": Following " +
str(target) + " followers of "+username)
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"now"+Fore.BLUE+"~"+Fore.WHITE+f"@Following"+Fore.RED+']')
batch = 1
while count < target:
batch_size = min((target-count), 100)
self.scroll_followers_list(batch_size)
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"batch"+Fore.BLUE+"~"+Fore.WHITE+f"@{batch}"+Fore.RED+f']✘ '+Fore.WHITE+f'of size {Fore.RED}{batch_size}{Style.RESET_ALL}.')
count += self.follow_button_click(batch_size)
string = '\n\t\tFollowed ' + \
str(count)+' users till batch '+str(batch)
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"followed"+Fore.BLUE+"~"+Fore.WHITE+f"@{count}"+Fore.RED+f']✘ '+Fore.WHITE+f'users till batch.')
log.write(string)
batch += 1
log.write("\n\tTask "+str(self.task)+": Followed " +
str(count) + " followers of "+username)
self.count += count
log.close()
else:
fol = no_f.replace(" followers", "")
fol = int(fol.replace(",", ""))
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"followers"+Fore.BLUE+"~"+Fore.WHITE+f"@{fol}"+Fore.RED+']')
self.driver.find_element_by_xpath("//a[contains(@href,'/followers')]")\
.click()
sleep(2)
count = 0
if target == 0:
target = int(input (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+f"{username}"+Fore.BLUE+"~"+Fore.WHITE+f"@Home"+Fore.RED+']✗ '+Fore.WHITE+'Enter max people to follow: '))
log.write("\n\tTask "+str(self.task)+": Following " +
str(target) + " followers of "+username)
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"now"+Fore.BLUE+"~"+Fore.WHITE+f"@Following"+Fore.RED+']')
batch = 1
while count < target:
batch_size = min((target-count), 100)
self.scroll_followers_list(batch_size)
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"batch"+Fore.BLUE+"~"+Fore.WHITE+f"@{batch}"+Fore.RED+f']✘ '+Fore.WHITE+f'of size {Fore.RED}{batch_size}{Style.RESET_ALL}.')
count += self.follow_button_click(batch_size)
string = '\n\t\tFollowed ' + \
str(count)+' users till batch '+str(batch)
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"followed"+Fore.BLUE+"~"+Fore.WHITE+f"@{count}"+Fore.RED+f']✘ '+Fore.WHITE+f'users till batch.')
log.write(string)
batch += 1
log.write("\n\tTask "+str(self.task)+": Followed " +
str(count) + " followers of "+username)
self.count += count
log.close()
def follow_users_followed_by(self, username, target=0):
log = open("log.txt", 'a')
self.task += 1
log.write("\n\tTask "+str(self.task) +
": Following users followed by "+username)
self.driver.get('https://www.instagram.com/' + username + '/')
sleep(5)
no_f = self.driver.find_element_by_xpath(
"/html/body/div[1]/section/main/div/header/section/ul/li[2]").text
fol = no_f.replace(" followers", "")
fol = int(fol.replace(",", ""))
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"followers"+Fore.BLUE+"~"+Fore.WHITE+f"@{fol}"+Fore.RED+f']✘ '+Fore.WHITE+f'users till batch.')
self.driver.find_element_by_xpath("//a[contains(@href,'/following')]")\
.click()
sleep(2)
count = 0
if target == 0:
target = int(input (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+f"{username}"+Fore.BLUE+"~"+Fore.WHITE+f"@Home"+Fore.RED+']✗ '+Fore.WHITE+'Enter max people to follow: '))
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"now"+Fore.BLUE+"~"+Fore.WHITE+f"@Following"+Fore.RED+']')
batch = 1
while count < target:
batch_size = min((target-count), 100)
self.scroll_followers_list(batch_size)
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"batch"+Fore.BLUE+"~"+Fore.WHITE+f"@{batch}"+Fore.RED+f']✘ of size {Fore.RED}{batch_size}{Style.RESET_ALL}.')
count += self.follow_button_click(batch_size, 'following')
string = '\n\t\tFollowed ' + \
str(count)+' users in batch '+str(batch)
print (Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"followed"+Fore.BLUE+"~"+Fore.WHITE+f"@{count}"+Fore.RED+f']✘ '+Fore.WHITE+f'users till batch.')
log.write(string)
batch += 1
log.write("\n\tTask "+str(self.task)+": Followed " +
str(count) + " users followed by "+username)
self.count += count
log.close()
def __del__(self):
log = open("log.txt", 'a')
print(Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"Bot"+Fore.BLUE+"~"+Fore.WHITE+f"@terminated"+Fore.RED+']')
print(Fore.RED+"\n ["+Fore.LIGHTGREEN_EX+"Followed"+Fore.BLUE+"~"+Fore.WHITE+f"@{self.count}"+Fore.RED+']')
log.write("\n\n\tFollowed total "+str(self.count) +' users and performed '+str(self.task)+' tasks \n__Session_End__ \n\n')
log.close()
# }
# }