Examples and improvement of InstaPy library
Take a look, develop and take advance of that tool
Explore the docs Β»
Report Bug
Β·
Request Feature
pip install instapy
Important: depending on your system, make sure to use pip3
and python3
instead.
That's it! π
If you're on Ubuntu, read the specific guide on Installing on Ubuntu (64-Bit). If you're on a Raspberry Pi, read the Installing on RaspberryPi guide instead.
If you would like to install a specific version of Instapy you may do so with:
pip install instapy==0.1.1
To run InstaPy, you'll need to run the quickstart script you've just downloaded.
You can put in your account details now by passing the username and password parameters to the InstaPy()
function in your quickstart script, like so:
InstaPy(username="abcd",
password="1234")
Or you can pass them using the Command Line Interface (CLI).
Once you have your quickstart script configured you can execute the script with the following commands.
python quickstart.py
-- or
python quickstart.py --username abcd --password 1234
InstaPy will now open a browser window and start working.
If want InstaPy to run in the background pass the
--headless-browser
option when running from the CLI
Or add theheadless_browser=True
parameter to theInstaPy(headless_browser=True)
constructor.
pip install instapy -U
In this paragraph are explained tha most usefull script that I created and used in Instagram automation. Anyways, I really encourage to see that starting examples provided by InstaPy. Those three are the one that I use every day for my account.
If you start from scratch, the best script to execute is the Starting example. It has very few interactions so you should not be worried about the bans.
Everything starts with InstaPy(
new object. With that sentence a session is created and logged into your account. This session will lasts until the script is finished.
session = InstaPy(username=insta_username,
password=insta_password,
headless_browser=True)
This session managment is really relevant since the login is the most used tool to analyse bot used by Instagram's team. To handle that, we should use this smart_run
that will remember your session in your server for the next executions. This means, for example if you creates a connection at 10pm and 12pm, the second one used the first session, so you don't make 2 logins.
with smart_run(session):
Before starting with the actions is recommended to set a quota limits, like that. In those limit you can customize almost anything to have limited your interactions.
session.set_user_interact(amount=3, randomize=True, percentage=80,media='Photo')
Ok, once you have a session and limits set, it's time to starts doing things. It's such a simple like that:
session.set_do_like(enabled=True, percentage=90)
In the library actions documentation you will find all the actions provided. They really have all kind of actions coded, so feel free to use all of them. For example, you can like by tag to cover a certain area of photos.
session.like_by_tags(random.sample(like_tag_list, 3),
amount=random.randint(10, 20),interact=True)
For my personal use I created a class to manage and makes simple all the connections and scripts. This class will creates the sessiΓ³n, logs and manage all the execeptions. The main constructor is the next one. As can be seen, a lot of limit are customized with muy preferences. Try to find the ones that suits with you.
class InstaBot:
def __init__(self, usr, pss, name):
filename = name+'.log'
logging.basicConfig(filename=filename)
self.usr = usr
self.name = name
try:
session = InstaPy(username=usr, password=pss)
session.login()
self.bot = session
self.bot.set_quota_supervisor(enabled=True, sleep_after=["likes", "comments_d", "follows", "unfollows", "server_calls_h"], sleepyhead=True, stochastic_flow=True, notify_me=True,
peak_likes_hourly=80,
peak_likes_daily=None,
peak_comments_hourly=30,
peak_comments_daily=None,
peak_follows_hourly=80,
peak_follows_daily=None,
peak_unfollows_hourly=80,
peak_unfollows_daily=None,
peak_server_calls_hourly=None,
peak_server_calls_daily=4700)
Loggin will be output to a file. This is file is the best way to check how your bot is working. Do not forget to check it at least once a week.
logging.error('Login completed '+self.getTime()+' by '+self.name)
except:
logging.critical('Login failed '+self.getTime()+' by '+self.name)
An action can be for example follow
that apart from following other account, creates some limits before.
def follow(self, list, number_of_users):
try:
self.bot.set_skip_users(skip_no_profile_pic=True, skip_private=False)
#self.bot.set_action_delays(enabled=True, like=5.2, randomize=True, random_range_from=70, random_range_to=140)
self.bot.set_relationship_bounds(min_posts=10)
#self.bot.set_user_interact(amount=2, percentage=100, randomize=True, media='Photo')
self.bot.follow_user_followers(list, amount=number_of_users, randomize=True)
logging.error('Follow completed '+self.getTime()+' by '+self.name)
except:
logging.critical('Follow failed '+self.getTime()+' by '+self.name)
Once you have created the InstaBot
object you can simply one of those actions:
def follow(self, list, number_of_users):
def follow_by_tags(self, list, number_of_users):
def follow_liker(self, list, number_of_users):
def comment(self, list, interactions):
def unfollow(self, number_of_users):
def like(self, list, interactions):
And as a result, the script seems as simple as that:
from main import InstaBot
print( __file__)
bot = InstaBot('username', 'PASSXX', 'normal_follow')
#Should execute every day at 10pm
#No more than 4 in (X*10*2 interactions)
bot.follow(['_chris_kap_', 'trasmispasos', 'algunandrea'], 10)
bot.end()
As you may expect this library need to be scheduled. The first option to do it is through crons
. You can find how to creates a cron execution in the docs. But apart from the old and classic ways of doing things there is one new way to do things: Schedule
import schedule
With that library you can easily define a function like that.
def cron_follow():
session = InstaPy(username='xxxx', password='passs',headless_browser=True)
session.login()
session.set_quota_supervisor(enabled=True, sleep_after=["likes", "comments_d", "follows", "unfollows", "server_calls_h"], sleepyhead=True, stochastic_flow=True, notify_me=True,
peak_likes_hourly=80,
peak_likes_daily=None,
peak_comments_hourly=30,
peak_comments_daily=None,
peak_follows_hourly=80,
peak_follows_daily=None,
peak_unfollows_hourly=80,
peak_unfollows_daily=None,
peak_server_calls_hourly=None,
peak_server_calls_daily=4700)
session.follow_user_followers(['relatosencontrados8', 'trasmispasos', 'algunandrea'], amount=10, randomize=True)
session.end()
And with a sentence schedule the execution of that function. The last code ensures the unlimited execution of the code. With that way we try to minize the logins attemps.
schedule.every().day.at("02:35").do(cron_unfollow)
schedule.every().day.at("10:35").do(cron_follow)
schedule.every().day.at("15:35").do(cron_like)
schedule.every().day.at("20:22").do(cron_follow)
while True:
schedule.run_pending()
time.sleep(10)
Here are some tips that I adquired using this library, that maybe are helpfull for you (π):
Instagram
purpose is to have real user, so they will to punish any suspicious interaction. They already know that the easiest way to automate Instagram is through web, so they put an effort to track all actions through this platform.- As
Instagram
and us know that we use web platform to automate bots, they are constantly changing the web. From the other part, they are also updating the library. But you should track your logs and bots almost every week to check if everything is working fine. Instagram's
limit is over 100 integration / hour but the have accumulatives counters also. Take care of reaching that limit in less than 2 hours.- Developing this library creates a lot of integrations. Do not try to develop with this library during a long time. Distrubuting the development is better to minimize the access.
Instagram
uses the IPs to check from where you are connecting. Try to execute the bot from a server of same region. Also schedule your execution times in times that the account is used. For example, if put a cron on a server of other region during the night,Instagram
will know fast.- As my point of view, is better to put strong restriction rather than working with hug numbers. Try to get quality user rather than a lot users.
There are some things missing (improve documentation...), feel free to add this if you want! If you want some guidelines feel free to contact us :)
1: This method is the slower and only must be used if you don't have connectivity with GitHub