Get comment likes (#1998)

This commit is contained in:
ybenel
2023-07-17 08:09:20 +02:00
committed by GitHub
parent ebdc5f1557
commit 3ebfe40729
+57 -12
View File
@@ -43,19 +43,64 @@ PostCommentAnswer.owner.__doc__ = "Owner :class:`Profile` of the comment."
PostCommentAnswer.likes_count.__doc__ = "Number of likes on comment." PostCommentAnswer.likes_count.__doc__ = "Number of likes on comment."
class PostComment(NamedTuple): class PostComment:
id: int def __init__(self, context: 'InstaloaderContext', node: Dict[str, Any],
created_at_utc: datetime answers: Iterator['PostCommentAnswer'], post: 'Post'):
text: str self._context = context
owner: 'Profile' self._node = node
likes_count: int self._answers = answers
answers: Iterator[PostCommentAnswer] self._post = post
@property
def id(self) -> int:
""" ID number of comment. """
return self._node['id']
for field in PostCommentAnswer._fields: @property
getattr(PostComment, field).__doc__ = getattr(PostCommentAnswer, field).__doc__ # pylint: disable=no-member def created_at_utc(self) -> datetime:
PostComment.answers.__doc__ = r"Iterator which yields all :class:`PostCommentAnswer`\ s for the comment." """ :class:`~datetime.datetime` when comment was created (UTC). """
return datetime.utcfromtimestamp(self._node['created_at'])
@property
def text(self):
""" Comment text. """
return self._node['text']
@property
def owner(self):
""" Owner :class:`Profile` of the comment. """
return Profile(self._context, self._node['owner'])
@property
def likes_count(self):
""" Number of likes on comment. """
return self._node.get('edge_liked_by', {}).get('count', 0)
@property
def answers(self) -> Iterator['PostCommentAnswer']:
""" Iterator which yields all :class:`PostCommentAnswer` for the comment. """
return self._answers
@property
def likes(self) -> Iterable['Profile']:
"""
Iterate over all likes of a comment. A :class:`Profile` instance of each like is yielded.
.. versionadded:: 4.11
"""
if self.likes_count != 0:
return NodeIterator(
self._context,
'5f0b1f6281e72053cbc07909c8d154ae',
lambda d: d['data']['comment']['edge_liked_by'],
lambda n: Profile(self._context, n),
{'comment_id': self.id},
'https://www.instagram.com/p/{0}/'.format(self._post.shortcode),
)
return []
def __repr__(self):
return f'<PostComment {self.id} of {self._post.shortcode}>'
class PostLocation(NamedTuple): class PostLocation(NamedTuple):
id: int id: int
@@ -584,8 +629,8 @@ class Post:
) )
def _postcomment(node): def _postcomment(node):
return PostComment(*_postcommentanswer(node), return PostComment(context=self._context, node=node,
answers=_postcommentanswers(node)) answers=_postcommentanswers(node), post=self)
if self.comments == 0: if self.comments == 0:
# Avoid doing additional requests if there are no comments # Avoid doing additional requests if there are no comments
return [] return []