Skip to content

Commit fcc7207

Browse files
authored
Add &__d=dis to MEDIA_JSON_BY_TAG and update JSON response path in getMediasByTag(), getPaginateMediasByTag(), and getCurrentTopMediasByTagName() (fix #1052) (#1091)
1 parent 230f1eb commit fcc7207

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

src/InstagramScraper/Endpoints.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Endpoints
1414
const ACCOUNT_ACTIVITY = 'https://www.instagram.com/accounts/activity/?__a=1';
1515
const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1&__d=dis';
1616
const MEDIA_JSON_BY_LOCATION_ID = 'https://www.instagram.com/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}';
17-
const MEDIA_JSON_BY_TAG = 'https://www.instagram.com/explore/tags/{tag}/?__a=1&max_id={max_id}';
17+
const MEDIA_JSON_BY_TAG = 'https://www.instagram.com/explore/tags/{tag}/?__a=1&max_id={max_id}&__d=dis';
1818
const GENERAL_SEARCH = 'https://www.instagram.com/web/search/topsearch/?query={query}&count={count}';
1919
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}';
2020
const COMMENTS_BEFORE_COMMENT_ID_BY_CODE = 'https://www.instagram.com/graphql/query/?query_hash=33ba35852cb50da46f5b5e889df7d159&variables={variables}';

src/InstagramScraper/Instagram.php

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,8 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n
13751375
}
13761376
if ($response->code !== static::HTTP_OK) {
13771377
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
1378-
'Something went wrong. Please report issue.', $response->code, static::getErrorBody($response->body));
1378+
'Something went wrong. Please report issue. Body: ' . static::getErrorBody($response->body)
1379+
, $response->code, static::getErrorBody($response->body));
13791380
}
13801381

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

1390-
if (empty($arr[$rootKey]['hashtag']['edge_hashtag_to_media']['count'])) {
1391+
if (empty($arr[$rootKey]['media_count'])) {
13911392
return [];
13921393
}
13931394

1394-
$nodes = $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['edges'];
1395+
$payload = $arr[$rootKey]['recent']['sections'];
1396+
$nodes = array();
1397+
foreach($payload as $p){
1398+
$nodes = array_merge($nodes,$p['layout_content']['medias']);
1399+
}
1400+
13951401
foreach ($nodes as $mediaArray) {
13961402
if ($index === $count) {
13971403
return $medias;
13981404
}
1399-
$media = Media::create($mediaArray['node']);
1405+
$media = Media::create($mediaArray['media']);
14001406
if (in_array($media->getId(), $mediaIds)) {
14011407
return $medias;
14021408
}
@@ -1410,8 +1416,8 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n
14101416
if (empty($nodes)) {
14111417
return $medias;
14121418
}
1413-
$maxId = $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
1414-
$hasNextPage = $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
1419+
$maxId = $arr[$rootKey]['recent']['next_max_id']; // $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
1420+
$hasNextPage = $arr[$rootKey]['recent']['more_available']; // $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
14151421
}
14161422
return $medias;
14171423
}
@@ -1443,8 +1449,9 @@ public function getPaginateMediasByTag($tag, $maxId = '')
14431449
}
14441450

14451451
if ($response->code !== static::HTTP_OK) {
1446-
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
1447-
'Something went wrong. Please report issue.', $response->code, static::getErrorBody($response->body));
1452+
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
1453+
'Something went wrong. Please report issue. Body: ' . static::getErrorBody($response->body)
1454+
, $response->code, static::getErrorBody($response->body));
14481455
}
14491456

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

1458-
if (empty($arr['data']['hashtag']['edge_hashtag_to_media']['count'])) {
1466+
if (empty($arr[$rootKey]['media_count'])) {
14591467
return $toReturn;
14601468
}
14611469

1462-
$nodes = $arr['data']['hashtag']['edge_hashtag_to_media']['edges'];
1470+
$payload = $arr[$rootKey]['recent']['sections'];
1471+
$nodes = array();
1472+
foreach($payload as $p){
1473+
$nodes = array_merge($nodes,$p['layout_content']['medias']);
1474+
}
14631475

14641476
if (empty($nodes)) {
14651477
return $toReturn;
14661478
}
14671479

14681480
foreach ($nodes as $mediaArray) {
1469-
$medias[] = Media::create($mediaArray['node']);
1481+
$medias[] = Media::create($mediaArray['media']);
14701482
}
14711483

1472-
$maxId = $arr['data']['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
1473-
$hasNextPage = $arr['graphql']['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
1474-
$count = $arr['data']['hashtag']['edge_hashtag_to_media']['count'];
1484+
$maxId = $arr[$rootKey]['recent']['next_max_id']; // $arr['data']['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'];
1485+
$hasNextPage = $arr[$rootKey]['recent']['more_available']; // $arr['graphql']['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page'];
1486+
$count = $arr[$rootKey]['media_count']; // $arr['data']['hashtag']['edge_hashtag_to_media']['count'];
14751487

14761488
$toReturn = [
14771489
'medias' => $medias,
@@ -1572,16 +1584,30 @@ public function getCurrentTopMediasByTagName($tagName)
15721584
throw new InstagramNotFoundException('Account with given username does not exist.');
15731585
}
15741586
if ($response->code !== static::HTTP_OK) {
1575-
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
1576-
'Something went wrong. Please report issue.');
1587+
throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' .
1588+
'Something went wrong. Please report issue. Body: ' . static::getErrorBody($response->body)
1589+
, $response->code, static::getErrorBody($response->body));
15771590
}
15781591

15791592
$this->parseCookies($response->headers);
1580-
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);
1593+
1594+
$arr = $this->decodeRawBodyToJson($response->raw_body);
1595+
1596+
if (!is_array($arr)) {
1597+
throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
1598+
}
1599+
$rootKey = array_key_exists('graphql', $arr) ? 'graphql' : 'data';
1600+
15811601
$medias = [];
1582-
$nodes = (array)@$jsonResponse['graphql']['hashtag']['edge_hashtag_to_top_posts']['edges'];
1602+
1603+
$payload = $arr[$rootKey]['top']['sections'];
1604+
$nodes = array();
1605+
foreach($payload as $p){
1606+
$nodes = array_merge($nodes,$p['layout_content']['medias']);
1607+
}
1608+
15831609
foreach ($nodes as $mediaArray) {
1584-
$medias[] = Media::create($mediaArray['node']);
1610+
$medias[] = Media::create($mediaArray['media']);
15851611
}
15861612
return $medias;
15871613
}

0 commit comments

Comments
 (0)