-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIG_Autolike.py
318 lines (260 loc) · 11.6 KB
/
IG_Autolike.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import argparse
import yaml
import instaloader
import threading
import signal
import sys
from instapy import InstaPy
from instapy.util import web_address_navigator, get_relationship_counts
from instapy.like_util import get_links_from_feed, check_link, like_image, verify_liking
from selenium.common.exceptions import NoSuchElementException
from colorama import Style, Fore
from typing import List, Set
def Ig_Auto_Like(
maxLikes : int = 2000,
minLikes : int = 10,
maxFollowers : int = 10000,
minFollowing : int = 30,
minFollowers : int = 30,
whiteList : List[str] = [],
unfollowWhiteList : List[str] = []
):
# similar implementation as InstaPy.like_by_feed_generator()
# autolike instagram posts and manage followees if specified
session.login()
postsLiked : int = 0
numOfSearch : int = 0
linkNotFoundLoopError : int = 0
history : List[str] = []
alreadyLiked : int = 0
whiteListLike : int = 0
postByNonFollowees : int = 0
breakOuterLoop : bool = False
unfollowWhiteList = set(unfollowWhiteList)
while (postsLiked < NUM_POSTS):
if (breakOuterLoop):
break
try:
links = get_links_from_feed(
session.browser, NUM_POSTS, numOfSearch, session.logger
)
if len(links) > 0:
linkNotFoundLoopError = 0
if len(links) == 0:
linkNotFoundLoopError += 1
if linkNotFoundLoopError >= 10:
session.logger.warning(
"Loop error, 0 links"
" for 10 times consecutively, exit loop"
)
break
except NoSuchElementException:
session.logger.warning("Too few images, aborting")
session.aborting = True
return
numOfSearch += 1
for _, link in enumerate(links):
if postsLiked == NUM_POSTS:
breakOuterLoop = True
break
if link in history:
session.logger.info(
"This link has already been visited: {}".format(
link)
)
continue
else:
session.logger.info("New link found...")
history.append(link)
session.logger.info(
"[{} posts liked / {} amount]".format(
postsLiked, NUM_POSTS)
)
session.logger.info(link)
try:
(
inappropriate,
userName,
isVideo,
reason,
scope,
) = check_link(
session.browser,
link,
session.dont_like,
session.mandatory_words,
session.mandatory_language,
session.is_mandatory_character,
session.mandatory_character,
session.check_character_set,
session.ignore_if_contains,
session.logger,
)
except KeyError as e:
print(Fore.RED + "KEYERROR EXCEPTION: {}".format(e))
print(
"This is likely due to the current InstaPy library implementation. Try run `pip3 install -I https://github.com/schealex/InstaPy/zipball/develop` to install the fix. If you believe this is not the cause, comment out this exception handler." + Style.RESET_ALL
)
print("For more information, refer to https://github.com/timgrossmann/InstaPy/issues/6191 and https://github.com/timgrossmann/InstaPy/pull/6195")
breakOuterLoop = True
break
except Exception as ex:
session.logger.info("EXCEPTION ENCOUNTERED: {}, continuing...".format(ex))
continue
if whiteListLike < NUM_POSTS / 3 and userName in whiteList:
session.logger.info("{} is in the whitelist".format(userName))
likeState, msg = like_image(
session.browser,
userName,
session.blacklist,
session.logger,
session.logfolder,
postsLiked,
)
if likeState is True:
postsLiked += 1
whiteListLike += 1
session.jumps["consequent"]["likes"] = 0
else:
alreadyLiked += 1
continue
if (not CONTACTS_RETRIEVED):
print(Fore.RED + "THREADINFO | Auto Like Thread Waiting For Secure Contacts Set" + Style.RESET_ALL)
CONTACTS_EVENT.wait()
print(Fore.GREEN + "THREADINFO | Auto Like Thread Resuming" + Style.RESET_ALL)
if userName not in SELF_FOLLOWEES:
postByNonFollowees += 1
session.logger.warning("{} is not a followee, skipping...".format(userName))
if postByNonFollowees > NUM_POSTS / 8:
session.logger.info("{} posts by non followees in feed, aborting".format(postByNonFollowees))
breakOuterLoop = True
break
if userName not in SECURE_CONTACTS:
session.logger.info("User Name not in secure contacts, skipping...")
continue
if isVideo or inappropriate:
session.logger.info("Post is video or inappropriate, skipping...")
continue
session.liking_approved = verify_liking(
session.browser,
maxLikes,
minLikes,
session.logger,
)
usrFollowerCnt, usrFollowingCnt = get_relationship_counts(
session.browser, userName, session.logger
)
if (usrFollowerCnt > maxFollowers or usrFollowerCnt < minFollowers or usrFollowingCnt < minFollowing):
session.logger.info("User follower / following count out of range, skipping...")
continue
minLikes = max(minLikes, usrFollowerCnt / 30)
if session.liking_approved:
# validate user
validation, details = session.validate_user_call(userName)
if validation is not True:
session.logger.info(details)
not_valid_users += 1
continue
else:
web_address_navigator(session.browser, link)
# try to like
likeState, msg = like_image(
session.browser,
userName,
session.blacklist,
session.logger,
session.logfolder,
postsLiked,
)
if likeState is True:
postsLiked += 1
session.jumps["consequent"]["likes"] = 0
else:
alreadyLiked += 1
if alreadyLiked >= NUM_POSTS / 3:
session.logger.info("Too much already liked posts, terminating")
session.logger.info("Already liked {} / Amount {}".format(alreadyLiked, NUM_POSTS))
breakOuterLoop = True
break
session.logger.info("Finished Liking {} / {} Posts".format(postsLiked, NUM_POSTS))
if (ARGS.unfollow):
if (not CONTACTS_RETRIEVED):
print(Fore.RED + "THREADINFO | Auto Like Thread (UNFOLLOW) Waiting For Secure Contacts Set" + Style.RESET_ALL)
CONTACTS_EVENT.wait()
print(Fore.GREEN + "THREADINFO | Auto Like Thread (UNFOLLOW) Resuming" + Style.RESET_ALL)
Manage_Contacts(unfollowWhiteList)
def Get_Secure_Contacts():
# get list of users that are both follower and followee
global SECURE_CONTACTS
global CONTACTS_RETRIEVED
global SELF_FOLLOWERS
global SELF_FOLLOWEES
print(Fore.GREEN + "THREADINFO | Getting Secure Contacts" + Style.RESET_ALL)
loader = instaloader.Instaloader()
loader.login(USERNAME, PASSWORD)
profile = instaloader.Profile.from_username(loader.context, USERNAME)
followees = profile.get_followees()
followers = profile.get_followers()
SELF_FOLLOWERS = {f.username for f in followers}
SELF_FOLLOWEES = {f.username for f in followees}
SECURE_CONTACTS = SELF_FOLLOWEES.intersection(SELF_FOLLOWERS)
CONTACTS_RETRIEVED = True
CONTACTS_EVENT.set()
print(Fore.GREEN + "THREADINFO | Thread Finished Processing Secure Contacts" + Style.RESET_ALL)
def Manage_Contacts(unfollowWhiteList : Set[str]):
# unfollow nonfollowers followed by the user
toUnfollow : List[str] = []
nonfollower = SELF_FOLLOWEES.difference(SELF_FOLLOWERS)
for userName in nonfollower:
if userName not in unfollowWhiteList:
usrFollowerCnt, usrFollowingCnt = get_relationship_counts(
session.browser, userName, session.logger
)
if usrFollowerCnt < cfg["maxFollowers"]:
toUnfollow.append(userName)
session.logger.info("{} will be unfollowed".format(userName))
else:
session.logger.info("User {} in unfollow white list, skipping".format(userName))
session.unfollow_users(
amount = len(toUnfollow),
custom_list_enabled = True,
custom_list = toUnfollow,
custom_list_param = "all",
style = "RANDOM",
unfollow_after = None,
sleep_delay = 60
)
def Browser_Signal_Handler(sig, frame):
# Signal handler for keyboard interruption
session.logger.info("Process Terminated through SIGINT")
sys.exit(0)
def Main():
# Main thread
contactThread = threading.Thread(target = Get_Secure_Contacts)
autoLikeThread = threading.Thread(target = Ig_Auto_Like, kwargs = cfg)
contactThread.daemon = True
contactThread.start()
autoLikeThread.start()
contactThread.join()
autoLikeThread.join()
if __name__ == "__main__":
signal.signal(signal.SIGINT, Browser_Signal_Handler)
ARG_PARSER = argparse.ArgumentParser()
ARG_PARSER.add_argument("-u", "--username", help = "your ig username", required = True)
ARG_PARSER.add_argument("-p", "--password", help = "your ig password", required = True)
ARG_PARSER.add_argument("-a", "--amount", type = int, help = "posts to like", required = False, default = 0)
ARG_PARSER.add_argument("-f", "--unfollow", help = "whether to unfollow nonfollowers", action='store_true')
ARGS = ARG_PARSER.parse_args()
USERNAME = ARGS.username
PASSWORD = ARGS.password
NUM_POSTS = ARGS.amount
with open('Config.yaml', 'r') as cfgFile:
cfg = yaml.load(cfgFile, yaml.SafeLoader)
CONTACTS_RETRIEVED = False
CONTACTS_EVENT = threading.Event()
SECURE_CONTACTS : Set[str] = set()
SELF_FOLLOWERS : Set[str] = set()
SELF_FOLLOWEES : Set[str] = set()
session = InstaPy(username= USERNAME, password= PASSWORD)
Main()
session.browser.close()