Skip to content

Commit

Permalink
Merge pull request #490 from valga/threaded_comments
Browse files Browse the repository at this point in the history
Media: Load threaded comments
  • Loading branch information
raiym authored May 21, 2019
2 parents 2599b88 + fa34096 commit a3a5bba
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
68 changes: 68 additions & 0 deletions src/InstagramScraper/Model/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ class Comment extends AbstractModel
*/
protected $owner;

/**
* @var static[]
*/
protected $childComments = [];

/**
* @var int
*/
protected $childCommentsCount = 0;

/**
* @var bool
*/
protected $hasMoreChildComments = false;

/**
* @var string
*/
protected $childCommentsNextPage = '';

/**
* @var bool
*/
Expand Down Expand Up @@ -62,6 +82,38 @@ public function getOwner()
return $this->owner;
}

/**
* @return static[]
*/
public function getChildComments()
{
return $this->childComments;
}

/**
* @return int
*/
public function getChildCommentsCount()
{
return $this->childCommentsCount;
}

/**
* @return bool
*/
public function hasMoreChildComments()
{
return $this->hasMoreChildComments;
}

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

/**
* @param $value
* @param $prop
Expand All @@ -81,6 +133,22 @@ protected function initPropertiesCustom($value, $prop)
case 'owner':
$this->owner = Account::create($value);
break;
case 'edge_threaded_comments':
if (isset($value['count'])) {
$this->childCommentsCount = (int) $value['count'];
}
if (isset($value['edges']) && is_array($value['edges'])) {
foreach ($value['edges'] as $commentData) {
$this->childComments[] = static::create($commentData['node']);
}
}
if (isset($value['page_info']['has_next_page'])) {
$this->hasMoreChildComments = (bool) $value['page_info']['has_next_page'];
}
if (isset($value['page_info']['end_cursor'])) {
$this->childCommentsNextPage = (string) $value['page_info']['end_cursor'];
}
break;
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/InstagramScraper/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ class Media extends AbstractModel
*/
protected $comments = [];

/**
* @var Comment[]
*/
protected $previewComments = [];

/**
* @var bool
*/
Expand Down Expand Up @@ -400,6 +405,14 @@ public function getComments()
return $this->comments;
}

/**
* @return Comment[]
*/
public function getPreviewComments()
{
return $this->previewComments;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -455,7 +468,7 @@ protected function initPropertiesCustom($value, $prop, $arr)
case 'link':
$this->link = $value;
break;
case 'edge_media_to_comment':
case 'comments':
$this->commentsCount = $arr[$prop]['count'];
break;
case 'likes':
Expand Down Expand Up @@ -557,7 +570,18 @@ protected function initPropertiesCustom($value, $prop, $arr)
$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'];
}
if (isset($arr[$prop]['edges']) && is_array($arr[$prop]['edges'])) {
foreach ($arr[$prop]['edges'] as $commentData) {
$this->previewComments[] = Comment::create($commentData['node']);
}
}
break;
case 'edge_media_to_comment':
case 'edge_media_to_parent_comment':
if (isset($arr[$prop]['count'])) {
$this->commentsCount = (int) $arr[$prop]['count'];
}
Expand Down

0 comments on commit a3a5bba

Please sign in to comment.