Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = null)
- {
- $index = 0;
- $medias = [];
- $mediaIds = [];
- $hasNextPage = true;
- while ($index < $count && $hasNextPage) {
- $response = Request::get(Endpoints::getMediasJsonByTagLink($tag, $maxId),
- $this->generateHeaders($this->userSession));
- if ($response->code !== 200) {
- 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'];
- $arr = json_decode($response->raw_body, true);
- if (!is_array($arr)) {
- throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
- }
- if (empty($arr['tag']['media']['count'])) {
- return [];
- }
- $nodes = $arr['tag']['media']['nodes'];
- foreach ($nodes as $mediaArray) {
- if ($index === $count) {
- return $medias;
- }
- $media = Media::create($mediaArray);
- if (in_array($media->getId(), $mediaIds)) {
- return $medias;
- }
- if (isset($minTimestamp) && $media->getCreatedTime() < $minTimestamp) {
- return $medias;
- }
- $mediaIds[] = $media->getId();
- $medias[] = $media;
- $index++;
- }
- if (empty($nodes)) {
- return $medias;
- }
- $maxId = $arr['tag']['media']['page_info']['end_cursor'];
- $hasNextPage = $arr['tag']['media']['page_info']['has_next_page'];
- }
- return $medias;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement