Skip to content

Commit

Permalink
Merge pull request #2 from congkv/feature/add_function_to_scrap_all_f…
Browse files Browse the repository at this point in the history
…ollowers

Add getPaginateAllFollowers() function
  • Loading branch information
congkv authored Aug 11, 2020
2 parents 03139e0 + edc1754 commit 538fe19
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,51 @@ public function getPaginateFollowers($accountId, $count = 20, $pageSize = 20, $d
return $toReturn;
}

/**
* @param $accountId
* @param int $pageSize
* @param string $nextPage
*
* @return array
* @throws InstagramException
* @throws InstagramNotFoundException
*/
public function getPaginateAllFollowers($accountId, $pageSize = 20, $nextPage = '')
{
$response = Request::get(Endpoints::getFollowersJsonLink($accountId, $pageSize, $nextPage),
$this->generateHeaders($this->userSession));
if ($response->code === static::HTTP_NOT_FOUND) {
throw new InstagramNotFoundException('Account with this id doesn\'t exist');
}
if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}

$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);

if ($jsonResponse['data']['user']['edge_followed_by']['count'] === 0) {
return [];
}

$edgesArray = $jsonResponse['data']['user']['edge_followed_by']['edges'];
if (count($edgesArray) === 0) {
throw new InstagramException('Failed to get followers of account id ' . $accountId . '. The account is private.', static::HTTP_FORBIDDEN);
}

$accounts = [];
foreach ($edgesArray as $edge) {
$accounts[] = $edge['node'];
}

$pageInfo = $jsonResponse['data']['user']['edge_followed_by']['page_info'];

return [
'hasNextPage' => $pageInfo['has_next_page'],
'nextPage' => $pageInfo['end_cursor'],
'accounts' => $accounts
];
}

/**
* @param string $accountId Account id of the profile to query
* @param int $count Total followed accounts to retrieve
Expand Down

0 comments on commit 538fe19

Please sign in to comment.