Skip to content

Commit db84d98

Browse files
authored
Merge pull request #1135 from msk-psp/master
Adding following_v1_chunk method same as for followers
2 parents 682bf7e + 0d82741 commit db84d98

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

instagrapi/mixins/user.py

+46-24
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def search_following(self, user_id: str, query: str) -> List[UserShort]:
467467

468468
def user_following_gql(self, user_id: str, amount: int = 0) -> List[UserShort]:
469469
"""
470-
Get user's following information by Public Graphql API
470+
Get user's following users information by Public Graphql API
471471
472472
Parameters
473473
----------
@@ -513,46 +513,68 @@ def user_following_gql(self, user_id: str, amount: int = 0) -> List[UserShort]:
513513
users = users[:amount]
514514
return users
515515

516-
def user_following_v1(self, user_id: str, amount: int = 0) -> List[UserShort]:
516+
def user_following_v1_chunk(
517+
self, user_id: str, max_amount: int = 0, max_id: str = ""
518+
) -> Tuple[List[UserShort], str]:
517519
"""
518-
Get user's following users information by Private Mobile API
520+
Get user's following users information by Private Mobile API and max_id (cursor)
519521
520522
Parameters
521523
----------
522524
user_id: str
523525
User id of an instagram account
524-
amount: int, optional
525-
Maximum number of media to return, default is 0
526+
max_amount: int, optional
527+
Maximum number of media to return, default is 0 - Inf
528+
max_id: str, optional
529+
Max ID, default value is empty String
526530
527531
Returns
528532
-------
529-
List[UserShort]
530-
List of objects of User type
533+
Tuple[List[UserShort], str]
534+
Tuple of List of users and max_id
531535
"""
532-
user_id = str(user_id)
533-
max_id = ""
536+
unique_set = set()
534537
users = []
535538
while True:
536-
if amount and len(users) >= amount:
537-
break
538-
params = {
539-
"rank_token": self.rank_token,
540-
"search_surface": "follow_list_page",
541-
"includes_hashtags": "true",
542-
"enable_groups": "true",
543-
"query": "",
544-
"count": 10000,
545-
}
546-
if max_id:
547-
params["max_id"] = max_id
548539
result = self.private_request(
549-
f"friendships/{user_id}/following/", params=params
540+
f"friendships/{user_id}/following/",
541+
params={
542+
"max_id": max_id,
543+
"count": 10000,
544+
"rank_token": self.rank_token,
545+
"search_surface": "follow_list_page",
546+
"query": "",
547+
"enable_groups": "true",
548+
},
550549
)
551550
for user in result["users"]:
552-
users.append(extract_user_short(user))
551+
user = extract_user_short(user)
552+
if user.pk in unique_set:
553+
continue
554+
unique_set.add(user.pk)
555+
users.append(user)
553556
max_id = result.get("next_max_id")
554-
if not max_id:
557+
if not max_id or (max_amount and len(users) >= max_amount):
555558
break
559+
return users, max_id
560+
561+
def user_following_v1(self, user_id: str, amount: int = 0) -> List[UserShort]:
562+
"""
563+
Get user's following users formation by Private Mobile API
564+
565+
Parameters
566+
----------
567+
user_id: str
568+
User id of an instagram account
569+
amount: int, optional
570+
Maximum number of media to return, default is 0 - Inf
571+
572+
Returns
573+
-------
574+
List[UserShort]
575+
List of objects of User type
576+
"""
577+
users, _ = self.user_following_v1_chunk(str(user_id), amount)
556578
if amount:
557579
users = users[:amount]
558580
return users

0 commit comments

Comments
 (0)