subzeroid / instagrapi

🔥 The fastest and powerful Python library for Instagram Private API 2024
https://hikerapi.com/p/bkXQlaVe
MIT License
4.21k stars 667 forks source link

I get an issue when requesting to get followers of a public account. #1253

Open isakurbanov744 opened 1 year ago

isakurbanov744 commented 1 year ago

I have provided the code below, I get the error for the get_followers function


import os
from time import sleep
from typing import Dict, List
from user import User

from instagrapi import Client
from instagrapi.types import UserShort

IG_USERNAME = ""
IG_PASSWORD = ""
IG_CREDENTIAL_PATH = "data/settings.json"
SLEEP_TIME = "600"  # in seconds

class Bot:
    _cl = None

    def __init__(self):
        self._cl = Client()
        if os.path.exists(IG_CREDENTIAL_PATH):
            self._cl.load_settings(IG_CREDENTIAL_PATH)
            self._cl.login(IG_USERNAME, IG_PASSWORD, relogin=True)
            self._cl.relogin()
        else:
            self._cl.login(IG_USERNAME, IG_PASSWORD, relogin=True)
            self._cl.dump_settings(IG_CREDENTIAL_PATH)

    def follow_by_username(self, username) -> bool:
        """
        Follow a user

        Parameters
        ----------
        username: str
            Username for an Instagram account

        Returns
        -------
        bool
            A boolean value
        """
        userid = self._cl.user_id_from_username(username)
        return self._cl.user_follow(userid)

    def unfollow_by_username(self, username) -> bool:
        """
        Unfollow a user

        Parameters
        ----------
        username: str
            Username for an Instagram account

        Returns
        -------
        bool
            A boolean value
        """
        userid = self._cl.user_id_from_username(username)
        return self._cl.user_unfollow(userid)

    def get_followers(self, amount: int = 0) -> Dict[int, UserShort]:
        """
        Get bot's followers

        Parameters
        ----------
        amount: int, optional
            Maximum number of media to return, default is 0 - Inf

        Returns
        -------
        Dict[int, UserShort]
            Dict of user_id and User object
        """
        follower_list = self._cl.user_followers("372686256", amount=300, use_cache=False)

        followers = []

        for key, value in follower_list.items():
            user = User(value.pk, value.username)
            followers.append(user)

        print(len(followers))

    def get_followers_usernames(self, amount: int = 0) -> List[str]:
        """
        Get bot's followers usernames

        Parameters
        ----------
        amount: int, optional
            Maximum number of media to return, default is 0 - Inf

        Returns
        -------
        List[str]
            List of usernames
        """
        followers = self._cl.user_followers(self._cl.user_id, amount=amount)
        return [user.username for user in followers.values()]

    def get_following(self, amount: int = 0) -> Dict[int, UserShort]:
        """
        Get bot's followed users

        Parameters
        ----------
        amount: int, optional
            Maximum number of media to return, default is 0 - Inf

        Returns
        -------
        Dict[int, UserShort]
            Dict of user_id and User object
        """
        return self._cl.user_following(self._cl.user_id, amount=amount)

    def get_following_usernames(self, amount: int = 0) -> List[str]:
        """
        Get bot's followed usernames

        Parameters
        ----------
        amount: int, optional
            Maximum number of media to return, default is 0 - Inf

        Returns
        -------
        List[str]
            List of usernames
        """
        following = self._cl.user_following("372686256", amount=amount)
        return [user.username for user in following.values()]

    def update(self):
        #print(self.get_followers(15000))
        print(self.get_user_id("isakurbanov1"))

    def get_user_id(self, username):
        try:
            user_id = self._cl.user_id_from_username(str(username))
        except:
            print("User not found")
            print("Exiting now...")

        return user_id

if __name__ == "__main__":
    bot = Bot()

    bot.follow_by_username("leomessi")

    while True:
        """
        Infnit loop
        """
        bot.update()
        sleep(int(SLEEP_TIME))

The error message states:

instagrapi.exceptions.LoginRequired: login_required

adw0rd commented 1 year ago

Looking at the code, you are not calling get_followers

Provide login, password, session, proxy, etc. to be able to reproduce your case

isakurbanov744 commented 1 year ago

I am providing login, password and session. I just didn't include them here. But I still get this error.

Guiona commented 1 year ago

Hi,

I've same issue, after cl.login I get correct user_id, I can get medias for user_id but when I want get user_following from same user_id error 400

Simple code

from instagrapi import Client
import json

cl = Client()
cl.login('USERNAME', 'PASSWORD')
user_id = cl.user_id_from_username("mypersonnalaccount") # Same Use for login
print("******** %s ********" % user_id)
following = cl.user_following(user_id, amount=20)

Any ideas? Solutions?

Guiona commented 1 year ago

Update: No error with cl.user_followers(user_id, amount=20)

claell commented 1 year ago

1292 seems to be the same

ubuntu2326 commented 1 year ago

You may try the same using Ensta.

Installation

pip install ensta

This is how to fetch follower list:

from ensta import Host, NewSessionID

sessionid = NewSessionID("username", "password")

host = Host(sessionid)
followers = host.followers("melon_owe")

for user in followers:
    print(user.username)  # access any data

Check out ensta's github repo for more...