Skip to content

Commit

Permalink
merge changes from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
mewlabs committed Feb 13, 2019
2 parents b7f8743 + bb619f0 commit 66737cf
Show file tree
Hide file tree
Showing 3 changed files with 327 additions and 55 deletions.
51 changes: 22 additions & 29 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,7 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null)
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.', $response->code);
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$cookies = static::parseCookies($response->headers);
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);
$nodes = $jsonResponse['data']['shortcode_media']['edge_media_to_comment']['edges'];
foreach ($nodes as $commentArray) {
Expand All @@ -721,12 +720,14 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null)
/**
* We work only on https in this case if we have same cookies on Secure and not - we will choice Secure cookie
*
* @param string $rawCookies
* @param array $headers
*
* @return array
*/
private static function parseCookies($rawCookies)
private static function parseCookies($headers)
{
$rawCookies = isset($headers['Set-Cookie']) ? $headers['Set-Cookie'] : isset($headers['set-cookie']) ? $headers['set-cookie'] : [];

if (!is_array($rawCookies)) {
$rawCookies = [$rawCookies];
}
Expand All @@ -751,6 +752,11 @@ private static function parseCookies($rawCookies)
}

$cookies = $secure_cookies + $not_secure_cookies;

if (isset($cookies['csrftoken'])) {
$this->userSession['csrftoken'] = $cookies['csrftoken'];
}

return $cookies;
}

Expand Down Expand Up @@ -788,7 +794,6 @@ public function getMediaLikesByCode($code, $count = 10, $maxId = null)
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . $response->body . ' Something went wrong. Please report issue.', $response->code);
}
$cookies = self::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];

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

Expand Down Expand Up @@ -877,8 +882,7 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}

$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$cookies = static::parseCookies($response->headers);

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

Expand Down Expand Up @@ -939,8 +943,7 @@ public function getPaginateMediasByTag($tag, $maxId = '')
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}

$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$cookies = static::parseCookies($response->headers);

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

Expand Down Expand Up @@ -995,8 +998,7 @@ public function getCurrentTopMediasByTagName($tagName)
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$cookies = static::parseCookies($response->headers);
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);
$medias = [];
$nodes = (array)@$jsonResponse['graphql']['hashtag']['edge_hashtag_to_top_posts']['edges'];
Expand All @@ -1023,8 +1025,7 @@ public function getCurrentTopMediasByLocationId($facebookLocationId)
if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$cookies = static::parseCookies($response->headers);
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);
$nodes = $jsonResponse['location']['top_posts']['nodes'];
$medias = [];
Expand Down Expand Up @@ -1053,8 +1054,7 @@ public function getMediasByLocationId($facebookLocationId, $quantity = 24, $offs
if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$cookies = static::parseCookies($response->headers);
$arr = $this->decodeRawBodyToJson($response->raw_body);
$nodes = $arr['graphql']['location']['edge_location_to_media']['edges'];
foreach ($nodes as $mediaArray) {
Expand Down Expand Up @@ -1156,8 +1156,7 @@ public function getLocationById($facebookLocationId)
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}

$cookies = static::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$cookies = static::parseCookies($response->headers);
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);
return Location::create($jsonResponse['graphql']['location']);
}
Expand Down Expand Up @@ -1444,11 +1443,8 @@ public function login($force = false, $support_two_step_verification = false)
if (isset($match[1])) {
$csrfToken = $match[1];
}
if (isset($response->headers['Set-Cookie'])):
$cookies = static::parseCookies($response->headers['Set-Cookie']);
else:
$cookies = static::parseCookies($response->headers['set-cookie']);
endif;
$cookies = static::parseCookies($response->headers);

$mid = $cookies['mid'];
$headers = [
'cookie' => "ig_cb=1; csrftoken=$csrfToken; mid=$mid;",
Expand Down Expand Up @@ -1476,11 +1472,8 @@ public function login($force = false, $support_two_step_verification = false)
}
}

if (isset($response->headers['Set-Cookie'])):
$cookies = static::parseCookies($response->headers['Set-Cookie']);
else:
$cookies = static::parseCookies($response->headers['set-cookie']);
endif;
$cookies = static::parseCookies($response->headers);

$cookies['mid'] = $mid;
$cachedString->set($cookies);
static::$instanceCache->save($cachedString);
Expand Down Expand Up @@ -1515,7 +1508,7 @@ public function isLoggedIn($session)
if ($response->code !== static::HTTP_OK) {
return false;
}
$cookies = static::parseCookies($response->headers['Set-Cookie']);
$cookies = static::parseCookies($response->headers);
if (!isset($cookies['ds_user_id'])) {
return false;
}
Expand All @@ -1530,7 +1523,7 @@ public function isLoggedIn($session)
*/
private function verifyTwoStep($response, $cookies)
{
$new_cookies = static::parseCookies($response->headers['Set-Cookie']);
$new_cookies = static::parseCookies($response->headers);
$cookies = array_merge($cookies, $new_cookies);
$cookie_string = '';
foreach ($cookies as $name => $value) {
Expand Down
Loading

0 comments on commit 66737cf

Please sign in to comment.