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

Feature/20171125 get following #224

Merged
merged 2 commits into from
Nov 25, 2017
Merged
Show file tree
Hide file tree
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
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::getFollowingJsonLink($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