diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index b25b150b..36b6ead1 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -9,6 +9,7 @@ class Endpoints const ACCOUNT_PAGE = 'https://www.instagram.com/{username}/'; const MEDIA_LINK = 'https://www.instagram.com/p/{code}'; const ACCOUNT_MEDIAS = 'https://www.instagram.com/graphql/query/?query_hash=e769aa130647d2354c40ea6a439bfc08&variables={variables}'; + const ACCOUNT_TAGGED_MEDIAS = 'https://www.instagram.com/graphql/query/?query_hash=be13233562af2d229b008d2976b998b5&variables={variables}'; const ACCOUNT_JSON_INFO = 'https://www.instagram.com/{username}/?__a=1'; const ACCOUNT_ACTIVITY = 'https://www.instagram.com/accounts/activity/?__a=1'; const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1'; @@ -86,6 +87,11 @@ public static function getAccountMediasJsonLink($variables) return str_replace('{variables}', urlencode($variables), static::ACCOUNT_MEDIAS); } + public static function getAccountTaggedMediasJsonLink($variables) + { + return str_replace('{variables}', urlencode($variables), static::ACCOUNT_TAGGED_MEDIAS); + } + public static function getMediaPageLink($code) { return str_replace('{code}', urlencode($code), static::MEDIA_LINK); diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index c4569a6e..8b633e98 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -501,6 +501,22 @@ public function getMedias($username, $count = 20, $maxId = '') return $this->getMediasByUserId($account->getId(), $count, $maxId); } + /** + * @param string $username + * @param int $count + * @param string $maxId + * + * @return Media[] + * @throws InstagramException + * @throws InstagramNotFoundException + */ + public function getTaggedMedias($username, $count = 20, $maxId = '') + { + + $account = $this->getAccount($username); + return $this->getTaggedMediasByUserId($account->getId(), $count, $maxId); + } + /** * @param string $username * @@ -622,6 +638,60 @@ public function getMediasByUserId($id, $count = 12, $maxId = '') return $medias; } + /** + * @param int $id + * @param int $count + * @param string $maxId + * + * @return Media[] + * @throws InstagramException + * @throws InstagramNotFoundException + */ + public function getTaggedMediasByUserId($id, $count = 12, $maxId = '') + { + $index = 0; + $medias = []; + $isMoreAvailable = true; + while ($index < $count && $isMoreAvailable) { + $variables = json_encode([ + 'id' => (string)$id, + 'first' => (string)$count, + 'after' => (string)$maxId + ]); + + $response = Request::get(Endpoints::getAccountTaggedMediasJsonLink($variables), $this->generateHeaders($this->userSession, $this->generateGisToken($variables))); + + if (static::HTTP_NOT_FOUND === $response->code) { + throw new InstagramNotFoundException('Account with given id 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.', $response->code); + } + + $arr = $this->decodeRawBodyToJson($response->raw_body); + + if (!is_array($arr)) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code); + } + + $nodes = $arr['data']['user']['edge_user_to_photos_of_you']['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++; + } + $maxId = $arr['data']['user']['edge_user_to_photos_of_you']['page_info']['end_cursor']; + $isMoreAvailable = $arr['data']['user']['edge_user_to_photos_of_you']['page_info']['has_next_page']; + } + return $medias; + } + /** * @param $variables * @return string