Skip to content

Commit 8c11098

Browse files
committed
WOW! InstaBot go to v 1.0.
Add new function new_auto_mod() - now bot can like, follow, infollow and write comments! Test it about week with 1000 likes, 150 follow, 150 unfollow and 50 comments at day - all work great! For newbie add example.py with all they need to start. Will write all changes in readme from few days.
1 parent 6cd4158 commit 8c11098

File tree

2 files changed

+193
-15
lines changed

2 files changed

+193
-15
lines changed

example.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from instabot import InstaBot
2+
3+
bot = InstaBot(login="your_login", password="your_password",
4+
like_per_day=1000,
5+
max_like_for_one_tag=5,
6+
follow_per_day=150,
7+
follow_time=5*60*60,
8+
unfollow_per_day=150,
9+
comments_per_day=50,
10+
tag_list=['girl', 'car', 'cat'],
11+
log_mod=0)
12+
13+
bot.new_auto_mod()

instabot.py

+180-15
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import datetime
55
import logging
66
import json
7+
import atexit
8+
import itertools
79

810
class InstaBot:
911
"""
10-
Instagram bot v 0.05
12+
Instagram bot v 1.0
1113
like_per_day=1000 - How many likes set bot in one day.
1214
1315
media_max_like=10 - Don't like media (photo or video) if it have more than
@@ -47,8 +49,14 @@ class InstaBot:
4749
# If InstaBot think you have banned - going to sleep.
4850
ban_sleep_time = 2*60*60
4951

50-
# All likes counter.
51-
like_conter = 0
52+
# All counter.
53+
like_counter = 0
54+
follow_counter = 0
55+
unfollow_counter = 0
56+
comments_counter = 0
57+
58+
# List of user_id, that bot follow
59+
bot_follow_list = []
5260

5361
# Log setting.
5462
log_file_path = '/var/www/python/log/'
@@ -58,16 +66,43 @@ class InstaBot:
5866
media_by_tag = 0
5967
login_status = False
6068

69+
# For new_auto_mod
70+
next_iteration = {"Like": 0, "Follow": 0, "Unfollow": 0, "Comments": 0}
71+
6172
def __init__(self, login, password,
6273
like_per_day=1000,
6374
media_max_like=10,
6475
media_min_like=0,
76+
follow_per_day=0,
77+
follow_time=5*60*60,
78+
unfollow_per_day=0,
79+
comments_per_day=0,
6580
tag_list=['cat', 'car', 'dog'],
6681
max_like_for_one_tag = 5,
6782
log_mod = 0):
68-
self.like_per_day = like_per_day
83+
6984
self.time_in_day = 24*60*60
70-
self.like_delay = self.time_in_day / self.like_per_day
85+
# Like
86+
self.like_per_day = like_per_day
87+
if self.like_per_day != 0:
88+
self.like_delay = self.time_in_day / self.like_per_day
89+
90+
# Follow
91+
self.follow_time = follow_time
92+
self.follow_per_day = follow_per_day
93+
if self.follow_per_day != 0:
94+
self.follow_delay = self.time_in_day / self.follow_per_day
95+
96+
# Unfollow
97+
self.unfollow_per_day = unfollow_per_day
98+
if self.unfollow_per_day != 0:
99+
self.unfollow_delay = self.time_in_day / self.unfollow_per_day
100+
101+
# Comment
102+
self.comments_per_day = comments_per_day
103+
if self.comments_per_day != 0:
104+
self.comments_delay = self.time_in_day / self.comments_per_day
105+
71106
# Don't like if media have more than n likes.
72107
self.media_max_like = media_max_like
73108
# Don't like if media have less than n likes.
@@ -85,13 +120,26 @@ def __init__(self, login, password,
85120
self.user_login = login.lower()
86121
self.user_password = password
87122

123+
self.media_by_tag = []
124+
88125
now_time = datetime.datetime.now()
89-
log_string = 'Insta Bot v0.05 start at %s:' %\
126+
log_string = 'Insta Bot v1.0 start at %s:' %\
90127
(now_time.strftime("%d.%m.%Y %H:%M"))
91128
self.write_log(log_string)
92129
self.login()
93130

94-
def __del__ (self):
131+
atexit.register(self.cleanup)
132+
133+
def cleanup (self):
134+
# Unfollow all bot follow
135+
if len(self.bot_follow_list)>0:
136+
for f in self.bot_follow_list:
137+
log_string = "Try to unfollow: %s" % (f[0])
138+
self.write_log(log_string)
139+
self.unfollow(f[0])
140+
self.bot_follow_list.remove(f)
141+
142+
# Logout
95143
if (self.login_status):
96144
self.logout()
97145

@@ -137,7 +185,9 @@ def login(self):
137185

138186
def logout(self):
139187
now_time = datetime.datetime.now()
140-
log_string = 'Insta Bot logout, like count %i.' % self.like_conter
188+
log_string = 'Logout: likes - %i, follow - %i, unfollow - %i, comments - %i.' %\
189+
(self.like_counter, self.follow_counter,
190+
self.unfollow_counter, self.comments_counter)
141191
self.write_log(log_string)
142192

143193
try:
@@ -180,7 +230,7 @@ def get_media_id_by_tag (self, tag):
180230
else:
181231
return 0
182232

183-
def like_all_exist_media (self, media_size=-1):
233+
def like_all_exist_media (self, media_size=-1, delay=True):
184234
""" Like all media ID that have self.media_by_tag """
185235

186236
if (self.login_status):
@@ -205,10 +255,10 @@ def like_all_exist_media (self, media_size=-1):
205255
if like.status_code == 200:
206256
# Like, all ok!
207257
self.error_400 = 0
208-
self.like_conter += 1
258+
self.like_counter += 1
209259
log_string = "Liked: %s. Like #%i." %\
210260
(self.media_by_tag[i]['id'],
211-
self.like_conter)
261+
self.like_counter)
212262
self.write_log(log_string)
213263
elif like.status_code == 400:
214264
log_string = "Not liked: %i" \
@@ -226,9 +276,11 @@ def like_all_exist_media (self, media_size=-1):
226276
self.write_log(log_string)
227277
# Some error.
228278
i += 1
229-
time.sleep(self.like_delay*0.9 +
279+
if delay:
280+
time.sleep(self.like_delay*0.9 +
230281
self.like_delay*0.2*random.random())
231-
#else:
282+
else:
283+
return True
232284
# This media have to many likes!
233285
else:
234286
self.write_log("No media to like!")
@@ -239,6 +291,7 @@ def like(self, media_id):
239291
url_likes = self.url_likes % (media_id)
240292
try:
241293
like = self.s.post(url_likes)
294+
last_liked_media_id = media_id
242295
except:
243296
self.write_log("Exept on like!")
244297
like = 0
@@ -257,11 +310,15 @@ def unlike(self, media_id):
257310

258311
def comment(self, media_id, comment_text):
259312
""" Send http request to comment """
260-
if (self.login_status and comment_text!=""):
313+
if (self.login_status):
261314
comment_post = {'comment_text' : comment_text}
262315
url_comment = self.url_comment % (media_id)
263316
try:
264317
comment = self.s.post(url_comment, data=comment_post)
318+
if comment.status_code == 200:
319+
self.comments_counter += 1
320+
log_string = 'Write: "%s". #%i.' % (comment_text, self.comments_counter)
321+
self.write_log(log_string)
265322
return comment
266323
except:
267324
self.write_log("Exept on comment!")
@@ -273,17 +330,25 @@ def follow(self, user_id):
273330
url_follow = self.url_follow % (user_id)
274331
try:
275332
follow = self.s.post(url_follow)
333+
if follow.status_code == 200:
334+
self.follow_counter += 1
335+
log_string = "Follow: %s #%i." % (user_id, self.follow_counter)
336+
self.write_log(log_string)
276337
return follow
277338
except:
278339
self.write_log("Exept on follow!")
279340
return False
280341

281342
def unfollow(self, user_id):
282343
""" Send http request to unfollow """
283-
if (self.login_status and user_id>0):
344+
if (self.login_status):
284345
url_unfollow = self.url_unfollow % (user_id)
285346
try:
286347
unfollow = self.s.post(url_unfollow)
348+
if unfollow.status_code == 200:
349+
self.unfollow_counter += 1
350+
log_string = "Unfollow: %s #%i." % (user_id, self.unfollow_counter)
351+
self.write_log(log_string)
287352
return unfollow
288353
except:
289354
self.write_log("Exept on unfollow!")
@@ -298,6 +363,106 @@ def auto_mod(self):
298363
self.like_all_exist_media(random.randint \
299364
(1, self.max_like_for_one_tag))
300365

366+
def new_auto_mod(self):
367+
while True:
368+
# ------------------- Get media_id -------------------
369+
if len(self.media_by_tag) == 0:
370+
self.get_media_id_by_tag(random.choice(self.tag_list))
371+
self.this_tag_like_count = 0
372+
self.max_tag_like_count = random.randint(1, self.max_like_for_one_tag)
373+
# ------------------- Like -------------------
374+
self.new_auto_mod_like()
375+
# ------------------- Follow -------------------
376+
self.new_auto_mod_follow()
377+
# ------------------- Unfollow -------------------
378+
self.new_auto_mod_unfollow()
379+
# ------------------- Comment -------------------
380+
self.new_auto_mod_comments()
381+
382+
# Bot iteration in 1 sec
383+
time.sleep(3)
384+
# print("Tic!")
385+
386+
def new_auto_mod_like(self):
387+
if time.time()>self.next_iteration["Like"] and self.like_per_day!=0 \
388+
and len(self.media_by_tag) > 0:
389+
# You have media_id to like:
390+
if self.like_all_exist_media(media_size=1, delay=False):
391+
# If like go to sleep:
392+
self.next_iteration["Like"] = time.time() +\
393+
self.add_time(self.like_delay)
394+
# Count this tag likes:
395+
self.this_tag_like_count += 1
396+
if self.this_tag_like_count >= self.max_tag_like_count:
397+
self.media_by_tag = [0]
398+
# Del first media_id
399+
del self.media_by_tag[0]
400+
401+
def new_auto_mod_follow(self):
402+
if time.time()>self.next_iteration["Follow"] and \
403+
self.follow_per_day!=0 and len(self.media_by_tag) > 0:
404+
405+
log_string = "Try to follow: %s" % (self.media_by_tag[0]["owner"]["id"])
406+
self.write_log(log_string)
407+
408+
if self.follow(self.media_by_tag[0]["owner"]["id"]) != False:
409+
self.bot_follow_list.append([self.media_by_tag[0]["owner"]["id"],
410+
time.time()])
411+
self.next_iteration["Follow"] = time.time() +\
412+
self.add_time(self.follow_delay)
413+
414+
def new_auto_mod_unfollow(self):
415+
if time.time()>self.next_iteration["Unfollow"] and \
416+
self.unfollow_per_day!=0 and len(self.bot_follow_list) > 0:
417+
for f in self.bot_follow_list:
418+
if time.time() > (f[1] + self.follow_time):
419+
420+
log_string = "Try to unfollow: %s" % (f[0])
421+
self.write_log(log_string)
422+
423+
if self.unfollow(f[0]) != False:
424+
self.bot_follow_list.remove(f)
425+
self.next_iteration["Unfollow"] = time.time() +\
426+
self.add_time(self.unfollow_delay)
427+
428+
def new_auto_mod_comments(self):
429+
if time.time()>self.next_iteration["Comments"] and self.comments_per_day!=0 \
430+
and len(self.media_by_tag) > 0:
431+
432+
comment_text = self.generate_comment()
433+
log_string = "Try to comment: %s" % (self.media_by_tag[0]['id'])
434+
self.write_log(log_string)
435+
if self.comment(self.media_by_tag[0]['id'], comment_text) != False:
436+
self.next_iteration["Comments"] = time.time() +\
437+
self.add_time(self.comments_delay)
438+
439+
def add_time(self, time):
440+
""" Make some random for next iteration"""
441+
return time*0.9 + time*0.2*random.random()
442+
443+
def generate_comment(self):
444+
c_list = list(itertools.product(
445+
["this", "the", "your", "i think", "think",
446+
"i'm sure this"],
447+
["photo", "picture", "pic", "shot", "snapshot",
448+
"exposition"],
449+
["is", "look", "", "feel", "really"],
450+
["great", "super", "good one", "very good",
451+
"good", "so good", "wow", "WOW", "cool",
452+
"GREAT", "magnificent", "magical", "very cool",
453+
"stylish", "so stylish", "beautiful",
454+
"so beautiful", "so stylish", "so professional",
455+
"lovely", "so lovely", "very lovely",
456+
"glorious", "so glorious", "very glorious",
457+
"adorable", "excellent", "amazing"],
458+
[".", "..", "...", "!", "!!", "!!!"]))
459+
460+
repl = [(" ", " "), (" .", "."), (" !", "!")]
461+
res = " ".join(random.choice(c_list))
462+
for s, r in repl:
463+
res = res.replace(s, r)
464+
return res.capitalize()
465+
301466
def write_log(self, log_text):
302467
""" Write log by print() or logger """
303468

0 commit comments

Comments
 (0)