Skip to content

Commit

Permalink
Can get list of accounts a user is following
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachmatullah, Agro committed Nov 25, 2017
1 parent cd89cbc commit 5be4c30
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Endpoints
const COMMENTS_BEFORE_COMMENT_ID_BY_CODE = 'https://www.instagram.com/graphql/query/?query_id=17852405266163336&shortcode={{shortcode}}&first={{count}}&after={{commentId}}';
const LAST_LIKES_BY_CODE = 'ig_shortcode({{code}}){likes{nodes{id,user{id,profile_pic_url,username,follows{count},followed_by{count},biography,full_name,media{count},is_private,external_url,is_verified}},page_info}}';
const LIKES_BY_SHORTCODE = 'https://www.instagram.com/graphql/query/?query_id=17864450716183058&variables={"shortcode":"{{shortcode}}","first":{{count}},"after":"{{likeId}}"}';
const FOLLOWING_URL = 'https://www.instagram.com/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}';
const FOLLOWING_URL = 'https://www.instagram.com/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}&after={{after}}';
const FOLLOWERS_URL = 'https://www.instagram.com/graphql/query/?query_id=17851374694183129&id={{accountId}}&first={{count}}&after={{after}}';
const FOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/follow/';
const UNFOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/unfollow/';
Expand Down Expand Up @@ -133,4 +133,18 @@ public static function getFollowersJsonLink($accountId, $count, $after = '')

return $url;
}

public static function getFollowingJsonLink($accountId, $count, $after = '')
{
$url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOWING_URL);
$url = str_replace('{{count}}', urlencode($count), $url);

if ($after === '') {
$url = str_replace('&after={{after}}', '', $url);
} else {
$url = str_replace('{{after}}', urlencode($after), $url);
}

return $url;
}
}
65 changes: 65 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,71 @@ public function getFollowers($accountId, $count = 20, $pageSize = 20, $delayed =
return $accounts;
}

/**
* @param string $accountId Account id of the profile to query
* @param int $count Total followed accounts to retrieve
* @param int $pageSize Internal page size for pagination
* @param bool $delayed Use random delay between requests to mimic browser behaviour
*
* @return array
* @throws InstagramException
*/
public function getFollowing($accountId, $count = 20, $pageSize = 20, $delayed = true)
{
if ($delayed) {
set_time_limit(1800); // 30 mins
}

$index = 0;
$accounts = [];
$endCursor = '';

if ($count < $pageSize) {
throw new InstagramException('Count must be greater than or equal to page size.');
}

while (true) {
$response = Request::get(Endpoints::getFollowersJsonLink($accountId, $pageSize, $endCursor),
$this->generateHeaders($this->userSession));
if ($response->code !== 200) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);

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

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

foreach ($edgesArray as $edge) {
$accounts[] = $edge['node'];
$index++;
if ($index >= $count) {
break 2;
}
}

$pageInfo = $jsonResponse['data']['user']['edge_follow']['page_info'];
if ($pageInfo['has_next_page']) {
$endCursor = $pageInfo['end_cursor'];
} else {
break;
}

if ($delayed) {
// Random wait between 1 and 3 sec to mimic browser
$microsec = rand(1000000, 3000000);
usleep($microsec);
}
}
return $accounts;
}

/**
* @param bool $force
*
Expand Down

0 comments on commit 5be4c30

Please sign in to comment.