From 8ae164be0c2849befa2d3e6d14317bf54652e7f0 Mon Sep 17 00:00:00 2001 From: Fiki Pratama <55952660+nsmle@users.noreply.github.com> Date: Fri, 19 Aug 2022 23:51:43 +0700 Subject: [PATCH] Fix timeline media bug (#329) * Fix timeline media bug * Fix timeline media bug --- src/Instagram/Hydrator/CarouselHydrator.php | 55 +++++++++++-------- src/Instagram/Hydrator/ImageHydrator.php | 12 +++- src/Instagram/Hydrator/ReelsHydrator.php | 5 +- .../Hydrator/TimelineFeedHydrator.php | 26 ++++++++- src/Instagram/Model/TimelineFeed.php | 21 +++++++ src/Instagram/Transport/TimelineDataFeed.php | 18 ++---- 6 files changed, 93 insertions(+), 44 deletions(-) diff --git a/src/Instagram/Hydrator/CarouselHydrator.php b/src/Instagram/Hydrator/CarouselHydrator.php index 0d5a5e7..3c6ea02 100644 --- a/src/Instagram/Hydrator/CarouselHydrator.php +++ b/src/Instagram/Hydrator/CarouselHydrator.php @@ -6,7 +6,7 @@ use Instagram\Exception\InstagramFetchException; use Instagram\Hydrator\UserInfoHydrator; -use Instagram\Model\{Media, Carousel}; +use Instagram\Model\{Media, Carousel, Image}; use Instagram\Utils\InstagramHelper; class CarouselHydrator @@ -76,28 +76,34 @@ public function carouselMediaHydrator(array $carouselItems): array { $carouselMedias = []; foreach ($carouselItems as $carouselItem) { - $carouselType = $this->getTypeCarousel($carouselItem->media_type); - - if ($carouselType == Media::TYPE_IMAGE) { - $carouselMedia = [ - 'id' => $carouselItem->pk, - 'parentId' => $carouselItem->carousel_parent_id, - 'type' => $carouselType, - 'width' => $carouselItem->original_width, - 'height' => $carouselItem->original_height, - ]; - - if (property_exists($carouselItem, 'image_versions2')) { - $carouselMedia['image'] = $carouselItem->image_versions2->candidates; - } - - if (property_exists($carouselItem, 'video_versions')) { - $carouselMedia['video'] = $carouselItem->video_versions; - } - - if (property_exists($carouselItem, 'accessibility_caption')) { - $carouselMedia['accessibilityCaption'] = $carouselItem->accessibility_caption; - } + $carouselType = $this->getCarouselType($carouselItem->media_type); + + $carouselMedia = [ + 'id' => $carouselItem->pk, + 'parentId' => $carouselItem->carousel_parent_id, + 'type' => $carouselType, + 'width' => $carouselItem->original_width, + 'height' => $carouselItem->original_height, + ]; + + if (property_exists($carouselItem, 'image_versions2')) { + $carouselMedia['image'] = $carouselItem->image_versions2->candidates; + } + + if (property_exists($carouselItem, 'video_versions')) { + $carouselMedia['video'] = $carouselItem->video_versions; + } + + if (property_exists($carouselItem, 'video_duration')) { + $carouselMedia['duration'] = $carouselItem->video_duration; + } + + if (property_exists($carouselItem, 'accessibility_caption')) { + $carouselMedia['accessibilityCaption'] = $carouselItem->accessibility_caption; + } + + if (property_exists($carouselItem, 'number_of_qualities')) { + $carouselMedia['quality'] = $carouselItem->number_of_qualities; } $carouselMedias[] = (object) $carouselMedia; @@ -108,10 +114,11 @@ public function carouselMediaHydrator(array $carouselItems): array /** * @param int $media_type + * * @return string * @throws InstagramFetchException */ - private function getTypeCarousel(int $media_type): string + private function getCarouselType(int $media_type): string { switch ($media_type) { case Media::MEDIA_TYPE_IMAGE: diff --git a/src/Instagram/Hydrator/ImageHydrator.php b/src/Instagram/Hydrator/ImageHydrator.php index 4712aa9..358629e 100644 --- a/src/Instagram/Hydrator/ImageHydrator.php +++ b/src/Instagram/Hydrator/ImageHydrator.php @@ -35,7 +35,7 @@ public function imageBaseHydrator(\StdClass $node): Image $image->setDate(\DateTime::createFromFormat('U', (string) $node->taken_at)); $image->setLikes($node->like_count); $image->setIsLiked($node->has_liked); - $image->setComments($node->comment_count); + $image->setHeight($node->original_height); $image->setWidth($node->original_width); @@ -43,9 +43,15 @@ public function imageBaseHydrator(\StdClass $node): Image return $node; }, $node->image_versions2->candidates)); + if (property_exists($node, 'comment_count')) { + $image->setComments($node->comment_count); + } + if (property_exists($node, 'caption')) { - $image->setCaption($node->caption->text); - $image->setHashtags(InstagramHelper::buildHashtags($node->caption->text)); + if (!empty($node->caption)) { + $image->setCaption($node->caption->text); + $image->setHashtags(InstagramHelper::buildHashtags($node->caption->text)); + } } if (property_exists($node, 'accessibility_caption')) { diff --git a/src/Instagram/Hydrator/ReelsHydrator.php b/src/Instagram/Hydrator/ReelsHydrator.php index 508abfd..48ee7b5 100644 --- a/src/Instagram/Hydrator/ReelsHydrator.php +++ b/src/Instagram/Hydrator/ReelsHydrator.php @@ -35,7 +35,6 @@ public function reelsBaseHydrator(\StdClass $node): Reels $reels->setDate(\DateTime::createFromFormat('U', (string) $node->taken_at)); $reels->setLikes($node->like_count); $reels->setIsLiked($node->has_liked); - $reels->setComments($node->comment_count); $reels->setViews($node->view_count); $reels->setPlays($node->play_count); $reels->setDuration($node->video_duration); @@ -51,6 +50,10 @@ public function reelsBaseHydrator(\StdClass $node): Reels return $node; }, $node->video_versions)); + if (property_exists($node, 'comment_count')) { + $reels->setComments($node->comment_count); + } + if (property_exists($node, 'caption')) { if (!empty($node->caption)) { $reels->setCaption($node->caption->text); diff --git a/src/Instagram/Hydrator/TimelineFeedHydrator.php b/src/Instagram/Hydrator/TimelineFeedHydrator.php index 2010c63..552efa2 100644 --- a/src/Instagram/Hydrator/TimelineFeedHydrator.php +++ b/src/Instagram/Hydrator/TimelineFeedHydrator.php @@ -48,9 +48,31 @@ public function hydrateTimelineFeed(\StdClass $feed): void } foreach ($feed->feed_items as $feedItem) { - $timeline = $this->timelineHydrator->timelineBaseHydrator($feedItem->media_or_ad); - $this->timelineFeed->addTimeline($timeline); + if (property_exists($feedItem, 'media_or_ad')) { + // Timeline Feed + if (!property_exists($feedItem->media_or_ad, 'label')) { + $timeline = $this->timelineHydrator->timelineBaseHydrator($feedItem->media_or_ad); + $this->timelineFeed->addTimeline($timeline); + } + + // Sponsored Feed + if (!property_exists($feedItem, 'label')) { + } + } + + // Suggested User Feed + if (property_exists($feedItem, 'suggested_users')) { + } } + + // get additionals info + $additionalInfo = []; + $additionalInfo['pullToRefresh'] = $feed->pull_to_refresh_window_ms; + $additionalInfo['preloadDistance'] = $feed->preload_distance; + $additionalInfo['lastHeadLoad'] = \DateTime::createFromFormat('U', (string) $feed->last_head_load_ms); + $additionalInfo['hideLikeAndViewCounts'] = boolval($feed->hide_like_and_view_counts); + $additionalInfo['clientFeedChangelistApplied'] = $feed->client_feed_changelist_applied; + $this->timelineFeed->setAdditionalInfo((object) $additionalInfo); } /** diff --git a/src/Instagram/Model/TimelineFeed.php b/src/Instagram/Model/TimelineFeed.php index e8cf3f0..27dfeb5 100644 --- a/src/Instagram/Model/TimelineFeed.php +++ b/src/Instagram/Model/TimelineFeed.php @@ -26,6 +26,11 @@ class TimelineFeed */ private $newFeedPostExist = false; + /** + * @var \stdClass + */ + private $additionalInfo; + /** * @return array */ @@ -89,4 +94,20 @@ public function setNewFeedPostExist(bool $newFeedPostExist): void { $this->newFeedPostExist = $newFeedPostExist; } + + /** + * @return \stdClass + */ + public function getAdditionalInfo(): stdClass + { + return $this->additionalInfo; + } + + /** + * @param \stdClass $additionalInfo + */ + public function setAdditionalInfo(\stdClass $additionalInfo): void + { + $this->additionalInfo = $additionalInfo; + } } diff --git a/src/Instagram/Transport/TimelineDataFeed.php b/src/Instagram/Transport/TimelineDataFeed.php index 9411d17..0b742e9 100644 --- a/src/Instagram/Transport/TimelineDataFeed.php +++ b/src/Instagram/Transport/TimelineDataFeed.php @@ -41,13 +41,8 @@ public function fetchDataNewFeedPostExist(): array $endpoint = 'https://i.instagram.com/api/v1/feed/new_feed_posts_exist/'; $csrfToken = ''; - - /** @var SetCookie $cookie */ - foreach ($this->session->getCookies() as $cookie) { - if ($cookie->getName() === 'csrftoken') { - $csrfToken = $cookie->getValue(); - break; - } + if (!empty($this->session->getCookies()->getCookieByName("csrftoken"))) { + $csrfToken = $this->session->getCookies()->getCookieByName("csrftoken")->getValue(); } $options = [ @@ -89,13 +84,8 @@ public function fetchDataTimelineFeed(string $maxId = null): array $endpoint = Endpoints::TIMELINE_URL; $csrfToken = ''; - - /** @var SetCookie $cookie */ - foreach ($this->session->getCookies() as $cookie) { - if ($cookie->getName() === 'csrftoken') { - $csrfToken = $cookie->getValue(); - break; - } + if (!empty($this->session->getCookies()->getCookieByName("csrftoken"))) { + $csrfToken = $this->session->getCookies()->getCookieByName("csrftoken")->getValue(); } $options = [