Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = null)
  2.     {
  3.         $index = 0;
  4.         $medias = [];
  5.         $mediaIds = [];
  6.         $hasNextPage = true;
  7.         while ($index < $count && $hasNextPage) {
  8.             $response = Request::get(Endpoints::getMediasJsonByTagLink($tag, $maxId),
  9.                 $this->generateHeaders($this->userSession));
  10.             if ($response->code !== 200) {
  11.                 throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
  12.             }
  13.             $cookies = static::parseCookies($response->headers['Set-Cookie']);
  14.             $this->userSession['csrftoken'] = $cookies['csrftoken'];
  15.             $arr = json_decode($response->raw_body, true);
  16.             if (!is_array($arr)) {
  17.                 throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
  18.             }
  19.             if (empty($arr['tag']['media']['count'])) {
  20.                 return [];
  21.             }
  22.             $nodes = $arr['tag']['media']['nodes'];
  23.             foreach ($nodes as $mediaArray) {
  24.                 if ($index === $count) {
  25.                     return $medias;
  26.                 }
  27.                 $media = Media::create($mediaArray);
  28.                 if (in_array($media->getId(), $mediaIds)) {
  29.                     return $medias;
  30.                 }
  31.                 if (isset($minTimestamp) && $media->getCreatedTime() < $minTimestamp) {
  32.                     return $medias;
  33.                 }
  34.                 $mediaIds[] = $media->getId();
  35.                 $medias[] = $media;
  36.                 $index++;
  37.             }
  38.             if (empty($nodes)) {
  39.                 return $medias;
  40.             }
  41.             $maxId = $arr['tag']['media']['page_info']['end_cursor'];
  42.             $hasNextPage = $arr['tag']['media']['page_info']['has_next_page'];
  43.         }
  44.         return $medias;
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement