Skip to content

Commit

Permalink
Add &__d=dis to MEDIA_JSON_BY_TAG and update JSON response path in …
Browse files Browse the repository at this point in the history
…getMediasByTag(), getPaginateMediasByTag(), and getCurrentTopMediasByTagName() (fix #1052) (#1091)
  • Loading branch information
vzz3 authored Sep 7, 2022
1 parent 230f1eb commit fcc7207
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Endpoints
const ACCOUNT_ACTIVITY = 'https://www.instagram.com/accounts/activity/?__a=1';
const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1&__d=dis';
const MEDIA_JSON_BY_LOCATION_ID = 'https://www.instagram.com/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}';
const MEDIA_JSON_BY_TAG = 'https://www.instagram.com/explore/tags/{tag}/?__a=1&max_id={max_id}';
const MEDIA_JSON_BY_TAG = 'https://www.instagram.com/explore/tags/{tag}/?__a=1&max_id={max_id}&__d=dis';
const GENERAL_SEARCH = 'https://www.instagram.com/web/search/topsearch/?query={query}&count={count}';
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 COMMENTS_BEFORE_COMMENT_ID_BY_CODE = 'https://www.instagram.com/graphql/query/?query_hash=33ba35852cb50da46f5b5e889df7d159&variables={variables}';
Expand Down
64 changes: 45 additions & 19 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,8 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n
}
if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
'Something went wrong. Please report issue.', $response->code, static::getErrorBody($response->body));
'Something went wrong. Please report issue. Body: ' . static::getErrorBody($response->body)
, $response->code, static::getErrorBody($response->body));
}

$this->parseCookies($response->headers);
Expand All @@ -1387,16 +1388,21 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n
}
$rootKey = array_key_exists('graphql', $arr) ? 'graphql' : 'data';

if (empty($arr[$rootKey]['hashtag']['edge_hashtag_to_media']['count'])) {
if (empty($arr[$rootKey]['media_count'])) {
return [];
}

$nodes = $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['edges'];
$payload = $arr[$rootKey]['recent']['sections'];
$nodes = array();
foreach($payload as $p){
$nodes = array_merge($nodes,$p['layout_content']['medias']);
}

foreach ($nodes as $mediaArray) {
if ($index === $count) {
return $medias;
}
$media = Media::create($mediaArray['node']);
$media = Media::create($mediaArray['media']);
if (in_array($media->getId(), $mediaIds)) {
return $medias;
}
Expand All @@ -1410,8 +1416,8 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n
if (empty($nodes)) {
return $medias;
}
$maxId = $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
$hasNextPage = $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
$maxId = $arr[$rootKey]['recent']['next_max_id']; // $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
$hasNextPage = $arr[$rootKey]['recent']['more_available']; // $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
}
return $medias;
}
Expand Down Expand Up @@ -1443,8 +1449,9 @@ public function getPaginateMediasByTag($tag, $maxId = '')
}

if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
'Something went wrong. Please report issue.', $response->code, static::getErrorBody($response->body));
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
'Something went wrong. Please report issue. Body: ' . static::getErrorBody($response->body)
, $response->code, static::getErrorBody($response->body));
}

$this->parseCookies($response->headers);
Expand All @@ -1454,24 +1461,29 @@ public function getPaginateMediasByTag($tag, $maxId = '')
if (!is_array($arr)) {
throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
}
$rootKey = array_key_exists('graphql', $arr) ? 'graphql' : 'data';

if (empty($arr['data']['hashtag']['edge_hashtag_to_media']['count'])) {
if (empty($arr[$rootKey]['media_count'])) {
return $toReturn;
}

$nodes = $arr['data']['hashtag']['edge_hashtag_to_media']['edges'];
$payload = $arr[$rootKey]['recent']['sections'];
$nodes = array();
foreach($payload as $p){
$nodes = array_merge($nodes,$p['layout_content']['medias']);
}

if (empty($nodes)) {
return $toReturn;
}

foreach ($nodes as $mediaArray) {
$medias[] = Media::create($mediaArray['node']);
$medias[] = Media::create($mediaArray['media']);
}

$maxId = $arr['data']['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
$hasNextPage = $arr['graphql']['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
$count = $arr['data']['hashtag']['edge_hashtag_to_media']['count'];
$maxId = $arr[$rootKey]['recent']['next_max_id']; // $arr['data']['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
$hasNextPage = $arr[$rootKey]['recent']['more_available']; // $arr['graphql']['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
$count = $arr[$rootKey]['media_count']; // $arr['data']['hashtag']['edge_hashtag_to_media']['count'];

$toReturn = [
'medias' => $medias,
Expand Down Expand Up @@ -1572,16 +1584,30 @@ public function getCurrentTopMediasByTagName($tagName)
throw new InstagramNotFoundException('Account with given username does not exist.');
}
if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
'Something went wrong. Please report issue.');
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
'Something went wrong. Please report issue. Body: ' . static::getErrorBody($response->body)
, $response->code, static::getErrorBody($response->body));
}

$this->parseCookies($response->headers);
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);

$arr = $this->decodeRawBodyToJson($response->raw_body);

if (!is_array($arr)) {
throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
}
$rootKey = array_key_exists('graphql', $arr) ? 'graphql' : 'data';

$medias = [];
$nodes = (array)@$jsonResponse['graphql']['hashtag']['edge_hashtag_to_top_posts']['edges'];

$payload = $arr[$rootKey]['top']['sections'];
$nodes = array();
foreach($payload as $p){
$nodes = array_merge($nodes,$p['layout_content']['medias']);
}

foreach ($nodes as $mediaArray) {
$medias[] = Media::create($mediaArray['node']);
$medias[] = Media::create($mediaArray['media']);
}
return $medias;
}
Expand Down

0 comments on commit fcc7207

Please sign in to comment.