This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstagram.py
226 lines (198 loc) · 7.41 KB
/
instagram.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
#This code was written by Adarsh Punj in December 2018
#post() function is under development
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from pynotify import pynotify
from random import randint
from strings import string
import time
import requests
import os
driver = None
def sleep(x):
time.sleep(x)
def configure_environment():
global driver
options = Options()
options.add_argument('--headless')
options.add_argument('--incognito')
options.add_argument('disable-extensions')
options.add_argument(string().user_agent)
driver = webdriver.Chrome(string().chromedriver_path
,chrome_options=options)
def login():
driver.get("https://www.instagram.com/accounts/login")
login_credentials = driver.find_elements_by_css_selector(
"._2hvTZ.pexuQ.zyHYP")
login_credentials[0].send_keys(string().username)
sleep(1)
login_credentials[1].send_keys(string().password)
sleep(1)
click_login = driver.find_elements_by_css_selector(
"._0mzm-.sqdOP.L3NKy")[1].click()
sleep(3)
pynotify("Instagram Bot","Logged into Instagram").push()
def post():
pynotify("Instagram Bot,"Post function is under development").push()
def scroll_following_list():
driver.get("https://www.instagram.com/"+string().username)
following = driver.find_elements_by_css_selector(".-nal3")[2].click()
sleep(5)
first_scroll_timeout = time.time()+5
#Increase total_scroll_timeout acccording to the number of accounts you follow.
total_scroll_timeout = time.time()+25
driver.find_element_by_css_selector("._7UhW9.xLCgt.MMzan._0PwGv.fDxYl").click()
while time.time()<first_scroll_timeout:
ActionChains(driver).send_keys(Keys.DOWN).perform()
sleep(0.1)
driver.find_element_by_css_selector(".wFPL8").click()
while time.time()<total_scroll_timeout:
ActionChains(driver).send_keys(Keys.DOWN).perform()
def print_following_list():
scroll_following_list()
users = driver.find_elements_by_css_selector(".FPmhX.notranslate._0imsa")
for i in range(0,len(users)):
print users[i].get_attribute('title')
print "TOTAL FOLLOWERS: "+str(len(users))
def follow(account):
driver.get("https://instagram.com/"+account)
try:
driver.find_element_by_css_selector("._5f5mN.jIbKX._6VtSN.yZn4P").click()
except:
pynotify("INSTAGRAM BOT","You might be following @"+account)
def unfollow(account):
driver.get("https://instagram.com/"+account)
driver.find_element_by_css_selector("._5f5mN.-fzfL._6VtSN.yZn4P").click()
try:
driver.find_element_by_css_selector(".aOOlW.-Cab_").click()
except:
pynotify("INSTAGRAM BOT","You probably weren't following @"+account)
driver.find_element_by_css_selector("._5f5mN.-fzfL._6VtSN.yZn4P").click()
def follow_fans_of(page):
total_follows = 0
pynotify("INSTAGRAM BOT","Targetting page @"+page).push()
driver.get("https://www.instagram.com/"+page)
driver.find_element_by_css_selector(".Nnq7C.weEfm").click()
sleep(3)
try:
driver.find_element_by_css_selector("._0mzm-.sqdOP.yWX7d._8A5w5").click()
except:
pynotify("Instagram Bot"
,"Error while targetting user @"+page+". Unable to fetch likes list").push()
time.sleep(3)
follow_buttons = driver.find_elements_by_css_selector("._0mzm-.sqdOP.L3NKy")
for button in range(0,len(follow_buttons)):
if(follow_buttons[button].text=="Follow"):
try:
follow_buttons[button].click()
pynotify("Instagram Bot","Followed a user").push()
total_follows = total_follows+1
sleep(randint(20,50))
except:
pynotify("Instagram Bot","Error while targetting user @"+page).push()
break
pynotify("Instagram Bot","Total Follows in the session: "+str(total_follows)).push()
def follow_users():
for page in string().target_pages:
try:
follow_fans_of(page)
except:
follow_fans_of(string().target_pages[
string().target_pages.index(page)+1])
sleep(randint(30,40))
def unfollow_users(limit):
scroll_following_list()
users_unfollowed = 0
unfollow_button = driver.find_elements_by_css_selector("._0mzm-.sqdOP.L3NKy._8A5w5")
sleep(3)
for i in range(1,len(unfollow_button)):
sleep(randint(30,40))
driver.execute_script("arguments[0].click();", unfollow_button[-i])
sleep(1)
confirm_unfollow_button = driver.find_element_by_css_selector(".aOOlW.-Cab_")
confirm_unfollow_button.click()
pynotify("Instagram Bot","Unfollowed a user").push()
users_unfollowed = users_unfollowed+1
if users_unfollowed == limit:
os.system("say "+str(users_unfollowed)+"unfollowed. Aborting the function.")
driver.get("https://instagram.com/")
break
def retrieve_data_from_access_tool(data_name):
data_list = []
driver.get("https://www.instagram.com/accounts/access_tool/"+data_name)
while True:
try:
driver.find_element_by_css_selector("._0mzm-.sqdOP.L3NKy").click()
sleep(0.5)
except:
break
try:
element_list = driver.find_elements_by_class_name("-utLf")
for element in element_list:
data_list.append(element.text)
except:
pass
return data_list
def get_summary():
accounts_you_follow = retrieve_data_from_access_tool(
"accounts_you_follow")
accounts_following_you = retrieve_data_from_access_tool(
"accounts_following_you")
current_follow_requests = retrieve_data_from_access_tool(
"current_follow_requests")
size = 0
print "ACCOUNTS YOU FOLLOW: "+str(len(accounts_you_follow))
for accounts in accounts_you_follow:
print accounts
size = size+1
print "TOTAL: "+str(size)
size = 0
print '\n'
print "ACCOUNTS FOLLOWING YOU: "
for accounts in accounts_following_you:
print accounts
size = size+1
print "TOTAL: "+str(size)
size = 0
print '\n'
print "YOU BOTH FOLLOW EACH OTHER:"
for accounts in accounts_following_you:
if accounts in accounts_you_follow:
print accounts
size = size+1
print "TOTAL: "+str(size)
size = 0
print '\n'
print "ACCOUNTS THAT ARE NOT FOLLOWING YOU: "
for accounts in accounts_you_follow:
if accounts in accounts_following_you:
pass
else:
print accounts
size = size+1
print "TOTAL: "+str(size)
size = 0
print '\n'
print "CURRENT FOLLOW REQUESTS YOU HAVE MADE:"
for accounts in current_follow_requests:
print accounts
size=size+1
print "TOTAL: "+str(size)
def unfollow_accounts_not_following():
accounts_you_follow = retrieve_data_from_access_tool(
"accounts_you_follow")
accounts_following_you = retrieve_data_from_access_tool(
"accounts_following_you")
for accounts in accounts_you_follow:
if accounts in accounts_following_you:
pass
else:
unfollow(account)
sleep(2)
def withdraw_pending_requests():
current_follow_requests = retrieve_data_from_access_tool("current_follow_requests")
for account in reversed(current_follow_requests):
sleep(2)
unfollow(account)