Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding following_v1_chunk method same as for followers #1135

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 46 additions & 24 deletions instagrapi/mixins/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def search_following(self, user_id: str, query: str) -> List[UserShort]:

def user_following_gql(self, user_id: str, amount: int = 0) -> List[UserShort]:
"""
Get user's following information by Public Graphql API
Get user's following users information by Public Graphql API

Parameters
----------
Expand Down Expand Up @@ -513,46 +513,68 @@ def user_following_gql(self, user_id: str, amount: int = 0) -> List[UserShort]:
users = users[:amount]
return users

def user_following_v1(self, user_id: str, amount: int = 0) -> List[UserShort]:
def user_following_v1_chunk(
self, user_id: str, max_amount: int = 0, max_id: str = ""
) -> Tuple[List[UserShort], str]:
"""
Get user's following users information by Private Mobile API
Get user's following users information by Private Mobile API and max_id (cursor)

Parameters
----------
user_id: str
User id of an instagram account
amount: int, optional
Maximum number of media to return, default is 0
max_amount: int, optional
Maximum number of media to return, default is 0 - Inf
max_id: str, optional
Max ID, default value is empty String

Returns
-------
List[UserShort]
List of objects of User type
Tuple[List[UserShort], str]
Tuple of List of users and max_id
"""
user_id = str(user_id)
max_id = ""
unique_set = set()
users = []
while True:
if amount and len(users) >= amount:
break
params = {
"rank_token": self.rank_token,
"search_surface": "follow_list_page",
"includes_hashtags": "true",
"enable_groups": "true",
"query": "",
"count": 10000,
}
if max_id:
params["max_id"] = max_id
result = self.private_request(
f"friendships/{user_id}/following/", params=params
f"friendships/{user_id}/following/",
params={
"max_id": max_id,
"count": 10000,
"rank_token": self.rank_token,
"search_surface": "follow_list_page",
"query": "",
"enable_groups": "true",
},
)
for user in result["users"]:
users.append(extract_user_short(user))
user = extract_user_short(user)
if user.pk in unique_set:
continue
unique_set.add(user.pk)
users.append(user)
max_id = result.get("next_max_id")
if not max_id:
if not max_id or (max_amount and len(users) >= max_amount):
break
return users, max_id

def user_following_v1(self, user_id: str, amount: int = 0) -> List[UserShort]:
"""
Get user's following users formation by Private Mobile API

Parameters
----------
user_id: str
User id of an instagram account
amount: int, optional
Maximum number of media to return, default is 0 - Inf

Returns
-------
List[UserShort]
List of objects of User type
"""
users, _ = self.user_following_v1_chunk(str(user_id), amount)
if amount:
users = users[:amount]
return users
Expand Down