-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbots.py
More file actions
315 lines (300 loc) · 9.74 KB
/
bots.py
File metadata and controls
315 lines (300 loc) · 9.74 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
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as b
import time
import csv
import pandas as pd
import datetime
import random
from dateutil.parser import parse
def followersFollowing(gp, user):
followers, followersTag = gp.get_followers()
if len(followers) < gp.get_num_flw("followers", user) - 2:
print("DIDN'T LOAD ALL FOLLOWERS")
exit()
print("DONE GETTING FOLLOWERS")
dfList = {'followers': followers,'followersTag': followersTag}
df = pd.DataFrame(dfList, columns=['followers', "followersTag"])
df.to_csv('followers.csv')
print("DONE SAVING")
following, followingTag = gp.get_following()
if len(following) < gp.get_num_flw("following", user) - 2:
print("DIDN'T LOADALL FOLLOWING")
exit()
print("DONE GETTING FOLLOWING")
dfList2 = {'following': following,'followingTag': followingTag}
df2 = pd.DataFrame(dfList2, columns=['following', "followingTag"])
df2.to_csv('following.csv')
print("DONE SAVING")
return followers, followersTag, following, followingTag
import sys, select
def timeout_input(prompt, timeout=3, default=""):
print(prompt, end=': ', flush=True)
inputs, outputs, errors = select.select([sys.stdin], [], [], timeout)
print()
return (0, sys.stdin.readline().strip()) if inputs else (-1, default)
def unfollow_bot(gDict, gp, max_follows):
print('accounts targeted')
daysPadding = 7
t = time.time()
# how many pages we likes/followed
L = 0
F = 0
now = datetime.datetime.now()
for key, value in gDict.d.items():
if value["they_still_following"] == False and value["i_still_following"] == True:
if timeout_input("Press enter to stop: ") == (-1, ''):
# Todo: Make sure not rescenct follow(past week)
follow_date = parse(value["date_i_followed"])
print(f"\n{key}:\nFollow date: {value['date_i_followed']}")
if now - datetime.timedelta(daysPadding) > follow_date:
gp.driver.get('https://www.instagram.com' + key)
# key[1:-1] because "/user/" needs to be "user"
num_flw = gp.get_num_flw("followers", key[1:-1])
max_followers = 3000
if num_flw < max_followers:
print(f"Num flws(less than {max_followers}): {num_flw}")
time.sleep(2)
print(f'current unfollows: {str(F)} of {max_follows}')
if F < max_follows:
time.sleep(random.randint(5,10))
try:
if gp.unfollow_page():
print(f'{key}: unfollowed successfully')
if gDict.unfollow(key):
print("Updated dict succesfully")
else:
print("Updated dict error")
F += 1
else:
print(f'Could not unfollow(problem finding selenium elements)')
except:
print(f'{key}: could not unfollow')
else:
return
# time.sleep(3600)
else:
print(f"Followed less than {daysPadding} days ago...")
else:
return
# def like(gp):
# if gp.is_public():
# # key[1:-1] because "/user/" needs to be "user"
# # num_flw = gp.get_num_flw("followers", key[1:-1])
# time.sleep(2)
# print('current likes: ' + str(L))
# print(f'{key}: attempting to like')
# time.sleep(random.randint(5,10))
# try:
# photo = gp.like_post()
# if photo:
# print(f'{key}: liked successfully')
# # Not yet implimented
# gDict.like(key,photo)
# L += 1
# except:
# print(f'{key}: could not like')
def like_bot(gDict, gp, max_likes):
print('LIKING')
t = time.time()
#how many pages we likes / followed
L = 0
F = 0
# This is a temporary fix. Not neccesary because I will be liking newer photos. not just people who I havent liked their photos
pics_len = 1
while L < max_likes:
# for key, value in gDict.d.items():
key, value = random.choice(list(gDict.d.items()))
# timeBefore = time.time()
if value["i_still_following"]:# and len(value["likedPhotos"]) < pics_len:
if timeout_input("Press enter to stop: ") == (-1, ''):
gp.driver.get('https://www.instagram.com' + key)
if gp.is_public():
# key[1:-1] because "/user/" needs to be "user"
# num_flw = gp.get_num_flw("followers", key[1:-1])
time.sleep(2)
print('current likes: ' + str(L))
print(f'{key}: attempting to like')
time.sleep(random.randint(5,10))
try:
photo = gp.like_post()
if photo:
print(f'{key}: liked successfully')
# Not yet implimented
gDict.like(key,photo)
L += 1
except:
print(f'{key}: could not like')
else:
return
pics_len += 1
return
#global href dict of everyone I followed and who follows me back
# usersDictList = {
# "href": "/test/",
# "info": {"href": "/test/","date_i_followed":"01-01-2020","date_they_followed":None,"date_unfollow":"01-07-2020","liked_photos":["id's"]}
# }
def follow_bot(goog, gDict, gp, max_follows = 40):
# List of people to follow from other channels
listToFollow = goog.toFollowGet()#= ["/test1/", "/test2/"]
with open('kurzgesagtFollowers.csv') as f:
listToFollow = f.readlines()
print(len(listToFollow))
print('accounts targeted')
t = time.time()
#how many pages we likes / followed
L = 0
F = 0
for hrefL in listToFollow[1:]:
# href = hrefL.split(':')[0][1:-1]# google
href = hrefL.split(',')[1]# json
if href not in gDict.d:
gp.driver.get('https://www.instagram.com' + href)
time.sleep(2)
if timeout_input("Press enter to stop: ") == (-1, ''):#gp.get_num_flw("followers", href[1:-1]) < 5000:
print('current follows: ' + str(F))
if F < max_follows:
time.sleep(random.randint(5,10))
# try:
if gp.follow_page(href) == 1:
print(f"{href}: Followed succesfully.")
F += 1
if gDict.followed(href) == 1:
print(f"{href}: Saved new following.")
else:
print(f"{href}: ERROR saving new following.")
# Like post here!
else:
print(f"{href}: ERROR following page.")
# except:
# print(f"{href}: ERROR following.")
else:
return
else:
return
# else:
# time.sleep(3600)
# def follow(goog, gDict, gp):
# toFollow = goog.toFollowGet()
# for href in toFollow:
# if href not in gDict.d:
# if gp.follow(href) == True:
# print(f"{href}: Followed succesfully.")
# if gDict.followed(href):
# print(f"{href}: Saved new following.")
# else:
# print(f"{href}: ERROR saving new following.")
# else:
# print(f"{href}: ERROR could not follow...")
def run_old__bot(refs, driver, gp):
print(len(refs))
print('accounts targeted')
t = time.time()
#how many pages we likes / followed
L = 0
F = 0
for r in refs:
# https://www.instagram.com
time.sleep(2)
if gp.get_num_flw("followers", r) < 3000:
if gp.is_public():
print('public account')
print('current likes: ' + str(L))
if L < max_likes:
try:
gp.like_post()
L += 1
print("POST LIKED")
except:
print('could not like..lets follow instead')
try:
gp.follow_page()
print('page followed successfully')
F += 1
except:
print('could not follow')
else:
time.sleep(3600) # time.sleep(3600 - (time.time() - t)) -> t= time.time()
else:
print('account is private')
print('current follows: ' + str(F))
if F < max_follows:
time.sleep(2)
try:
gp.follow_page()
print('page followed successfully')
F += 1
except:
print('could not follow')
else:
time.sleep(3600)
def checkRefollow(gDict):
refollow = 1
for key, value in gDict.d.items():
if value["refollowed"] > 1:
print(f"{key}: {value}")
refollow+=1
print(f"\nTotal refollowed: {refollow}\n")
# def checkNew(gDict):
# check = 1
# for key, value in gDict.d.items():
# if value["date_i_followed"] == "2020-08-19":
# print(f"{key}: {value}")
# check+=1
# print(f"\nTotal: {check}\n")
def followUnfollowLikeHour(gDict, gp, max_follows, max_likes):
i = 0
while True:
print(f"Ran {i} times:\n")
i+=1
unfollow_bot(gDict, gp, max_follows)
follow_bot(goog, gDict, gp, max_follows)
like_bot(gDict, gp, max_likes)
if timeout_input("MAIN LOOP: Press enter to stop: ",3600) != (-1, ''):
break
def unfollowHour(gDict, gp, max_follows):
i = 0
while True:
print(f"Ran {i} times:\n")
i+=1
unfollow_bot(gDict, gp, max_follows)
if timeout_input("MAIN LOOP: Press enter to stop: ",3600) != (-1, ''):
break
def checkUnfollow(gDict, gp, max_follows):
total = 0
for key, value in gDict.d.items():
if value["date_unfollow"] == "2020-09-13":
total +=1
cur = 0
for key, value in gDict.d.items():
if value["date_unfollow"] == "2020-09-13":
cur+=1
check_unfollowed = ""
print(f"\n{cur}/{total}\n{key}: {value}")
gp.driver.get(f'https://www.instagram.com{key}')
source = gp.driver.page_source
if "Sorry, this page isn't available." not in source:
try:
check_unfollowed = WebDriverWait(gp.driver, 10).until(EC.presence_of_element_located((By.XPATH, "//header//button[text()='Follow']")))
except:
check_unfollowed = ""
if check_unfollowed == "":
input("Check insta?: ")
else:
print("User does not exist.")
# ### Get button elements ####
# >>> popup = WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[4]/div/div/div[2]')))
# >>> buttons = popup.find_elements_by_xpath("//button[text()='Following']")
# better: buttons = popup.find_elements_by_xpath("//li[@class='wo9IH']//button[text()='Following']")
# ### another
# links = popup.find_elements_by_xpath("//li[@class='wo9IH']")
# print(f"\nTotal refollowed: {refollow}\n")
# source = gp.driver.page_source
# if "Sorry, this page isn't available." not in source:
# print("not found")
# else:
# print("found")
# check_unfollowed = WebDriverWait(gp.driver, 10).until(EC.presence_of_element_located((By.XPATH, "//header/*/button[text()='Follow']")))