Skip to content

Commit

Permalink
Merge pull request postaddictme#290 from aik27/master
Browse files Browse the repository at this point in the history
 Hotfix for Instagram changes
  • Loading branch information
raiym authored Mar 14, 2018
2 parents 6a4b982 + 93a13e4 commit c527b5b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 88 deletions.
8 changes: 4 additions & 4 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Endpoints
const LOGIN_URL = 'https://www.instagram.com/accounts/login/ajax/';
const ACCOUNT_PAGE = 'https://www.instagram.com/{username}';
const MEDIA_LINK = 'https://www.instagram.com/p/{code}';
const ACCOUNT_MEDIAS = 'https://www.instagram.com/{username}/?__a=1&max_id={max_id}';
const ACCOUNT_MEDIAS = 'https://instagram.com/graphql/query/?query_id=17888483320059182&id={user_id}&first=30&after={max_id}';
const ACCOUNT_JSON_INFO = 'https://www.instagram.com/{username}/?__a=1';
const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1';
const MEDIA_JSON_BY_LOCATION_ID = 'https://www.instagram.com/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}';
Expand Down Expand Up @@ -49,10 +49,10 @@ public static function getAccountJsonInfoLinkByAccountId($id)
return str_replace('{userId}', urlencode($id), static::ACCOUNT_JSON_INFO_BY_ID);
}

public static function getAccountMediasJsonLink($username, $maxId = '')
public static function getAccountMediasJsonLink($userId, $maxId = '')
{
$url = str_replace('{username}', urlencode($username), static::ACCOUNT_MEDIAS);
return str_replace('{max_id}', urlencode($maxId), $url);
$url = str_replace('{user_id}', urlencode($userId), static::ACCOUNT_MEDIAS);
return str_replace('{max_id}', urlencode($maxId), $url);
}

public static function getMediaPageLink($code)
Expand Down
161 changes: 81 additions & 80 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,38 +221,38 @@ private function generateHeaders($session)
*/
public function getMedias($username, $count = 20, $maxId = '')
{
$index = 0;
$medias = [];
$isMoreAvailable = true;
while ($index < $count && $isMoreAvailable) {
$response = Request::get(Endpoints::getAccountMediasJsonLink($username, $maxId), $this->generateHeaders($this->userSession));
if (static::HTTP_OK !== $response->code) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
if (!is_array($arr)) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}
$nodes = $arr['user']['media']['nodes'];
// fix - count takes longer/has more overhead
if (!isset($nodes) || empty($nodes)) {
return [];
}
foreach ($nodes as $mediaArray) {
if ($index === $count) {
return $medias;
}
$medias[] = Media::create($mediaArray);
$index++;
}
if (empty($nodes) || !isset($nodes)) {
return $medias;
}
$maxId = $nodes[count($nodes) - 1]['id'];
$isMoreAvailable = $arr['user']['media']['page_info']['has_next_page'];
}
return $medias;
$account = $this->getAccount($username);
$index = 0;
$medias = [];
$isMoreAvailable = true;
while ($index < $count && $isMoreAvailable) {
$response = Request::get(Endpoints::getAccountMediasJsonLink($account->getId(), $maxId), $this->generateHeaders($this->userSession));
if (static::HTTP_OK !== $response->code) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}
$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
if (!is_array($arr)) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}
$nodes = $arr['data']['user']['edge_owner_to_timeline_media']['edges'];
// fix - count takes longer/has more overhead
if (!isset($nodes) || empty($nodes)) {
return [];
}
foreach ($nodes as $mediaArray) {
if ($index === $count) {
return $medias;
}
$medias[] = Media::create($mediaArray['node']);
$index++;
}
if (empty($nodes) || !isset($nodes)) {
return $medias;
}
$maxId = $arr['data']['user']['edge_owner_to_timeline_media']['page_info']['end_cursor'];
$isMoreAvailable = $arr['data']['user']['edge_owner_to_timeline_media']['page_info']['has_next_page'];
}
return $medias;
}

/**
Expand Down Expand Up @@ -320,52 +320,53 @@ public function getMediaByCode($mediaCode)
*/
public function getPaginateMedias($username, $maxId = '')
{
$hasNextPage = true;
$medias = [];

$toReturn = [
'medias' => $medias,
'maxId' => $maxId,
'hasNextPage' => $hasNextPage,
];

$response = Request::get(Endpoints::getAccountMediasJsonLink($username, $maxId),
$this->generateHeaders($this->userSession));

// use a raw constant in the code is not a good idea!!
//if ($response->code !== 200) {
if (static::HTTP_OK !== $response->code) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);

if (!is_array($arr)) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}
$nodes = $arr['user']['media']['nodes'];

//if (count($arr['items']) === 0) {
// 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.
// If you simply need to know whether or not the array is empty then use empty.
if (empty($nodes)) {
return $toReturn;
}

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

$maxId = $arr['user']['media']['page_info']['end_cursor'];
$hasNextPage = $arr['user']['media']['page_info']['has_next_page'];

$toReturn = [
'medias' => $medias,
'maxId' => $maxId,
'hasNextPage' => $hasNextPage,
];

return $toReturn;
$account = $this->getAccount($username);
$hasNextPage = true;
$medias = [];

$toReturn = [
'medias' => $medias,
'maxId' => $maxId,
'hasNextPage' => $hasNextPage,
];

$response = Request::get(Endpoints::getAccountMediasJsonLink($account->getId(), $maxId),
$this->generateHeaders($this->userSession));

// use a raw constant in the code is not a good idea!!
//if ($response->code !== 200) {
if (static::HTTP_OK !== $response->code) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);

if (!is_array($arr)) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}
$nodes = $arr['data']['user']['edge_owner_to_timeline_media']['edges'];

//if (count($arr['items']) === 0) {
// 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.
// If you simply need to know whether or not the array is empty then use empty.
if (empty($nodes)) {
return $toReturn;
}

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

$maxId = $arr['data']['user']['edge_owner_to_timeline_media']['page_info']['end_cursor'];
$isMoreAvailable = $arr['data']['user']['edge_owner_to_timeline_media']['page_info']['has_next_page'];

$toReturn = [
'medias' => $medias,
'maxId' => $maxId,
'hasNextPage' => $hasNextPage,
];

return $toReturn;
}

/**
Expand Down Expand Up @@ -589,10 +590,10 @@ public function getAccount($username)
}

$userArray = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
if (!isset($userArray['user'])) {
if (!isset($userArray['graphql']['user'])) {
throw new InstagramException('Account with this username does not exist');
}
return Account::create($userArray['user']);
return Account::create($userArray['graphql']['user']);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/InstagramScraper/Model/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ protected function initPropertiesCustom($value, $prop, $array)
case 'external_url':
$this->externalUrl = $value;
break;
case 'follows':
case 'edge_follow':
$this->followsCount = !empty($array[$prop]['count']) ? (int)$array[$prop]['count'] : 0;
break;
case 'followed_by':
case 'edge_followed_by':
$this->followedByCount = !empty($array[$prop]['count']) ? (int)$array[$prop]['count'] : 0;
break;
case 'media':
case 'edge_owner_to_timeline_media':
$this->mediaCount = !empty($array[$prop]['count']) ? $array[$prop]['count'] : 0;
break;
case 'is_private':
Expand All @@ -245,4 +245,4 @@ protected function initPropertiesCustom($value, $prop, $array)
break;
}
}
}
}

0 comments on commit c527b5b

Please sign in to comment.