Skip to content

Instantly share code, notes, and snippets.

@YannickSF
Created December 8, 2022 15:59
Show Gist options
  • Save YannickSF/ad51972c68aeffae54cf7dc01d09d9d9 to your computer and use it in GitHub Desktop.
Save YannickSF/ad51972c68aeffae54cf7dc01d09d9d9 to your computer and use it in GitHub Desktop.
instagram stories watcher trough python's bot

Quick Start :

Install dependances :

pip3 install -r requirements.txt

before launching script :

open script in editor there multiples variables in the top of the script.

  • ARGS, determine if the script launch by input parameters or with the assign one in the script.
  • ACCOUNT_USERNAME, "login" make sure to put the login between double quotes.
  • ACCOUNT_PASSWORD, "password" make sure to put the login between double quotes.
  • USER_TO_WATCH, "username"username to target.
  • USER_TO_WATCH, "username,username1,..." list of username to target.
  • SUCCESS_STEP, determine the number of success before bot's pause.
  • STEP_SLEEP, sleep time before each step of user's viewing stories (avoid instagram's locked).
  • BOT_SLEEP, sleep time of success and bot's pause before restarting operation.

Start the script :

python3 stories_watcher.py <"login"> <"password"> <success_step> <step_sleep> <bot_sleep>

to stop program : ctrl + x

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment