Skip to content

Commit c527b5b

Browse files
authored
Merge pull request #290 from aik27/master
Hotfix for Instagram changes
2 parents 6a4b982 + 93a13e4 commit c527b5b

File tree

3 files changed

+89
-88
lines changed

3 files changed

+89
-88
lines changed

src/InstagramScraper/Endpoints.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Endpoints
88
const LOGIN_URL = 'https://www.instagram.com/accounts/login/ajax/';
99
const ACCOUNT_PAGE = 'https://www.instagram.com/{username}';
1010
const MEDIA_LINK = 'https://www.instagram.com/p/{code}';
11-
const ACCOUNT_MEDIAS = 'https://www.instagram.com/{username}/?__a=1&max_id={max_id}';
11+
const ACCOUNT_MEDIAS = 'https://instagram.com/graphql/query/?query_id=17888483320059182&id={user_id}&first=30&after={max_id}';
1212
const ACCOUNT_JSON_INFO = 'https://www.instagram.com/{username}/?__a=1';
1313
const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1';
1414
const MEDIA_JSON_BY_LOCATION_ID = 'https://www.instagram.com/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}';
@@ -49,10 +49,10 @@ public static function getAccountJsonInfoLinkByAccountId($id)
4949
return str_replace('{userId}', urlencode($id), static::ACCOUNT_JSON_INFO_BY_ID);
5050
}
5151

52-
public static function getAccountMediasJsonLink($username, $maxId = '')
52+
public static function getAccountMediasJsonLink($userId, $maxId = '')
5353
{
54-
$url = str_replace('{username}', urlencode($username), static::ACCOUNT_MEDIAS);
55-
return str_replace('{max_id}', urlencode($maxId), $url);
54+
$url = str_replace('{user_id}', urlencode($userId), static::ACCOUNT_MEDIAS);
55+
return str_replace('{max_id}', urlencode($maxId), $url);
5656
}
5757

5858
public static function getMediaPageLink($code)

src/InstagramScraper/Instagram.php

Lines changed: 81 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -221,38 +221,38 @@ private function generateHeaders($session)
221221
*/
222222
public function getMedias($username, $count = 20, $maxId = '')
223223
{
224-
$index = 0;
225-
$medias = [];
226-
$isMoreAvailable = true;
227-
while ($index < $count && $isMoreAvailable) {
228-
$response = Request::get(Endpoints::getAccountMediasJsonLink($username, $maxId), $this->generateHeaders($this->userSession));
229-
if (static::HTTP_OK !== $response->code) {
230-
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
231-
}
232-
233-
$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
234-
if (!is_array($arr)) {
235-
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
236-
}
237-
$nodes = $arr['user']['media']['nodes'];
238-
// fix - count takes longer/has more overhead
239-
if (!isset($nodes) || empty($nodes)) {
240-
return [];
241-
}
242-
foreach ($nodes as $mediaArray) {
243-
if ($index === $count) {
244-
return $medias;
245-
}
246-
$medias[] = Media::create($mediaArray);
247-
$index++;
248-
}
249-
if (empty($nodes) || !isset($nodes)) {
250-
return $medias;
251-
}
252-
$maxId = $nodes[count($nodes) - 1]['id'];
253-
$isMoreAvailable = $arr['user']['media']['page_info']['has_next_page'];
254-
}
255-
return $medias;
224+
$account = $this->getAccount($username);
225+
$index = 0;
226+
$medias = [];
227+
$isMoreAvailable = true;
228+
while ($index < $count && $isMoreAvailable) {
229+
$response = Request::get(Endpoints::getAccountMediasJsonLink($account->getId(), $maxId), $this->generateHeaders($this->userSession));
230+
if (static::HTTP_OK !== $response->code) {
231+
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
232+
}
233+
$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
234+
if (!is_array($arr)) {
235+
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
236+
}
237+
$nodes = $arr['data']['user']['edge_owner_to_timeline_media']['edges'];
238+
// fix - count takes longer/has more overhead
239+
if (!isset($nodes) || empty($nodes)) {
240+
return [];
241+
}
242+
foreach ($nodes as $mediaArray) {
243+
if ($index === $count) {
244+
return $medias;
245+
}
246+
$medias[] = Media::create($mediaArray['node']);
247+
$index++;
248+
}
249+
if (empty($nodes) || !isset($nodes)) {
250+
return $medias;
251+
}
252+
$maxId = $arr['data']['user']['edge_owner_to_timeline_media']['page_info']['end_cursor'];
253+
$isMoreAvailable = $arr['data']['user']['edge_owner_to_timeline_media']['page_info']['has_next_page'];
254+
}
255+
return $medias;
256256
}
257257

258258
/**
@@ -320,52 +320,53 @@ public function getMediaByCode($mediaCode)
320320
*/
321321
public function getPaginateMedias($username, $maxId = '')
322322
{
323-
$hasNextPage = true;
324-
$medias = [];
325-
326-
$toReturn = [
327-
'medias' => $medias,
328-
'maxId' => $maxId,
329-
'hasNextPage' => $hasNextPage,
330-
];
331-
332-
$response = Request::get(Endpoints::getAccountMediasJsonLink($username, $maxId),
333-
$this->generateHeaders($this->userSession));
334-
335-
// use a raw constant in the code is not a good idea!!
336-
//if ($response->code !== 200) {
337-
if (static::HTTP_OK !== $response->code) {
338-
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
339-
}
340-
341-
$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
342-
343-
if (!is_array($arr)) {
344-
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
345-
}
346-
$nodes = $arr['user']['media']['nodes'];
347-
348-
//if (count($arr['items']) === 0) {
349-
// I generally use empty. Im not sure why people would use count really - If the array is large then count takes longer/has more overhead.
350-
// If you simply need to know whether or not the array is empty then use empty.
351-
if (empty($nodes)) {
352-
return $toReturn;
353-
}
354-
355-
foreach ($nodes as $mediaArray) {
356-
$medias[] = Media::create($mediaArray);
357-
}
358-
359-
$maxId = $arr['user']['media']['page_info']['end_cursor'];
360-
$hasNextPage = $arr['user']['media']['page_info']['has_next_page'];
361-
362-
$toReturn = [
363-
'medias' => $medias,
364-
'maxId' => $maxId,
365-
'hasNextPage' => $hasNextPage,
366-
];
367-
368-
return $toReturn;
323+
$account = $this->getAccount($username);
324+
$hasNextPage = true;
325+
$medias = [];
326+
327+
$toReturn = [
328+
'medias' => $medias,
329+
'maxId' => $maxId,
330+
'hasNextPage' => $hasNextPage,
331+
];
332+
333+
$response = Request::get(Endpoints::getAccountMediasJsonLink($account->getId(), $maxId),
334+
$this->generateHeaders($this->userSession));
335+
336+
// use a raw constant in the code is not a good idea!!
337+
//if ($response->code !== 200) {
338+
if (static::HTTP_OK !== $response->code) {
339+
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
340+
}
341+
342+
$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
343+
344+
if (!is_array($arr)) {
345+
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
346+
}
347+
$nodes = $arr['data']['user']['edge_owner_to_timeline_media']['edges'];
348+
349+
//if (count($arr['items']) === 0) {
350+
// I generally use empty. Im not sure why people would use count really - If the array is large then count takes longer/has more overhead.
351+
// If you simply need to know whether or not the array is empty then use empty.
352+
if (empty($nodes)) {
353+
return $toReturn;
354+
}
355+
356+
foreach ($nodes as $mediaArray) {
357+
$medias[] = Media::create($mediaArray['node']);
358+
}
359+
360+
$maxId = $arr['data']['user']['edge_owner_to_timeline_media']['page_info']['end_cursor'];
361+
$isMoreAvailable = $arr['data']['user']['edge_owner_to_timeline_media']['page_info']['has_next_page'];
362+
363+
$toReturn = [
364+
'medias' => $medias,
365+
'maxId' => $maxId,
366+
'hasNextPage' => $hasNextPage,
367+
];
368+
369+
return $toReturn;
369370
}
370371

371372
/**
@@ -589,10 +590,10 @@ public function getAccount($username)
589590
}
590591

591592
$userArray = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
592-
if (!isset($userArray['user'])) {
593+
if (!isset($userArray['graphql']['user'])) {
593594
throw new InstagramException('Account with this username does not exist');
594595
}
595-
return Account::create($userArray['user']);
596+
return Account::create($userArray['graphql']['user']);
596597
}
597598

598599
/**

src/InstagramScraper/Model/Account.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ protected function initPropertiesCustom($value, $prop, $array)
228228
case 'external_url':
229229
$this->externalUrl = $value;
230230
break;
231-
case 'follows':
231+
case 'edge_follow':
232232
$this->followsCount = !empty($array[$prop]['count']) ? (int)$array[$prop]['count'] : 0;
233233
break;
234-
case 'followed_by':
234+
case 'edge_followed_by':
235235
$this->followedByCount = !empty($array[$prop]['count']) ? (int)$array[$prop]['count'] : 0;
236236
break;
237-
case 'media':
237+
case 'edge_owner_to_timeline_media':
238238
$this->mediaCount = !empty($array[$prop]['count']) ? $array[$prop]['count'] : 0;
239239
break;
240240
case 'is_private':
@@ -245,4 +245,4 @@ protected function initPropertiesCustom($value, $prop, $array)
245245
break;
246246
}
247247
}
248-
}
248+
}

0 commit comments

Comments
 (0)