Skip to content

Commit

Permalink
Parse big integers as strings in json
Browse files Browse the repository at this point in the history
  • Loading branch information
raiym committed Nov 23, 2017
1 parent e7b08cf commit 09d6cb5
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 156 deletions.
15 changes: 8 additions & 7 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Endpoints
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_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 LIKES_BY_SHORTCODE = 'https://www.instagram.com/graphql/query/?query_id=17864450716183058&variables={"shortcode":"{{shortcode}}","first":{{count}},"after":"{{likeId}}"}';
const LIKES_BY_SHORTCODE = 'https://www.instagram.com/graphql/query/?query_id=17864450716183058&variables={"shortcode":"{{shortcode}}","first":{{count}},"after":"{{likeId}}"}';
const FOLLOWING_URL = 'https://www.instagram.com/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}';
const FOLLOWERS_URL = 'https://www.instagram.com/graphql/query/?query_id=17851374694183129&id={{accountId}}&first={{count}}&after={{after}}';
const FOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/follow/';
Expand Down Expand Up @@ -95,14 +95,15 @@ public static function getLastLikesByCodeLink($code)
$url = str_replace('{{code}}', urlencode($code), static::LAST_LIKES_BY_CODE);
return $url;
}

public static function getLastLikesByCode($code, $count, $lastLikeID) {
$url = str_replace('{{shortcode}}', urlencode($code), static::LIKES_BY_SHORTCODE);
$url = str_replace('{{count}}', urlencode($count), $url);
$url = str_replace('{{likeId}}', urlencode($lastLikeID), $url);

public static function getLastLikesByCode($code, $count, $lastLikeID)
{
$url = str_replace('{{shortcode}}', urlencode($code), static::LIKES_BY_SHORTCODE);
$url = str_replace('{{count}}', urlencode($count), $url);
$url = str_replace('{{likeId}}', urlencode($lastLikeID), $url);

return $url;
}
}

public static function getGraphQlUrl($queryId, $parameters)
{
Expand Down
92 changes: 46 additions & 46 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Instagram
const HTTP_NOT_FOUND = 404;
const HTTP_OK = 200;
const MAX_COMMENTS_PER_REQUEST = 300;
const MAX_LIKES_PER_REQUEST = 300;
const MAX_LIKES_PER_REQUEST = 300;

private static $instanceCache;
private $sessionUsername;
Expand Down Expand Up @@ -74,7 +74,7 @@ public static function searchTagsByTagName($tag)
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$jsonResponse = json_decode($response->raw_body, true);
$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
if (!isset($jsonResponse['status']) || $jsonResponse['status'] !== 'ok') {
throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.');
}
Expand Down Expand Up @@ -128,7 +128,7 @@ public function searchAccountsByUsername($username)
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$jsonResponse = json_decode($response->raw_body, true);
$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
if (!isset($jsonResponse['status']) || $jsonResponse['status'] !== 'ok') {
throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.');
}
Expand Down Expand Up @@ -184,7 +184,7 @@ public function getMedias($username, $count = 20, $maxId = '')
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);
$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.');
}
Expand Down Expand Up @@ -243,7 +243,7 @@ public function getMediaByUrl($mediaUrl)
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.');
}
$mediaArray = json_decode($response->raw_body, true);
$mediaArray = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
if (!isset($mediaArray['graphql']['shortcode_media'])) {
throw new InstagramException('Media with this code does not exist');
}
Expand Down Expand Up @@ -292,7 +292,7 @@ public function getPaginateMedias($username, $maxId = '')
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);
$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.');
Expand Down Expand Up @@ -372,7 +372,7 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null)
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$jsonResponse = json_decode($response->raw_body, true);
$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
$nodes = $jsonResponse['data']['shortcode_media']['edge_media_to_comment']['edges'];
foreach ($nodes as $commentArray) {
$comments[] = Comment::create($commentArray['node']);
Expand All @@ -389,10 +389,32 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null)
}
return $comments;
}

/**

/**
* @param string $rawCookies
*
* @return array
*/
private static function parseCookies($rawCookies)
{
if (!is_array($rawCookies)) {
$rawCookies = [$rawCookies];
}

$cookies = [];
foreach ($rawCookies as $c) {
$c = explode(';', $c)[0];
$parts = explode('=', $c);
if (sizeof($parts) >= 2 && !is_null($parts[1])) {
$cookies[$parts[0]] = $parts[1];
}
}
return $cookies;
}

/**
* @param $code
* @param int $count
* @param int $count
* @param null $maxId
*
* @return array
Expand Down Expand Up @@ -425,14 +447,14 @@ public function getMediaLikesByCode($code, $count = 10, $maxId = null)
}
$cookies = self::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$jsonResponse = json_decode($response->raw_body, true);
$nodes = $jsonResponse['data']['shortcode_media']['edge_liked_by']['edges'];
$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);

$nodes = $jsonResponse['data']['shortcode_media']['edge_liked_by']['edges'];

foreach ($nodes as $likesArray) {
$likes[] = Like::create($likesArray['node']);
}

$hasPrevious = $jsonResponse['data']['shortcode_media']['edge_liked_by']['page_info']['has_next_page'];
$numberOfLikes = $jsonResponse['data']['shortcode_media']['edge_liked_by']['count'];
if ($count > $numberOfLikes) {
Expand All @@ -443,30 +465,8 @@ public function getMediaLikesByCode($code, $count = 10, $maxId = null)
}
$maxId = $jsonResponse['data']['shortcode_media']['edge_liked_by']['page_info']['end_cursor'];
}

return $likes;
}

/**
* @param string $rawCookies
*
* @return array
*/
private static function parseCookies($rawCookies)
{
if (!is_array($rawCookies)) {
$rawCookies = [$rawCookies];
}

$cookies = [];
foreach ($rawCookies as $c) {
$c = explode(';', $c)[0];
$parts = explode('=', $c);
if (sizeof($parts) >= 2 && !is_null($parts[1])) {
$cookies[$parts[0]] = $parts[1];
}
}
return $cookies;
return $likes;
}

/**
Expand Down Expand Up @@ -528,7 +528,7 @@ public function getAccount($username)
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$userArray = json_decode($response->raw_body, true);
$userArray = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
if (!isset($userArray['user'])) {
throw new InstagramException('Account with this username does not exist');
}
Expand Down Expand Up @@ -558,7 +558,7 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$arr = json_decode($response->raw_body, true);
$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
if (!is_array($arr)) {
throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
}
Expand Down Expand Up @@ -618,7 +618,7 @@ public function getPaginateMediasByTag($tag, $maxId = '')
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];

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

if (!is_array($arr)) {
throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
Expand Down Expand Up @@ -671,7 +671,7 @@ public function getCurrentTopMediasByTagName($tagName)
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$jsonResponse = json_decode($response->raw_body, true);
$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
$medias = [];
foreach ($jsonResponse['tag']['top_posts']['nodes'] as $mediaArray) {
$medias[] = Media::create($mediaArray);
Expand All @@ -698,7 +698,7 @@ public function getCurrentTopMediasByLocationId($facebookLocationId)
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$jsonResponse = json_decode($response->raw_body, true);
$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
$nodes = $jsonResponse['location']['top_posts']['nodes'];
$medias = [];
foreach ($nodes as $mediaArray) {
Expand Down Expand Up @@ -728,7 +728,7 @@ public function getMediasByLocationId($facebookLocationId, $quantity = 12, $offs
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$arr = json_decode($response->raw_body, true);
$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
$nodes = $arr['location']['media']['nodes'];
foreach ($nodes as $mediaArray) {
if ($index === $quantity) {
Expand Down Expand Up @@ -765,7 +765,7 @@ public function getLocationById($facebookLocationId)
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$jsonResponse = json_decode($response->raw_body, true);
$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
return Location::create($jsonResponse['location']);
}

Expand Down Expand Up @@ -799,7 +799,7 @@ public function getFollowers($accountId, $count = 20, $pageSize = 20, $delayed =
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

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

if ($jsonResponse['data']['user']['edge_followed_by']['count'] === 0) {
return $accounts;
Expand Down
4 changes: 2 additions & 2 deletions src/InstagramScraper/Model/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function getId()
{
return $this->id;
}
/**

/**
* @return mixed
*/
public function getUserName()
Expand Down
Loading

0 comments on commit 09d6cb5

Please sign in to comment.