Skip to content

Commit

Permalink
add Tagged Users on photo , little cleanup code (#894)
Browse files Browse the repository at this point in the history
Co-authored-by: Slawomir Chojnicki <[email protected]>
  • Loading branch information
binar123 and INKsearch authored Apr 12, 2021
1 parent 656f413 commit c6b13dd
Showing 1 changed file with 78 additions and 41 deletions.
119 changes: 78 additions & 41 deletions src/InstagramScraper/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ class Media extends AbstractModel
* @var string
*/
protected $locationAddressJson;
/**
* @var array
*/
protected $taggedUsers=[];
/**
* @var array
*/
protected $taggedUsersIds=[];

/**
* @param string $code
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -411,7 +435,7 @@ public function getLikesCount()
*/
public function getHasLiked()
{
return $this->hasLiked;
return $this->hasLiked;
}

/**
Expand All @@ -430,6 +454,14 @@ public function getLocationName()
return $this->locationName;
}

/**
* @param string
*/
public function setLocationName($name)
{
$this->locationName = $name;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -493,20 +525,31 @@ public function getLocationSlug()
{
return $this->locationSlug;
}

/**
* @param string
*/
public function setLocationSlug($slug)
{
$this->locationSlug = $slug;
}

/**
* @return string
*/
public function getAltText()
{
return $this->altText;
}

/**
* @return string
*/
public function getLocationAddressJson()
{
return $this->locationAddressJson;
}

/**
* @return mixed
*/
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -680,26 +710,21 @@ 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) {
$this->comments[] = Comment::create($commentData['node']);
}
}
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;
Expand All @@ -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;
}
Expand Down

0 comments on commit c6b13dd

Please sign in to comment.