diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index 3b9c2d44..6442c77f 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -36,68 +36,68 @@ class Endpoints public static function getAccountPageLink($username) { - return str_replace('{username}', urlencode($username), Endpoints::ACCOUNT_PAGE); + return str_replace('{username}', urlencode($username), static::ACCOUNT_PAGE); } public static function getAccountJsonLink($username) { - return str_replace('{username}', urlencode($username), Endpoints::ACCOUNT_JSON_INFO); + return str_replace('{username}', urlencode($username), static::ACCOUNT_JSON_INFO); } public static function getAccountJsonInfoLinkByAccountId($id) { - return str_replace('{userId}', urlencode($id), Endpoints::ACCOUNT_JSON_INFO_BY_ID); + return str_replace('{userId}', urlencode($id), static::ACCOUNT_JSON_INFO_BY_ID); } public static function getAccountMediasJsonLink($username, $maxId = '') { - $url = str_replace('{username}', urlencode($username), Endpoints::ACCOUNT_MEDIAS); + $url = str_replace('{username}', urlencode($username), static::ACCOUNT_MEDIAS); return str_replace('{max_id}', urlencode($maxId), $url); } public static function getMediaPageLink($code) { - return str_replace('{code}', urlencode($code), Endpoints::MEDIA_LINK); + return str_replace('{code}', urlencode($code), static::MEDIA_LINK); } public static function getMediaJsonLink($code) { - return str_replace('{code}', urlencode($code), Endpoints::MEDIA_JSON_INFO); + return str_replace('{code}', urlencode($code), static::MEDIA_JSON_INFO); } public static function getMediasJsonByLocationIdLink($facebookLocationId, $maxId = '') { - $url = str_replace('{{facebookLocationId}}', urlencode($facebookLocationId), Endpoints::MEDIA_JSON_BY_LOCATION_ID); + $url = str_replace('{{facebookLocationId}}', urlencode($facebookLocationId), static::MEDIA_JSON_BY_LOCATION_ID); return str_replace('{{maxId}}', urlencode($maxId), $url); } public static function getMediasJsonByTagLink($tag, $maxId = '') { - $url = str_replace('{tag}', urlencode($tag), Endpoints::MEDIA_JSON_BY_TAG); + $url = str_replace('{tag}', urlencode($tag), static::MEDIA_JSON_BY_TAG); return str_replace('{max_id}', urlencode($maxId), $url); } public static function getGeneralSearchJsonLink($query) { - return str_replace('{query}', urlencode($query), Endpoints::GENERAL_SEARCH); + return str_replace('{query}', urlencode($query), static::GENERAL_SEARCH); } public static function getCommentsBeforeCommentIdByCode($code, $count, $commentId) { - $url = str_replace('{{shortcode}}', urlencode($code), Endpoints::COMMENTS_BEFORE_COMMENT_ID_BY_CODE); + $url = str_replace('{{shortcode}}', urlencode($code), static::COMMENTS_BEFORE_COMMENT_ID_BY_CODE); $url = str_replace('{{count}}', urlencode($count), $url); return str_replace('{{commentId}}', urlencode($commentId), $url); } public static function getLastLikesByCodeLink($code) { - $url = str_replace('{{code}}', urlencode($code), Endpoints::LAST_LIKES_BY_CODE); + $url = str_replace('{{code}}', urlencode($code), static::LAST_LIKES_BY_CODE); return $url; } public static function getGraphQlUrl($queryId, $parameters) { - $url = str_replace('{{queryId}}', urlencode($queryId), Endpoints::GRAPH_QL_QUERY_URL); + $url = str_replace('{{queryId}}', urlencode($queryId), static::GRAPH_QL_QUERY_URL); foreach ($parameters as $key => $value) { $url .= "&$key=$value"; } @@ -106,13 +106,13 @@ public static function getGraphQlUrl($queryId, $parameters) public static function getFollowUrl($accountId) { - $url = str_replace('{{accountId}}', urlencode($accountId), Endpoints::FOLLOW_URL); + $url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOW_URL); return $url; } public static function getFollowersJsonLink($accountId, $count, $after = '') { - $url = str_replace('{{accountId}}', urlencode($accountId), Endpoints::FOLLOWERS_URL); + $url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOWERS_URL); $url = str_replace('{{count}}', urlencode($count), $url); if ($after === '') { diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index d1c22812..d73e66cc 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -40,9 +40,9 @@ public static function withCredentials($username, $password, $sessionFolder = nu CacheManager::setDefaultConfig([ 'path' => $sessionFolder, ]); - self::$instanceCache = CacheManager::getInstance('files'); + static::$instanceCache = CacheManager::getInstance('files'); } else { - self::$instanceCache = $sessionFolder; + static::$instanceCache = $sessionFolder; } $instance = new self(); $instance->sessionUsername = $username; @@ -63,17 +63,17 @@ public static function searchTagsByTagName($tag) $response = Request::get(Endpoints::getGeneralSearchJsonLink($tag)); // use a raw constant in the code is not a good idea!! //if ($response->code === 404) { - if (self::HTTP_NOT_FOUND === $response->code) { + if (static::HTTP_NOT_FOUND === $response->code) { throw new InstagramNotFoundException('Account with given username does not exist.'); } // use a raw constant in the code is not a good idea!! //if ($response->code !== 200) { - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + 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.'); } $jsonResponse = json_decode($response->raw_body, true); - if (!isset($jsonResponse['status']) || $jsonResponse['status'] != 'ok') { + if (!isset($jsonResponse['status']) || $jsonResponse['status'] !== 'ok') { throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.'); } @@ -98,13 +98,13 @@ private static function getErrorBody($rawError) return $rawError; } if (is_object($rawError)) { - $str = ""; + $str = ''; foreach ($rawError as $key => $value) { - $str .= " " . $key . " => " . $value . ";"; + $str .= ' ' . $key . ' => ' . $value . ';'; } return $str; } else { - return "Unknown body format"; + return 'Unknown body format'; } } @@ -119,15 +119,15 @@ private static function getErrorBody($rawError) public function searchAccountsByUsername($username) { $response = Request::get(Endpoints::getGeneralSearchJsonLink($username), $this->generateHeaders($this->userSession)); - if (self::HTTP_NOT_FOUND === $response->code) { + if (static::HTTP_NOT_FOUND === $response->code) { throw new InstagramNotFoundException('Account with given username does not exist.'); } - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + 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.'); } $jsonResponse = json_decode($response->raw_body, true); - if (!isset($jsonResponse['status']) || $jsonResponse['status'] != 'ok') { + if (!isset($jsonResponse['status']) || $jsonResponse['status'] !== 'ok') { throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.'); } if (!isset($jsonResponse['users']) || empty($jsonResponse['users'])) { @@ -178,13 +178,13 @@ public function getMedias($username, $count = 20, $maxId = '') $isMoreAvailable = true; while ($index < $count && $isMoreAvailable) { $response = Request::get(Endpoints::getAccountMediasJsonLink($username, $maxId), $this->generateHeaders($this->userSession)); - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + 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); if (!is_array($arr)) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } // fix - count takes longer/has more overhead if (empty($arr['items']) || !isset($arr['items'])) { @@ -232,13 +232,13 @@ public function getMediaByUrl($mediaUrl) $response = Request::get(rtrim($mediaUrl, '/') . '/?__a=1', $this->generateHeaders($this->userSession)); // use a raw constant in the code is not a good idea!! //if ($response->code === 404) { - if (self::HTTP_NOT_FOUND === $response->code) { + if (static::HTTP_NOT_FOUND === $response->code) { throw new InstagramNotFoundException('Media with given code does not exist or account is private.'); } // use a raw constant in the code is not a good idea!! //if ($response->code !== 200) { - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + 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); if (!isset($mediaArray['graphql']['shortcode_media'])) { @@ -285,14 +285,14 @@ public function getPaginateMedias($username, $maxId = '') // use a raw constant in the code is not a good idea!! //if ($response->code !== 200) { - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + 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); if (!is_array($arr)) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } //if (count($arr['items']) === 0) { @@ -328,7 +328,7 @@ public function getPaginateMedias($username, $maxId = '') public function getMediaCommentsById($mediaId, $count = 10, $maxId = null) { $code = Media::getCodeFromId($mediaId); - return self::getMediaCommentsByCode($code, $count, $maxId); + return static::getMediaCommentsByCode($code, $count, $maxId); } /** @@ -346,10 +346,10 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null) $index = 0; $hasPrevious = true; while ($hasPrevious && $index < $count) { - if ($remain > self::MAX_COMMENTS_PER_REQUEST) { - $numberOfCommentsToRetreive = self::MAX_COMMENTS_PER_REQUEST; - $remain -= self::MAX_COMMENTS_PER_REQUEST; - $index += self::MAX_COMMENTS_PER_REQUEST; + if ($remain > static::MAX_COMMENTS_PER_REQUEST) { + $numberOfCommentsToRetreive = static::MAX_COMMENTS_PER_REQUEST; + $remain -= static::MAX_COMMENTS_PER_REQUEST; + $index += static::MAX_COMMENTS_PER_REQUEST; } else { $numberOfCommentsToRetreive = $remain; $index += $remain; @@ -363,10 +363,10 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null) $response = Request::get($commentsUrl, $this->generateHeaders($this->userSession)); // use a raw constant in the code is not a good idea!! //if ($response->code !== 200) { - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + 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.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = json_decode($response->raw_body, true); $nodes = $jsonResponse['data']['shortcode_media']['edge_media_to_comment']['edges']; @@ -413,6 +413,7 @@ private static function parseCookies($rawCookies) * * @return Account * @throws InstagramException + * @throws \InvalidArgumentException */ public function getAccountById($id) { @@ -435,10 +436,10 @@ public function getAccountById($id) } if ($response->code !== 302) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->raw_body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->raw_body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; // Get the username from the response url. @@ -459,11 +460,11 @@ public function getAccountById($id) public function getAccount($username) { $response = Request::get(Endpoints::getAccountJsonLink($username), $this->generateHeaders($this->userSession)); - if (self::HTTP_NOT_FOUND === $response->code) { + if (static::HTTP_NOT_FOUND === $response->code) { throw new InstagramNotFoundException('Account with given username does not exist.'); } - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + 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); @@ -492,9 +493,9 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n $response = Request::get(Endpoints::getMediasJsonByTagLink($tag, $maxId), $this->generateHeaders($this->userSession)); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $arr = json_decode($response->raw_body, true); if (!is_array($arr)) { @@ -550,10 +551,10 @@ public function getPaginateMediasByTag($tag, $maxId = '') $this->generateHeaders($this->userSession)); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $arr = json_decode($response->raw_body, true); @@ -605,9 +606,9 @@ public function getCurrentTopMediasByTagName($tagName) throw new InstagramNotFoundException('Account with given username does not exist.'); } if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = json_decode($response->raw_body, true); $medias = []; @@ -632,9 +633,9 @@ public function getCurrentTopMediasByLocationId($facebookLocationId) throw new InstagramNotFoundException('Location with this id doesn\'t exist'); } if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = json_decode($response->raw_body, true); $nodes = $jsonResponse['location']['top_posts']['nodes']; @@ -662,9 +663,9 @@ public function getMediasByLocationId($facebookLocationId, $quantity = 12, $offs $response = Request::get(Endpoints::getMediasJsonByLocationIdLink($facebookLocationId, $offset), $this->generateHeaders($this->userSession)); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $arr = json_decode($response->raw_body, true); $nodes = $arr['location']['media']['nodes']; @@ -699,9 +700,9 @@ public function getLocationById($facebookLocationId) throw new InstagramNotFoundException('Location with this id doesn\'t exist'); } if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = json_decode($response->raw_body, true); return Location::create($jsonResponse['location']); @@ -734,7 +735,7 @@ public function getFollowers($accountId, $count = 20, $pageSize = 20, $delayed = $response = Request::get(Endpoints::getFollowersJsonLink($accountId, $pageSize, $endCursor), $this->generateHeaders($this->userSession)); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + 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); @@ -786,14 +787,14 @@ public function login($force = false) throw new InstagramAuthException("User credentials not provided"); } - $cachedString = self::$instanceCache->getItem($this->sessionUsername); + $cachedString = static::$instanceCache->getItem($this->sessionUsername); $session = $cachedString->get(); if ($force || !$this->isLoggedIn($session)) { $response = Request::get(Endpoints::BASE_URL); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $mid = $cookies['mid']; $csrfToken = $cookies['csrftoken']; $headers = ['cookie' => "csrftoken=$csrfToken; mid=$mid;", @@ -817,10 +818,10 @@ public function login($force = false) } } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $cookies['mid'] = $mid; $cachedString->set($cookies); - self::$instanceCache->save($cachedString); + static::$instanceCache->save($cachedString); $this->userSession = $cookies; } else { $this->userSession = $session; @@ -849,7 +850,7 @@ public function isLoggedIn($session) if ($response->code !== 200) { return false; } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); if (!isset($cookies['ds_user_id'])) { return false; } @@ -861,7 +862,7 @@ public function isLoggedIn($session) */ public function saveSession() { - $cachedString = self::$instanceCache->getItem($this->sessionUsername); + $cachedString = static::$instanceCache->getItem($this->sessionUsername); $cachedString->set($this->userSession); }