From c6b13dd9a960b67235924667a04f092f1bbe4934 Mon Sep 17 00:00:00 2001 From: binar123 Date: Mon, 12 Apr 2021 19:18:54 +0200 Subject: [PATCH] add Tagged Users on photo , little cleanup code (#894) Co-authored-by: Slawomir Chojnicki --- src/InstagramScraper/Model/Media.php | 119 ++++++++++++++++++--------- 1 file changed, 78 insertions(+), 41 deletions(-) diff --git a/src/InstagramScraper/Model/Media.php b/src/InstagramScraper/Model/Media.php index 71499d26..89abfec4 100644 --- a/src/InstagramScraper/Model/Media.php +++ b/src/InstagramScraper/Model/Media.php @@ -189,6 +189,14 @@ class Media extends AbstractModel * @var string */ protected $locationAddressJson; + /** + * @var array + */ + protected $taggedUsers=[]; + /** + * @var array + */ + protected $taggedUsersIds=[]; /** * @param string $code @@ -232,10 +240,26 @@ public static function getCodeFromId($id) $remainder = $id % 64; $id = ($id - $remainder) / 64; $code = $alphabet[$remainder] . $code; - }; + } return $code; } + /** + * @return array + */ + public function getTaggedUsers(): array + { + return $this->taggedUsers; + } + + /** + * @return array + */ + public function getTaggedUsersIds(): array + { + return $this->taggedUsersIds; + } + /** * @return mixed */ @@ -411,7 +435,7 @@ public function getLikesCount() */ public function getHasLiked() { - return $this->hasLiked; + return $this->hasLiked; } /** @@ -430,6 +454,14 @@ public function getLocationName() return $this->locationName; } + /** + * @param string + */ + public function setLocationName($name) + { + $this->locationName = $name; + } + /** * @return bool */ @@ -493,6 +525,15 @@ public function getLocationSlug() { return $this->locationSlug; } + + /** + * @param string + */ + public function setLocationSlug($slug) + { + $this->locationSlug = $slug; + } + /** * @return string */ @@ -500,6 +541,7 @@ public function getAltText() { return $this->altText; } + /** * @return string */ @@ -507,6 +549,7 @@ public function getLocationAddressJson() { return $this->locationAddressJson; } + /** * @return mixed */ @@ -515,21 +558,6 @@ public function getLocationAddress() return json_decode($this->locationAddressJson); } - /** - * @param string - */ - public function setLocationName($name) - { - $this->locationName = $name; - } - /** - * @param string - */ - public function setLocationSlug($slug) - { - $this->locationSlug = $slug; - } - /** * @param $value * @param $prop @@ -543,10 +571,12 @@ protected function initPropertiesCustom($value, $prop, $arr) case 'type': $this->type = $value; break; + case 'date': case 'created_time': $this->createdTime = (int)$value; break; case 'code': + case 'shortcode': $this->shortCode = $value; $this->link = Endpoints::getMediaPageLink($this->shortCode); break; @@ -559,6 +589,8 @@ protected function initPropertiesCustom($value, $prop, $arr) case 'comments': $this->commentsCount = $arr[$prop]['count']; break; + case 'edge_liked_by': + case 'edge_media_preview_like': case 'likes': $this->likesCount = $arr[$prop]['count']; break; @@ -633,13 +665,14 @@ protected function initPropertiesCustom($value, $prop, $arr) } break; case 'location': - if(isset($arr[$prop])) { + 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; } break; + case 'owner': case 'user': $this->owner = Account::create($arr[$prop]); break; @@ -663,13 +696,10 @@ protected function initPropertiesCustom($value, $prop, $arr) case 'taken_at_timestamp': $this->createdTime = $value; break; - case 'shortcode': - $this->shortCode = $value; - $this->link = Endpoints::getMediaPageLink($this->shortCode); - break; + case 'edge_media_preview_comment': if (isset($arr[$prop]['count'])) { - $this->commentsCount = (int) $arr[$prop]['count']; + $this->commentsCount = (int)$arr[$prop]['count']; } if (isset($arr[$prop]['edges']) && is_array($arr[$prop]['edges'])) { foreach ($arr[$prop]['edges'] as $commentData) { @@ -680,7 +710,7 @@ protected function initPropertiesCustom($value, $prop, $arr) case 'edge_media_to_comment': case 'edge_media_to_parent_comment': if (isset($arr[$prop]['count'])) { - $this->commentsCount = (int) $arr[$prop]['count']; + $this->commentsCount = (int)$arr[$prop]['count']; } if (isset($arr[$prop]['edges']) && is_array($arr[$prop]['edges'])) { foreach ($arr[$prop]['edges'] as $commentData) { @@ -688,18 +718,13 @@ protected function initPropertiesCustom($value, $prop, $arr) } } if (isset($arr[$prop]['page_info']['has_next_page'])) { - $this->hasMoreComments = (bool) $arr[$prop]['page_info']['has_next_page']; + $this->hasMoreComments = (bool)$arr[$prop]['page_info']['has_next_page']; } if (isset($arr[$prop]['page_info']['end_cursor'])) { - $this->commentsNextPage = (string) $arr[$prop]['page_info']['end_cursor']; + $this->commentsNextPage = (string)$arr[$prop]['page_info']['end_cursor']; } break; - case 'edge_media_preview_like': - $this->likesCount = $arr[$prop]['count']; - break; - case 'edge_liked_by': - $this->likesCount = $arr[$prop]['count']; - break; + case 'viewer_has_liked': $this->hasLiked = $arr[$prop]; break; @@ -725,19 +750,31 @@ protected function initPropertiesCustom($value, $prop, $arr) $this->sidecarMedias[] = static::create($edge['node']); } break; - case 'owner': - $this->owner = Account::create($arr[$prop]); - break; - case 'date': - $this->createdTime = (int)$value; + case 'edge_media_to_tagged_user': + if (!is_array($arr[$prop]['edges'])) { + break; + } + foreach ($arr[$prop]['edges'] as $edge) { + if (!isset($edge['node'])) { + continue; + } + + $this->taggedUsers[] = $edge['node']['user'] ?? []; + $this->taggedUsersIds[] = $edge['node']['user']['id'] ?? ''; + } + break; case '__typename': if ($value == 'GraphImage' || $value == 'GraphStoryImage') { $this->type = static::TYPE_IMAGE; - } else if ($value == 'GraphVideo' || $value == 'GraphStoryVideo') { - $this->type = static::TYPE_VIDEO; - } else if ($value == 'GraphSidecar') { - $this->type = static::TYPE_SIDECAR; + } else { + if ($value == 'GraphVideo' || $value == 'GraphStoryVideo') { + $this->type = static::TYPE_VIDEO; + } else { + if ($value == 'GraphSidecar') { + $this->type = static::TYPE_SIDECAR; + } + } } break; }