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

Add function to scrap all following #4

Merged
merged 1 commit into from
Aug 12, 2020
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
47 changes: 47 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,53 @@ public function getPaginateFollowing($accountId, $count = 20, $pageSize = 20, $d
return $toReturn;
}

/**
* @param $accountId
* @param int $pageSize
* @param string $nextPage
*
* @return array
* @throws InstagramException
* @throws InstagramNotFoundException
*/
public function getPaginateAllFollowing($accountId, $pageSize = 20, $nextPage = '')
{
$response = Request::get(Endpoints::getFollowingJsonLink($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);

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

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

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

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

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

/**
* @param array $reel_ids - array of instagram user ids
* @return array
Expand Down