|
|
|
import sys |
|
import time |
|
from instagrapi import Client |
|
|
|
|
|
class _Settings: |
|
ARGS = True |
|
ACCOUNT_USERNAME = '' |
|
ACCOUNT_PASSWORD = '' |
|
TARGET = '' |
|
USER_TO_WATCH = [] |
|
SUCCESS_STEP = 50 |
|
STEP_SLEEP = 1 |
|
BOT_SLEEP = 10 |
|
|
|
|
|
SETTINGS = _Settings() |
|
|
|
|
|
def view_story_by_username(client, target_user): |
|
success = 0 |
|
tmp_user = client.user_info_by_username(target_user) |
|
print(tmp_user.username) |
|
tmp_user_stories = client.user_stories(tmp_user.pk) |
|
|
|
if len(tmp_user_stories) > 0: |
|
print(tmp_user.username + '[{0}] Stories found'.format(len(tmp_user_stories))) |
|
|
|
tmp_seen_stories = [] |
|
for s in tmp_user_stories: |
|
tmp_seen_stories.append(s.id) |
|
|
|
for p in range(len(tmp_seen_stories)): |
|
client.story_seen([tmp_seen_stories[p]]) |
|
print('Story {0} viewed.'.format(p)) |
|
client.story_like(tmp_seen_stories[p]) |
|
print('Story {0} liked.'.format(p)) |
|
success += 1 |
|
|
|
return success |
|
|
|
|
|
def view_story_by_id(client, target_id): |
|
success = 0 |
|
tmp_user = client.user_info(target_id) |
|
print(tmp_user.username + ' - ' + target_id) |
|
tmp_user_stories = client.user_stories(target_id) |
|
|
|
if len(tmp_user_stories) > 0: |
|
print(tmp_user.username + '[{0}] Stories found'.format(len(tmp_user_stories))) |
|
|
|
tmp_seen_stories = [] |
|
for s in tmp_user_stories: |
|
tmp_seen_stories.append(s.id) |
|
|
|
client.story_seen(tmp_seen_stories) |
|
print('Story {0} viewed.'.format(len(tmp_seen_stories))) |
|
client.story_like(tmp_seen_stories[0]) |
|
print('Story 1 liked.') |
|
|
|
success += 1 |
|
|
|
return success |
|
|
|
|
|
def search_from_usernames(client): |
|
success_count = 0 |
|
|
|
print('Search users ..') |
|
for u in USER_TO_WATCH: |
|
success_count += view_story_by_username(client, u) |
|
|
|
print('STEP | {0} Stories seen/liked.'.format(success_count)) |
|
time.sleep(STEP_SLEEP) |
|
|
|
if success_count % SUCCESS_STEP == 0 and success_count > 0: |
|
print('SUCCESS SLEEP.'.format(success_count)) |
|
time.sleep(BOT_SLEEP) |
|
|
|
|
|
def search_from_following(client): |
|
success_count = 0 |
|
following = client.user_following(client.user_id) |
|
print('Search following ..') |
|
for f in following.keys(): |
|
|
|
success_count += view_story_by_id(client, f) |
|
|
|
print('STEP | {0} Stories seen/liked.'.format(success_count)) |
|
time.sleep(STEP_SLEEP) |
|
|
|
if success_count % SUCCESS_STEP == 0 and success_count > 0: |
|
print('SUCCESS SLEEP.'.format(success_count)) |
|
time.sleep(BOT_SLEEP) |
|
|
|
|
|
def search_from_target(client): |
|
tmp_user = client.user_info_by_username(TARGET) |
|
print(tmp_user.username) |
|
|
|
following = client.user_following(tmp_user.pk) |
|
print('Search {0} following ..'.format(tmp_user.username)) |
|
success_count = 0 |
|
|
|
for f in following.keys(): |
|
success_count += view_story_by_id(client, f) |
|
|
|
print('STEP | {0} Stories liked.'.format(success_count)) |
|
time.sleep(STEP_SLEEP) |
|
|
|
if success_count % SUCCESS_STEP == 0 and success_count > 0: |
|
print('SUCCESS SLEEP.'.format(success_count)) |
|
time.sleep(BOT_SLEEP) |
|
|
|
|
|
if __name__ == '__main__': |
|
if SETTINGS.ARGS: |
|
args = sys.argv |
|
args.remove(sys.argv[0]) |
|
if len(args) < 2: |
|
raise Exception('Invalid arguments') |
|
|
|
ACCOUNT_USERNAME = args[0] |
|
ACCOUNT_PASSWORD = args[1] |
|
TARGET = args[2] if len(args) >= 2 else ACCOUNT_USERNAME |
|
USER_TO_WATCH = args[3].split(',') if len(args) >= 4 else SETTINGS.USER_TO_WATCH |
|
SUCCESS_STEP = args[4] if len(args) >= 5 else SETTINGS.SUCCESS_STEP |
|
STEP_SLEEP = args[5] if len(args) >= 6 else SETTINGS.STEP_SLEEP |
|
BOT_SLEEP = args[6] if len(args) >= 7 else SETTINGS.BOT_SLEEP |
|
|
|
try: |
|
print('Starting...') |
|
cl = Client() |
|
print('Try to connect.') |
|
cl.login(SETTINGS.ACCOUNT_USERNAME, SETTINGS.ACCOUNT_PASSWORD) |
|
print('Success.') |
|
|
|
while True: |
|
if SETTINGS.TARGET is not SETTINGS.ACCOUNT_USERNAME: |
|
search_from_target(cl) |
|
elif len(SETTINGS.USER_TO_WATCH) > 0: |
|
search_from_usernames(cl) |
|
else: |
|
search_from_following(cl) |
|
|
|
print('Bot pause : {0}.'.format(SETTINGS.BOT_SLEEP)) |
|
time.sleep(SETTINGS.BOT_SLEEP) |
|
|
|
except Exception as ex: |
|
print('Error of connect.') |
|
print(ex.args) |