Search code examples
pythonfor-loopinstagram-api

Private InstagramApi


I am trying to get a certain user's followers and filter them based on certain criteria, because I would like to narrow down the list of users worth interacting with. If I do not include time.sleep(), I get a KeyError. This way I am getting 429, 500, etc... which I know means too many requests, but isn't there a way to circumvent this or am I doing it wrong? Is there a more effective/pythonic way to do this? Thanks in advance.

import imageio
imageio.plugins.ffmpeg.download()
from InstagramAPI import InstagramAPI
import re
import time

def get_user_followers(api, user_id):
    followers = []
    next_max_id = True
    while next_max_id:
        # first iteration hack
        if next_max_id is True:
            next_max_id = ''
        _ = api.getUserFollowers(user_id, maxid=next_max_id)
        followers.extend(api.LastJson.get('users', []))
        next_max_id = api.LastJson.get('next_max_id', '')
    return followers

#returns user id of the user you want to get followers from
def get_user_id(self, user):
    self.SendRequest('users/' + str(user) + '/usernameinfo/')
    userid = self.LastJson['user']['pk']
    return userid


follower_list = []
def scrape_followers(self, user):
    # gets the id from a user
    self.SendRequest('users/' + str(user) + '/usernameinfo/')
    userid = self.LastJson['user']['pk']
    self.SendRequest('users/' + str(userid) + '/info/')

    # filter users based on criteria
    pp = self.LastJson['user']['has_anonymous_profile_picture']             #has profile pic
    fer = self.LastJson['user']['follower_count']                           #number of followers
    fing = self.LastJson['user']['following_count']                         #number of followings
    bio = self.LastJson['user']['biography']                                #certain words in bio
    b = re.compile(r'free|shop|download', re.IGNORECASE).search(bio)
    business = self.LastJson['user']['is_business']                         #isn't a business profile
    story = self.LastJson['user']['has_highlight_reels']                    #is active/has a story 
    if not pp and 1500 > fer > 50 and 1000 > fing > 100 and not business and story and b == None:
        follower_list.append(userid)
    # return follower_list


# ACTUAL CALL
# Your login details
api = InstagramAPI("xxx", "xxx")
api.login()
#Call the function
user = 'GlitteryHell'
userid = get_user_id(api, user)
followers = get_user_followers(api, userid)
for i in range(10):
    user = list(followers[i].values())[1]
    try:
        scrape_followers(api, user)
        time.sleep(1)
    except KeyError:
        time.sleep(10)
print(follower_list)

Solution

  • Recently Instagram deprecated few APIs and change response of few APIs so now you cannot get user's profile picture and other info as part of API response.

    Follows and followed by related API also deprecated.

    You can read more about it here.

    https://www.instagram.com/developer/changelog/

    Also, there is a massive change in the number of requests. It's reduced from 4000 to 200 per user per hour.

    You can add more auth token to increase the number of requests you can send per hour.