Skip to content

Commit

Permalink
Expose max_id in user_followers API [#136] implement user_followers_v…
Browse files Browse the repository at this point in the history
…1_chunk which return users and max_id
  • Loading branch information
adw0rd committed May 16, 2021
1 parent 79ffb62 commit 0d86041
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions instagrapi/mixins/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from copy import deepcopy
from typing import Dict, List
from typing import Dict, List, Tuple

from instagrapi import config
from instagrapi.exceptions import (
Expand Down Expand Up @@ -395,37 +395,57 @@ def user_following(
following = dict(list(following.items())[:amount])
return following

def user_followers_v1(self, user_id: int, amount: int = 0) -> List[UserShort]:
def user_followers_v1_chunk(self, user_id: int, max_amount: int = 0, max_id: str = "") -> Tuple[List[UserShort], str]:
"""
Get user's followers information
Parameters
----------
user_id: int
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 = int(user_id)
max_id = ""
users = []
while True:
if amount and len(users) >= amount:
break
result = self.private_request(
f"friendships/{user_id}/followers/",
params={"max_id": max_id, "rank_token": self.rank_token},
)
result = self.private_request(f"friendships/{user_id}/followers/", params={
"max_id": max_id,
"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))
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_followers_v1(self, user_id: int, amount: int = 0) -> List[UserShort]:
"""
Get user's followers information
Parameters
----------
user_id: int
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, max_id = self.user_followers_v1_chunk(int(user_id), amount)
if amount:
users = users[:amount]
return users
Expand All @@ -443,7 +463,7 @@ def user_followers(
use_cache: bool, optional
Whether or not to use information from cache, default value is True
amount: int, optional
Maximum number of media to return, default is 0
Maximum number of media to return, default is 0 - Inf
Returns
-------
Expand Down

0 comments on commit 0d86041

Please sign in to comment.