From 587af7aa1ce771f3f12219ef48ef03783798a260 Mon Sep 17 00:00:00 2001 From: raiym Date: Fri, 23 Mar 2018 12:03:23 +0300 Subject: [PATCH] typos --- src/InstagramScraper/Instagram.php | 365 +++++++++++++-------------- src/InstagramScraper/Model/Media.php | 84 +++--- 2 files changed, 223 insertions(+), 226 deletions(-) diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 760e6763..44ecbb1f 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -27,15 +27,14 @@ class Instagram const PAGING_DELAY_MAXIMUM_MICROSEC = 3000000; // 3 sec max delay to simulate browser private static $instanceCache; + public $pagingTimeLimitSec = self::PAGING_TIME_LIMIT_SEC; + public $pagingDelayMinimumMicrosec = self::PAGING_DELAY_MINIMUM_MICROSEC; + public $pagingDelayMaximumMicrosec = self::PAGING_DELAY_MAXIMUM_MICROSEC; private $sessionUsername; private $sessionPassword; private $userSession; private $userAgent = null; - public $pagingTimeLimitSec = self::PAGING_TIME_LIMIT_SEC; - public $pagingDelayMinimumMicrosec = self::PAGING_DELAY_MINIMUM_MICROSEC; - public $pagingDelayMaximumMicrosec = self::PAGING_DELAY_MAXIMUM_MICROSEC; - /** * @param string $username * @param string $password @@ -100,15 +99,6 @@ public static function searchTagsByTagName($tag) return $hashtags; } - /** - * Set how many media objects should be retrieved in a single request - * @param int $count - */ - public static function setAccountMediasRequestCount($count) - { - Endpoints::setAccountMediasRequestCount($count); - } - /** * @param \stdClass|string $rawError * @@ -131,6 +121,54 @@ private static function getErrorBody($rawError) } + /** + * Set how many media objects should be retrieved in a single request + * @param int $count + */ + public static function setAccountMediasRequestCount($count) + { + Endpoints::setAccountMediasRequestCount($count); + } + + /** + * @param array $config + */ + public static function setProxy(array $config) + { + $defaultConfig = [ + 'port' => false, + 'tunnel' => false, + 'address' => false, + 'type' => CURLPROXY_HTTP, + 'timeout' => false, + 'auth' => [ + 'user' => '', + 'pass' => '', + 'method' => CURLAUTH_BASIC + ], + ]; + + $config = array_replace($defaultConfig, $config); + + Request::proxy($config['address'], $config['port'], $config['type'], $config['tunnel']); + + if (isset($config['auth'])) { + Request::proxyAuth($config['auth']['user'], $config['auth']['pass'], $config['auth']['method']); + } + + if (isset($config['timeout'])) { + Request::timeout((int)$config['timeout']); + } + } + + /** + * Disable proxy for all requests + */ + public static function disableProxy() + { + Request::proxy(''); + } + /** * @param string $username * @@ -164,60 +202,59 @@ public function searchAccountsByUsername($username) } /** - * @param $userAgent + * @param $session * - * @return string + * @return array */ - public function setUserAgent($userAgent) + private function generateHeaders($session) { - return $this->userAgent = $userAgent; + $headers = []; + if ($session) { + $cookies = ''; + foreach ($session as $key => $value) { + $cookies .= "$key=$value; "; + } + $headers = [ + 'cookie' => $cookies, + 'referer' => Endpoints::BASE_URL . '/', + 'x-csrftoken' => $session['csrftoken'], + ]; + } + + if ($this->getUserAgent()) { + $headers['user-agent'] = $this->getUserAgent(); + } + + return $headers; } /** - * @param $userAgent * - * @return null + * @return string */ - public function resetUserAgent($userAgent) + public function getUserAgent() { - return $this->userAgent = null; + return $this->userAgent; } /** + * @param $userAgent * * @return string */ - public function getUserAgent() + public function setUserAgent($userAgent) { - return $this->userAgent; + return $this->userAgent = $userAgent; } /** - * @param $session + * @param $userAgent * - * @return array + * @return null */ - private function generateHeaders($session) + public function resetUserAgent($userAgent) { - $headers = []; - if ($session) { - $cookies = ''; - foreach ($session as $key => $value) { - $cookies .= "$key=$value; "; - } - $headers = [ - 'cookie' => $cookies, - 'referer' => Endpoints::BASE_URL . '/', - 'x-csrftoken' => $session['csrftoken'], - ]; - } - - if($this->getUserAgent()) - { - $headers['user-agent'] = $this->getUserAgent(); - } - - return $headers; + return $this->userAgent = null; } /** @@ -235,7 +272,30 @@ public function getMedias($username, $count = 20, $maxId = '') return $this->getMediasByUserId($account->getId(), $count, $maxId); } - + /** + * @param string $username + * + * @return Account + * @throws InstagramException + * @throws InstagramNotFoundException + */ + public function getAccount($username) + { + $response = Request::get(Endpoints::getAccountJsonLink($username), $this->generateHeaders($this->userSession)); + if (static::HTTP_NOT_FOUND === $response->code) { + throw new InstagramNotFoundException('Account with given username does not exist.'); + } + 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.'); + } + + $userArray = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING); + if (!isset($userArray['graphql']['user'])) { + throw new InstagramNotFoundException('Account with this username does not exist', 404); + } + return Account::create($userArray['graphql']['user']); + } + /** * @param int $id * @param int $count @@ -278,7 +338,6 @@ public function getMediasByUserId($id, $count = 20, $maxId = '') } return $medias; } - /** * @param $mediaId @@ -345,53 +404,53 @@ public function getMediaByCode($mediaCode) */ public function getPaginateMedias($username, $maxId = '') { - $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']; + $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']; $hasNextPage = $arr['data']['user']['edge_owner_to_timeline_media']['page_info']['has_next_page']; - - $toReturn = [ - 'medias' => $medias, - 'maxId' => $maxId, - 'hasNextPage' => $hasNextPage, - ]; - - return $toReturn; + + $toReturn = [ + 'medias' => $medias, + 'maxId' => $maxId, + 'hasNextPage' => $hasNextPage, + ]; + + return $toReturn; } /** @@ -568,7 +627,6 @@ public function getAccountById($id) return $this->getAccount($username); } - /** * @param string $id * @@ -611,30 +669,6 @@ public function getUsernameById($id) return $username; } - /** - * @param string $username - * - * @return Account - * @throws InstagramException - * @throws InstagramNotFoundException - */ - public function getAccount($username) - { - $response = Request::get(Endpoints::getAccountJsonLink($username), $this->generateHeaders($this->userSession)); - if (static::HTTP_NOT_FOUND === $response->code) { - throw new InstagramNotFoundException('Account with given username does not exist.'); - } - 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.'); - } - - $userArray = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING); - if (!isset($userArray['graphql']['user'])) { - throw new InstagramNotFoundException('Account with this username does not exist', 404); - } - return Account::create($userArray['graphql']['user']); - } - /** * @param string $tag * @param int $count @@ -774,7 +808,7 @@ public function getCurrentTopMediasByTagName($tagName) $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING); $medias = []; - $nodes = (array) @$jsonResponse['graphql']['hashtag']['edge_hashtag_to_media']['edges']; + $nodes = (array)@$jsonResponse['graphql']['hashtag']['edge_hashtag_to_media']['edges']; foreach ($nodes as $mediaArray) { $medias[] = Media::create($mediaArray['node']); } @@ -1116,6 +1150,33 @@ public function login($force = false, $support_two_step_verification = false) return $this->generateHeaders($this->userSession); } + /** + * @param $session + * + * @return bool + */ + public function isLoggedIn($session) + { + if (is_null($session) || !isset($session['sessionid'])) { + return false; + } + $sessionId = $session['sessionid']; + $csrfToken = $session['csrftoken']; + $headers = ['cookie' => "csrftoken=$csrfToken; sessionid=$sessionId;", + 'referer' => Endpoints::BASE_URL . '/', + 'x-csrftoken' => $csrfToken, + ]; + $response = Request::get(Endpoints::BASE_URL, $headers); + if ($response->code !== 200) { + return false; + } + $cookies = static::parseCookies($response->headers['Set-Cookie']); + if (!isset($cookies['ds_user_id'])) { + return false; + } + return true; + } + private function verifyTwoStep($response, $cookies) { $new_cookies = static::parseCookies($response->headers['Set-Cookie']); @@ -1197,33 +1258,6 @@ private function verifyTwoStep($response, $cookies) return $response; } - /** - * @param $session - * - * @return bool - */ - public function isLoggedIn($session) - { - if (is_null($session) || !isset($session['sessionid'])) { - return false; - } - $sessionId = $session['sessionid']; - $csrfToken = $session['csrftoken']; - $headers = ['cookie' => "csrftoken=$csrfToken; sessionid=$sessionId;", - 'referer' => Endpoints::BASE_URL . '/', - 'x-csrftoken' => $csrfToken, - ]; - $response = Request::get(Endpoints::BASE_URL, $headers); - if ($response->code !== 200) { - return false; - } - $cookies = static::parseCookies($response->headers['Set-Cookie']); - if (!isset($cookies['ds_user_id'])) { - return false; - } - return true; - } - /** * */ @@ -1232,43 +1266,4 @@ public function saveSession() $cachedString = static::$instanceCache->getItem($this->sessionUsername); $cachedString->set($this->userSession); } - - /** - * @param array $config - */ - public static function setProxy(array $config) - { - $defaultConfig = [ - 'port' => false, - 'tunnel' => false, - 'address' => false, - 'type' => CURLPROXY_HTTP, - 'timeout' => false, - 'auth' => [ - 'user' => '', - 'pass' => '', - 'method' => CURLAUTH_BASIC - ], - ]; - - $config = array_replace($defaultConfig, $config); - - Request::proxy($config['address'], $config['port'], $config['type'], $config['tunnel']); - - if (isset($config['auth'])) { - Request::proxyAuth($config['auth']['user'], $config['auth']['pass'], $config['auth']['method']); - } - - if (isset($config['timeout'])) { - Request::timeout((int)$config['timeout']); - } - } - - /** - * Disable proxy for all requests - */ - public static function disableProxy() - { - Request::proxy(''); - } } diff --git a/src/InstagramScraper/Model/Media.php b/src/InstagramScraper/Model/Media.php index 2086a21c..8b4bdd07 100644 --- a/src/InstagramScraper/Model/Media.php +++ b/src/InstagramScraper/Model/Media.php @@ -262,7 +262,8 @@ public function getImageHighResolutionUrl() /** * @return array */ - public function getSquareThumbnailsUrl() { + public function getSquareThumbnailsUrl() + { return $this->squareThumbnailsUrl; } @@ -409,32 +410,33 @@ protected function initPropertiesCustom($value, $prop, $arr) $this->likesCount = $arr[$prop]['count']; break; case 'thumbnail_resources': - foreach( $value as $thumbnail ) { - $thumbnailsUrl[] = $thumbnail['src']; - switch ($thumbnail['config_width']) { - case 150: - $this->imageThumbnailUrl = $thumbnail['src']; - break; - case 320: - $this->imageLowResolutionUrl = $thumbnail['src']; - break; - case 640: - $this->imageStandardResolutionUrl = $thumbnail['src']; - break; - default:; - } + foreach ($value as $thumbnail) { + $thumbnailsUrl[] = $thumbnail['src']; + switch ($thumbnail['config_width']) { + case 150: + $this->imageThumbnailUrl = $thumbnail['src']; + break; + case 320: + $this->imageLowResolutionUrl = $thumbnail['src']; + break; + case 640: + $this->imageStandardResolutionUrl = $thumbnail['src']; + break; + default: + ; + } } $this->squareThumbnailsUrl = $thumbnailsUrl; break; case 'display_url': - $this->imageHighResolutionUrl = $value; - break; + $this->imageHighResolutionUrl = $value; + break; case 'display_src': - $this->imageHighResolutionUrl = $value; - if (!isset($this->type)) { - $this->type = static::TYPE_IMAGE; - } - break; + $this->imageHighResolutionUrl = $value; + if (!isset($this->type)) { + $this->type = static::TYPE_IMAGE; + } + break; case 'carousel_media': $this->type = self::TYPE_CAROUSEL; $this->carouselMedia = []; @@ -502,7 +504,7 @@ protected function initPropertiesCustom($value, $prop, $arr) $this->likesCount = $arr[$prop]['count']; break; case 'edge_liked_by': - $this->likesCount = $arr[$prop]['count']; + $this->likesCount = $arr[$prop]['count']; break; case 'edge_media_to_caption': if (is_array($arr[$prop]['edges']) && !empty($arr[$prop]['edges'])) { @@ -547,24 +549,6 @@ protected function initPropertiesCustom($value, $prop, $arr) } } - /** - * @param string $imageUrl - * - * @return array - */ - private static function getImageUrls($imageUrl) - { - $parts = explode('/', parse_url($imageUrl)['path']); - $imageName = $parts[sizeof($parts) - 1]; - $urls = [ - 'thumbnail' => Endpoints::INSTAGRAM_CDN_URL . 't/s150x150/' . $imageName, - 'low' => Endpoints::INSTAGRAM_CDN_URL . 't/s320x320/' . $imageName, - 'standard' => Endpoints::INSTAGRAM_CDN_URL . 't/s640x640/' . $imageName, - 'high' => Endpoints::INSTAGRAM_CDN_URL . 't/' . $imageName, - ]; - return $urls; - } - /** * @param $mediaArray * @param $carouselArray @@ -599,6 +583,24 @@ private static function setCarouselMedia($mediaArray, $carouselArray, $instance) return $mediaArray; } + /** + * @param string $imageUrl + * + * @return array + */ + private static function getImageUrls($imageUrl) + { + $parts = explode('/', parse_url($imageUrl)['path']); + $imageName = $parts[sizeof($parts) - 1]; + $urls = [ + 'thumbnail' => Endpoints::INSTAGRAM_CDN_URL . 't/s150x150/' . $imageName, + 'low' => Endpoints::INSTAGRAM_CDN_URL . 't/s320x320/' . $imageName, + 'standard' => Endpoints::INSTAGRAM_CDN_URL . 't/s640x640/' . $imageName, + 'high' => Endpoints::INSTAGRAM_CDN_URL . 't/' . $imageName, + ]; + return $urls; + } + /** * @return Account */