How To Grow Instagram Pages With Code

Today, we’re going to be developing scripts to automatically grow Instagram accounts, using Python and the Instagram API.

We’re going to be using meme pages as an example. In the meme page community, it’s very common practice for admins to follow users that follow other similar meme pages and then unfollow them later. The underlying premise of this strategy is outreach: people that otherwise would not have come across your page do end up looking at it, and if they like it, they’ll follow you back and stay following you for the content. If they don’t like the page, they just don’t follow back, no big deal. This is an alright strategy, but manually following thousands of people takes a pretty long time. So, we’re going to automate and optimize this process!

There’s the actual Instagram API, but its only accessible through mobile apps. All good, there’s also a open-source Python library on Github allows us to access the Instagram API by masking our scripts as a mobile app and using HTTP requests for the API calls.

So, after we download to library using git pull, we can start by testing that it works. Here, we’re just signing in and printing out our own data with this script:

from instabot import Bot

bot = Bot();

my_username = " ";
my_password = " "; #fill in with IG credentials

bot.login(username = my_username, password = my_password);

user_id = bot.get_user_id_from_username(my_username);

user_info = bot.get_user_info(user_id);

print(user_info);

Cool, so it’s working.

Now, we just have write a following method:

def follow(bot, followers, followCount, amt):
    lower = followCount;
    #upper = min(followCount + amt, len(followers));
    count = 0;
    actualCount = 0;
    #follow
    for user in followers[lower:]: 
        print("count", count);
        print("amt", amt);
        if count >= amt:
            return actualCount;
        username = bot.get_username_from_user_id(user);     
        #print("Attempting to follow:", username);
        if bot.follow(username):
            print("Followed! -", username);
            count += 1;
        else:
            print("Not Followed! -", username);
        actualCount += 1;
        
    return actualCount;

When we run this with a test script, we can see that it successfully goes through and follows users in an array:

Then, we just need to write an unfollowing method, which will successfully unfollow users in an array:

def unfollow(bot, amt):
    following = bot.get_user_following("");
    whitelist = bot.whitelist_file;
    following = list(set(following) - whitelist.set);
    i = 0;
    cap = min(100, len(following));
    for user in following[:-cap]: #get_user_following stored in reverse stack
        if bot.api.last_response.status_code == 400:
            sys.exit();
        username = bot.get_username_from_user_id(user);
        print("Attempting to unfollow:", username);
        if bot.unfollow(username):
            i += 1;
            print("Unfollowed -", username);
        else:
            print("Failed to unfollow -", username);
        if(i >= amt):
            break;
    return True;

With Instagram’s limits of following/unfollowing 160 users per hour, we’re going to run the follow method for 160 people in a 1 hour slot, and then run the unfollow method for 160 people in another 1 hour slot, and repeat this. Note that when we follow people, we are adding their ID’s to the end of our list of following, and when we unfollow, we want to unfollow from the start of our list of following, so that we don’t unfollow the people we just followed. This way, there’s ample time for them to check out our account and follow us back if they want to.

We’re also going to be feeding an array of a single account that’s like our meme page just to start. Eventually, it’ll be be able to crawl through and find other meme pages like ours in order to run autonomously forever but that’s in the next step:

amt = 160;
delay = 600;
for acc in accounts:
    followers = bot.get_user_followers(acc);
    skipped = bot.skipped_file;
    followed = bot.followed_file;
    unfollowed = bot.unfollowed_file;
    followers = list(set(followers) - skipped.set - followed.set - unfollowed.set);
    followCount = 0;
    printUpdate("Starting Running")
    while followCount < len(followers):
        startTime = printUpdate("Starting Following")
        followCount += follow(bot, followers, followCount, amt);
        endTime = printUpdate("Ending Following")
        secElapsed = (endTime - startTime).total_seconds();
        if secElapsed < 3600:
            sleep(3600 - secElapsed);
        startTime = printUpdate("Starting UnFollowing")
        unfollow(bot,amt);
        endTime = printUpdate("Ending UnFollowing")
        secElapsed = (endTime - startTime).total_seconds();
        if secElapsed < 3600:
            sleep(3600 - secElapsed);
                    
    accCount += 1;

Now, we’re going to let be sustainable for more than reaching out to users that follow one account by adding a filter. We’re going to look through who we’re following in the process and if they are another meme page like ours(because meme pages follow each other as well), we’re going to add it to our list of accounts that we’re running through in our loop. This will allow it to run for pretty much forever:

def filter(bot, amt):
    following = bot.get_user_following("");
    whitelist = bot.whitelist_file;
    following = list(set(following) - whitelist.set);
    added = 0;
    for user in following[-amt:]: #file read is stored in reversed stack
        if bot.api.last_response.status_code == 400:
            sys.exit();
        username = bot.get_username_from_user_id(user);
        user_info = bot.get_user_info(user)
        bio = user_info['biography'];
        username = user_info['username'];
        name = user_info['full_name'];
        followers = user_info['follower_count'];
        following = user_info['following_count'];
        if "meme" in bio.lower() or "meme" in name.lower() or "meme" in username.lower():
            if followers > 2500 and followers < 10000:
                if following/followers < 4:
                    accounts.append(username);
                    print("Added: ", username);
                    added += 1;
        if added >= 3:
            return;
        sleep(round(random.uniform(4,7), 2));
...
    while followCount < len(followers):
    ...
        if(len(accounts) - accCount < 2):
            startTime = printUpdate("Starting Filter");
            filter(bot, 400);
            endTime = printUpdate("Ending Filter");
            secElapsed = (endTime - startTime).total_seconds();
            if secElapsed < 3600:
                sleep(3600 - secElapsed);

Yeah, that’s pretty much it! We just automated Instagram growth! I ran this on my own page, and what I found was that for every 160 people the script followed, around 50 people followed me back. I started running the script when I had 2,000 followers, and I was able to gain over 5,000 real, active followers that enjoy my content through this. My entire goal with my page is just to make people laugh and be happy, so if I’m able to do that for more people, then I guess that’s pretty cool.

I should warn that Instagram does block accounts that do this if they detect the process, so I would be very cautious.

Try it yourself with the library, and don’t hesitate hit me up if you have any questions or concerns!

Source: Medium

Happy learning!

8 Likes