Skip to content

Commit

Permalink
Fixes #120. 404 error when trying to retrieve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
raiym committed Jun 14, 2017
1 parent 72cd030 commit edd2940
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
13 changes: 3 additions & 10 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Endpoints
const MEDIA_JSON_BY_TAG = 'https://www.instagram.com/explore/tags/{tag}/?__a=1&max_id={max_id}';
const GENERAL_SEARCH = 'https://www.instagram.com/web/search/topsearch/?query={query}';
const ACCOUNT_JSON_INFO_BY_ID = 'ig_user({userId}){id,username,external_url,full_name,profile_pic_url,biography,followed_by{count},follows{count},media{count},is_private,is_verified}';
const LAST_COMMENTS_BY_CODE = 'ig_shortcode({{code}}){comments.last({{count}}){count,nodes{id,created_at,text,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 COMMENTS_BEFORE_COMMENT_ID_BY_CODE = 'ig_shortcode({{code}}){comments.before({{commentId}},{{count}}){count,nodes{id,created_at,text,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 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 INSTAGRAM_QUERY_URL = 'https://www.instagram.com/query/';
const INSTAGRAM_GRAPHQL_QUERY_URL = 'https://www.instagram.com/graphql/query/';
const INSTAGRAM_CDN_URL = 'https://scontent.cdninstagram.com/';

public static function getAccountPageLink($username)
Expand Down Expand Up @@ -70,16 +70,9 @@ public static function getGeneralSearchJsonLink($query)
return str_replace('{query}', urlencode($query), Endpoints::GENERAL_SEARCH);
}

public static function getLastCommentsByCodeLink($code, $count)
{
$url = str_replace('{{code}}', urlencode($code), Endpoints::LAST_COMMENTS_BY_CODE);
return str_replace('{{count}}', urlencode($count), $url);

}

public static function getCommentsBeforeCommentIdByCode($code, $count, $commentId)
{
$url = str_replace('{{code}}', urlencode($code), Endpoints::COMMENTS_BEFORE_COMMENT_ID_BY_CODE);
$url = str_replace('{{shortcode}}', urlencode($code), Endpoints::COMMENTS_BEFORE_COMMENT_ID_BY_CODE);
$url = str_replace('{{count}}', urlencode($count), $url);
return str_replace('{{commentId}}', urlencode($commentId), $url);
}
Expand Down
19 changes: 9 additions & 10 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function getPaginateMedias($username, $maxId = '')
private function generateHeaders($session)
{
$headers = [];
if($session) {
if ($session) {
$cookies = '';
foreach ($session as $key => $value) {
$cookies .= "$key=$value; ";
Expand Down Expand Up @@ -256,31 +256,30 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null)
$remain = 0;
}
if (!isset($maxId)) {
$parameters = Endpoints::getLastCommentsByCodeLink($code, $numberOfCommentsToRetreive);
$maxId = '';

} else {
$parameters = Endpoints::getCommentsBeforeCommentIdByCode($code, $numberOfCommentsToRetreive, $maxId);
}
$response = Request::post(Endpoints::INSTAGRAM_QUERY_URL, $this->generateHeaders($this->userSession), ['q' => $parameters]);
$commentsUrl = Endpoints::getCommentsBeforeCommentIdByCode($code, $numberOfCommentsToRetreive, $maxId);
$response = Request::get($commentsUrl, $this->generateHeaders($this->userSession));
if ($response->code !== 200) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . $response->body . ' Something went wrong. Please report issue.');
}
$cookies = self::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$jsonResponse = json_decode($response->raw_body, true);
$nodes = $jsonResponse['comments']['nodes'];
$nodes = $jsonResponse['data']['shortcode_media']['edge_media_to_comment']['edges'];
foreach ($nodes as $commentArray) {
$comments[] = Comment::fromApi($commentArray);
$comments[] = Comment::fromApi($commentArray['node']);
}
$hasPrevious = $jsonResponse['comments']['page_info']['has_previous_page'];
$numberOfComments = $jsonResponse['comments']['count'];
$hasPrevious = $jsonResponse['data']['shortcode_media']['edge_media_to_comment']['page_info']['has_next_page'];
$numberOfComments = $jsonResponse['data']['shortcode_media']['edge_media_to_comment']['count'];
if ($count > $numberOfComments) {
$count = $numberOfComments;
}
if (sizeof($nodes) == 0) {
return $comments;
}
$maxId = $nodes[sizeof($nodes) - 1]['id'];
$maxId = $nodes[sizeof($nodes) - 1]['node']['id'];
}
return $comments;
}
Expand Down
9 changes: 9 additions & 0 deletions src/InstagramScraper/Model/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ function __construct()
{
}

public static function fromComment($userArray)
{
$instance = new self();
$instance->id = $userArray['id'];
$instance->profilePicUrl = $userArray['profile_pic_url'];
$instance->username = $userArray['username'];
return $instance;
}

public static function fromAccountPage($userArray)
{
$instance = new self();
Expand Down
2 changes: 1 addition & 1 deletion src/InstagramScraper/Model/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function fromApi($commentArray)
$instance->id = $commentArray['id'];
$instance->createdAt = $commentArray['created_at'];
$instance->text = $commentArray['text'];
$instance->owner = Account::fromAccountPage($commentArray['user']);
$instance->owner = Account::fromComment($commentArray['owner']);
return $instance;
}

Expand Down
1 change: 1 addition & 0 deletions tests/InstagramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,6 @@ public function testGeMediaCommentsByCode()
$comments = self::$instagram->getMediaCommentsByCode('BR5Njq1gKmB', 40);
//TODO: check why returns less comments
$this->assertEquals(40 - 4, sizeof($comments));
var_dump($comments);
}
}

0 comments on commit edd2940

Please sign in to comment.