From 5d8a2aff8f888bd26f21905293343c74dc746be3 Mon Sep 17 00:00:00 2001 From: fonclub Date: Mon, 24 Jan 2022 11:14:44 +0300 Subject: [PATCH] New feature and fix (#1024) * Add method for get info about current authenticated user * Change getMediaUrl and Media class to new Instagram structure --- src/InstagramScraper/Instagram.php | 30 +++++- src/InstagramScraper/Model/Media.php | 152 +++++++++++++++++++++++---- 2 files changed, 158 insertions(+), 24 deletions(-) diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index aacdb7f3..15858429 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -819,10 +819,10 @@ public function getMediaByUrl($mediaUrl) } $mediaArray = $this->decodeRawBodyToJson($response->raw_body); - if (!isset($mediaArray['graphql']['shortcode_media'])) { + if (!isset($mediaArray['items'])) { throw new InstagramException('Media with this code does not exist'); } - return Media::create($mediaArray['graphql']['shortcode_media']); + return Media::create(current($mediaArray['items'])); } /** @@ -2627,4 +2627,30 @@ public function allowPendingRequests($limit = 10) ['thread_ids' => json_encode($threadIds)] ); } + + /** + * @return Account + * @throws InstagramException + * @throws InstagramNotFoundException + */ + public function getCurrentUserInfo(): Account + { + $response = Request::get(Endpoints::USER_FEED_hash, $this->generateHeaders($this->userSession)); + + if ($response->code === static::HTTP_NOT_FOUND) { + throw new InstagramNotFoundException('Account with given username does not exist.'); + } + if ($response->code !== static::HTTP_OK) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + } + + $this->parseCookies($response->headers); + $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); + + if(!isset($jsonResponse['data']['user']['username'])){ + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + } + + return $this->getAccount($jsonResponse['data']['user']['username']); + } } diff --git a/src/InstagramScraper/Model/Media.php b/src/InstagramScraper/Model/Media.php index 89abfec4..8ce300de 100644 --- a/src/InstagramScraper/Model/Media.php +++ b/src/InstagramScraper/Model/Media.php @@ -14,6 +14,10 @@ class Media extends AbstractModel const TYPE_VIDEO = 'video'; const TYPE_SIDECAR = 'sidecar'; const TYPE_CAROUSEL = 'carousel'; + + const MEDIA_TYPE_IMAGE = 1; + const MEDIA_TYPE_VIDEO = 2; + const MEDIA_TYPE_CAROUSEL = 8; /** * @var string @@ -197,6 +201,8 @@ class Media extends AbstractModel * @var array */ protected $taggedUsersIds=[]; + + private $media_type; /** * @param string $code @@ -587,7 +593,6 @@ protected function initPropertiesCustom($value, $prop, $arr) $this->commentsDisabled = $value; break; case 'comments': - $this->commentsCount = $arr[$prop]['count']; break; case 'edge_liked_by': case 'edge_media_preview_like': @@ -637,7 +642,7 @@ protected function initPropertiesCustom($value, $prop, $arr) } break; case 'caption': - $this->caption = $arr[$prop]; + $this->caption = $value['text'] ?? ''; break; case 'accessibility_caption': $this->altText = $value; @@ -666,10 +671,10 @@ protected function initPropertiesCustom($value, $prop, $arr) break; case 'location': if (isset($arr[$prop])) { - $this->locationId = $arr[$prop]['id'] ? $arr[$prop]['id'] : null; - $this->locationName = $arr[$prop]['name'] ? $arr[$prop]['name'] : null; - $this->locationSlug = $arr[$prop]['slug'] ? $arr[$prop]['slug'] : null; - $this->locationAddressJson = isset($arr[$prop]['address_json']) ? $arr[$prop]['address_json'] : null; + $this->locationId = $arr[$prop]['id'] ?? null; + $this->locationName = $arr[$prop]['name'] ?? null; + $this->locationSlug = $arr[$prop]['slug'] ?? null; + $this->locationAddressJson = $arr[$prop]['address_json'] ?? null; } break; case 'owner': @@ -684,6 +689,7 @@ protected function initPropertiesCustom($value, $prop, $arr) case 'video_url': $this->videoStandardResolutionUrl = $value; break; + case 'view_count': case 'video_view_count': $this->videoViews = $value; break; @@ -777,6 +783,57 @@ protected function initPropertiesCustom($value, $prop, $arr) } } break; + case 'comment_count': + $this->commentsCount = $value; + break; + case 'media_type': + $this->media_type = $value; + switch ($value){ + case static::MEDIA_TYPE_IMAGE: + $this->type = static::TYPE_IMAGE; + break; + case static::MEDIA_TYPE_VIDEO: + $this->type = static::TYPE_VIDEO; + break; + case static::MEDIA_TYPE_CAROUSEL: + $this->type = static::TYPE_CAROUSEL; + break; + } + break; + case 'image_versions2': + foreach ($value['candidates'] as $media) { + $mediasUrl[] = $media['url']; + switch ($media['width']) { + case 150: + $this->imageThumbnailUrl = $media['url']; + break; + case 320: + $this->imageLowResolutionUrl = $media['url']; + break; + case 750: + $this->imageStandardResolutionUrl = $media['url']; + break; + case 1080: + $this->imageHighResolutionUrl = $media['url']; + break; + } + } + break; + case 'video_versions': + foreach ($value as $media) { + switch ($media['type']) { + case 101: + $this->videoStandardResolutionUrl = $media['url']; + break; + case 102: + $this->videoLowResolutionUrl = $media['url']; + break; + case 103: + $this->videoLowBandwidthUrl = $media['url']; + break; + } + } + break; } if (!$this->ownerId && !is_null($this->owner)) { $this->ownerId = $this->getOwner()->getId(); @@ -793,25 +850,76 @@ protected function initPropertiesCustom($value, $prop, $arr) private static function setCarouselMedia($mediaArray, $carouselArray, $instance) { $carouselMedia = new CarouselMedia(); - $carouselMedia->setType($carouselArray['type']); - - if (isset($carouselArray['images'])) { - $carouselImages = self::getImageUrls($carouselArray['images']['standard_resolution']['url']); - $carouselMedia->setImageLowResolutionUrl($carouselImages['low']); - $carouselMedia->setImageThumbnailUrl($carouselImages['thumbnail']); - $carouselMedia->setImageStandardResolutionUrl($carouselImages['standard']); - $carouselMedia->setImageHighResolutionUrl($carouselImages['high']); - } + if(isset($carouselArray['type'])) { + $carouselMedia->setType($carouselArray['type']); + + if (isset($carouselArray['images'])) { + $carouselImages = self::getImageUrls($carouselArray['images']['standard_resolution']['url']); + $carouselMedia->setImageLowResolutionUrl($carouselImages['low']); + $carouselMedia->setImageThumbnailUrl($carouselImages['thumbnail']); + $carouselMedia->setImageStandardResolutionUrl($carouselImages['standard']); + $carouselMedia->setImageHighResolutionUrl($carouselImages['high']); + } - if ($carouselMedia->getType() === self::TYPE_VIDEO) { - if (isset($mediaArray['video_views'])) { - $carouselMedia->setVideoViews($carouselArray['video_views']); + if ($carouselMedia->getType() === self::TYPE_VIDEO) { + if (isset($mediaArray['video_views'])) { + $carouselMedia->setVideoViews($carouselArray['video_views']); + } + if (isset($carouselArray['videos'])) { + $carouselMedia->setVideoLowResolutionUrl($carouselArray['videos']['low_resolution']['url']); + $carouselMedia->setVideoStandardResolutionUrl($carouselArray['videos']['standard_resolution']['url']); + $carouselMedia->setVideoLowBandwidthUrl($carouselArray['videos']['low_bandwidth']['url']); + } + } + } elseif(isset($carouselArray['media_type'])) { + switch ($carouselArray['media_type']){ + case static::MEDIA_TYPE_IMAGE: + $carouselMedia->setType(static::TYPE_IMAGE); + break; + case static::MEDIA_TYPE_VIDEO: + $carouselMedia->setType(static::TYPE_VIDEO); + break; } - if (isset($carouselArray['videos'])) { - $carouselMedia->setVideoLowResolutionUrl($carouselArray['videos']['low_resolution']['url']); - $carouselMedia->setVideoStandardResolutionUrl($carouselArray['videos']['standard_resolution']['url']); - $carouselMedia->setVideoLowBandwidthUrl($carouselArray['videos']['low_bandwidth']['url']); + + if($carouselArray['media_type'] == static::MEDIA_TYPE_VIDEO && isset($carouselArray['video_versions'])){ + if (isset($mediaArray['view_count'])) { + $carouselMedia->setVideoViews($carouselArray['view_count']); + } + + foreach ($carouselArray['video_versions'] as $media) { + switch ($media['type']) { + case 101: + $carouselMedia->setVideoStandardResolutionUrl($media['url']); + break; + case 102: + $carouselMedia->setVideoLowResolutionUrl($media['url']); + break; + case 103: + $carouselMedia->setVideoLowBandwidthUrl($media['url']); + break; + } + } } + + if($carouselArray['media_type'] == static::MEDIA_TYPE_IMAGE && isset($carouselArray['image_versions2'])){ + foreach ($carouselArray['image_versions2']['candidates'] as $media) { + $mediasUrl[] = $media['url']; + switch ($media['width']) { + case 150: + $carouselMedia->setImageThumbnailUrl($media['url']); + break; + case 320: + $carouselMedia->setImageLowResolutionUrl($media['url']); + break; + case 750: + $carouselMedia->setImageStandardResolutionUrl($media['url']); + break; + case 1080: + $carouselMedia->setImageHighResolutionUrl($media['url']); + break; + } + } + } } array_push($instance->carouselMedia, $carouselMedia); return $mediaArray;