Like and comment Instagram Bot

The bot use instagrapi library to like and comment posts from given hashtag it can work with multiple accounts at a time.
mail@pastecode.io avatar
unknown
python
3 years ago
4.2 kB
3
Indexable
Never
from instagrapi import Client
from instagrapi.exceptions import LoginRequired, UnknownError, FeedbackRequired, UserNotFound, ClientError, ClientConnectionError
import time
import random

cl_list = list()
cl_list_final =list()
account_name = list()
hastg_list = list()
hstg_list_name = list()
hastag_list_final = list()
media_id_hash = str('m')
medi_id_hstg_list = list()
usernames_lst = list()
comment_files = list()
comment_lst = list()
fl_list = list()
count_like = int(0)
count_comment = int(0)
newcl = str('cl')
hstg = str('h')
fl = str('fl')
data_comment = str('datacomment')


with open('accounts.txt','r') as file:
	acc_data = [line.strip() for line in file]


for each in range(int(len(acc_data) / 2)):
	newcl = newcl + str(each)
	cl_list.append(newcl)
	newcl = 'cl'

for each_usr,each_pass,cl_name in zip(acc_data[0::2], acc_data[1::2], cl_list):
	ACCOUNT_USERNAME = each_usr
	ACCOUNT_PASSWORD = each_pass
	account_name.append(ACCOUNT_USERNAME)
	try:
		cl_name = Client()
		cl_name.login(ACCOUNT_USERNAME, ACCOUNT_PASSWORD)
		cl_name.request_timeout = 15
		cl_list_final.append(cl_name)
		usernames_lst.append(ACCOUNT_USERNAME)
		print('Login with ', ACCOUNT_USERNAME)
	except:
		print('Can`t login with', ACCOUNT_USERNAME)

## Hashtag input by user.
for each in range(int(len(acc_data) / 2)):
	hastg = str(input('Please enter hashtag for account (one at a time) and press ENTER: '))
	hastg_list.append(hastg)


amount = int(input('Please enter amount of post to load from each hashtag eg. 90: '))


## Hashtag input by user.
for each in range(int(len(acc_data) / 2)):
	cmment = str(input('Please enter text file(must be end with .txt) with comments for accounts (one at a time) and press ENTER: '))
	comment_files.append(cmment)


print('Please wait to load the posts from hashtag.It can take a wile.')


## name of the hastag variable
for each in range(int(len(acc_data) / 2)):
	hstg = hstg + str(each)
	hstg_list_name.append(hstg)
	hstg = 'h'

## assing hashtag data in list in final list with the lists
for each_hstg, each_hstg_nm, each_cl in zip(hastg_list, hstg_list_name, cl_list_final):
	each_hstg_nm = each_cl.hashtag_medias_recent(each_hstg, amount)
	hastag_list_final.append(each_hstg_nm)

#print(len(hastag_list_final), 'list_final')

## list of number of hastag id list
for each in range(int(len(hastag_list_final))):
	media_id_hash += str(each)
	medi_id_hstg_list.append(media_id_hash)

listlist = list()
for each in medi_id_hstg_list:
	each = list()
	listlist.append(each)

#print(len(medi_id_hstg_list), 'len medi')

hashtag_sep_list = list()

for each_hstg_lst,each_id_hash in zip(hastag_list_final, listlist):
	for each in range(len(each_hstg_lst)):
		each_id_hash.append(each_hstg_lst[each].id)
	
	hashtag_sep_list.append(each_id_hash)

#print(each_id_hash, len(each_id_hash))

## file name ext
for each in range(int(len(acc_data) / 2)):
	fl = fl + str(each)
	fl_list.append(fl)
	fl = 'fl'


for each in range(int(len(acc_data) / 2)):
	data_comment = data_comment + str(each)
	comment_lst.append(data_comment)
	data_comment = 'datacomment'

comment_list_final = list()
## Open files with comments and save them in list
for each, each_nm, each_data  in zip(comment_files, fl_list, comment_lst):
	with open(each, 'r') as each_nm:
		each_data = [line.strip() for line in each_nm]
	comment_list_final.append(each_data)

## send likes and comments from each account hashtag comment file.
for each_cl, acc_name, hastag_nm, each_comnt, each_id_list in zip(cl_list_final, usernames_lst, hastg_list, comment_list_final, hashtag_sep_list):	
	for each_id in each_id_list:
		try:
			like = each_cl.media_like(each_id)
			count_like += 1
			print(acc_name + ' like a picture from hashtag: ' + hastag_nm)
		except UnknownError as e:
			continue
		except FeedbackRequired:
			continue
		except UserNotFound:
			continue
		except ClientError:
			continue
		try:
			comnt = random.choice(each_comnt)
			comment = each_cl.media_comment(each_id, comnt)
			count_comment += 1
			print(acc_name + ' send a comment: ' + comnt + ' to hashtag: ' + hastag_nm)
		except UnknownError as e:
			continue
		except FeedbackRequired:
			continue
		except UserNotFound:
			continue
		except ClientError:
			continue