From edc17544bbb70779776485446c129b780036411c Mon Sep 17 00:00:00 2001 From: Khuong Van Cong Date: Tue, 11 Aug 2020 17:32:03 +0700 Subject: [PATCH 1/3] add getPaginateAllFollowers() function --- src/InstagramScraper/Instagram.php | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index a49b7011..e2dbceab 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -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 From 1ca4a4cec878a4258921e118e6d4921d08b514fd Mon Sep 17 00:00:00 2001 From: Khuong Van Cong Date: Wed, 12 Aug 2020 09:50:36 +0700 Subject: [PATCH 2/3] add count followers --- src/InstagramScraper/Instagram.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index e2dbceab..38f19bea 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -1372,7 +1372,8 @@ public function getPaginateAllFollowers($accountId, $pageSize = 20, $nextPage = $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); - if ($jsonResponse['data']['user']['edge_followed_by']['count'] === 0) { + $count = $jsonResponse['data']['user']['edge_followed_by']['count']; + if ($count === 0) { return []; } @@ -1389,6 +1390,7 @@ public function getPaginateAllFollowers($accountId, $pageSize = 20, $nextPage = $pageInfo = $jsonResponse['data']['user']['edge_followed_by']['page_info']; return [ + 'count' => $count, 'hasNextPage' => $pageInfo['has_next_page'], 'nextPage' => $pageInfo['end_cursor'], 'accounts' => $accounts From a71a115c1fa05c1ef889fc90a0783e08f241cdb9 Mon Sep 17 00:00:00 2001 From: Khuong Van Cong Date: Wed, 12 Aug 2020 10:46:40 +0700 Subject: [PATCH 3/3] add function to scrap all following --- src/InstagramScraper/Instagram.php | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 38f19bea..57ae19d2 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -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