From edd29406d609008e0b89be86d66930cfd48a4164 Mon Sep 17 00:00:00 2001 From: raiym Date: Thu, 15 Jun 2017 01:43:35 +0600 Subject: [PATCH] Fixes #120. 404 error when trying to retrieve comments --- src/InstagramScraper/Endpoints.php | 13 +++---------- src/InstagramScraper/Instagram.php | 19 +++++++++---------- src/InstagramScraper/Model/Account.php | 9 +++++++++ src/InstagramScraper/Model/Comment.php | 2 +- tests/InstagramTest.php | 1 + 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index 4d126686..44271df6 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -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) @@ -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); } diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 5e0cf438..602371bc 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -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; "; @@ -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; } diff --git a/src/InstagramScraper/Model/Account.php b/src/InstagramScraper/Model/Account.php index 6f9b6a18..b56c8464 100644 --- a/src/InstagramScraper/Model/Account.php +++ b/src/InstagramScraper/Model/Account.php @@ -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(); diff --git a/src/InstagramScraper/Model/Comment.php b/src/InstagramScraper/Model/Comment.php index f361e5f0..c13d3f9c 100644 --- a/src/InstagramScraper/Model/Comment.php +++ b/src/InstagramScraper/Model/Comment.php @@ -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; } diff --git a/tests/InstagramTest.php b/tests/InstagramTest.php index 56218195..39ee2a97 100644 --- a/tests/InstagramTest.php +++ b/tests/InstagramTest.php @@ -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); } } \ No newline at end of file