@@ -467,7 +467,7 @@ def search_following(self, user_id: str, query: str) -> List[UserShort]:
467
467
468
468
def user_following_gql (self , user_id : str , amount : int = 0 ) -> List [UserShort ]:
469
469
"""
470
- Get user's following information by Public Graphql API
470
+ Get user's following users information by Public Graphql API
471
471
472
472
Parameters
473
473
----------
@@ -513,46 +513,68 @@ def user_following_gql(self, user_id: str, amount: int = 0) -> List[UserShort]:
513
513
users = users [:amount ]
514
514
return users
515
515
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 ]:
517
519
"""
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)
519
521
520
522
Parameters
521
523
----------
522
524
user_id: str
523
525
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
526
530
527
531
Returns
528
532
-------
529
- List[UserShort]
530
- List of objects of User type
533
+ Tuple[ List[UserShort], str ]
534
+ Tuple of List of users and max_id
531
535
"""
532
- user_id = str (user_id )
533
- max_id = ""
536
+ unique_set = set ()
534
537
users = []
535
538
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
548
539
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
+ },
550
549
)
551
550
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 )
553
556
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 ) :
555
558
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 )
556
578
if amount :
557
579
users = users [:amount ]
558
580
return users
0 commit comments