Skip to content

Commit

Permalink
Fix timeline media bug (#329)
Browse files Browse the repository at this point in the history
* Fix timeline media bug

* Fix timeline media bug
  • Loading branch information
nsmle authored Aug 19, 2022
1 parent 7e9e250 commit 8ae164b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 44 deletions.
55 changes: 31 additions & 24 deletions src/Instagram/Hydrator/CarouselHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand Down
12 changes: 9 additions & 3 deletions src/Instagram/Hydrator/ImageHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@ 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);

$image->setImage(array_map(function ($node) {
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')) {
Expand Down
5 changes: 4 additions & 1 deletion src/Instagram/Hydrator/ReelsHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
26 changes: 24 additions & 2 deletions src/Instagram/Hydrator/TimelineFeedHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Instagram/Model/TimelineFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class TimelineFeed
*/
private $newFeedPostExist = false;

/**
* @var \stdClass
*/
private $additionalInfo;

/**
* @return array
*/
Expand Down Expand Up @@ -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;
}
}
18 changes: 4 additions & 14 deletions src/Instagram/Transport/TimelineDataFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit 8ae164b

Please sign in to comment.