Let Post.get_likes() yield Profile instances

This commit is contained in:
Alexander Graf 2018-04-28 17:32:13 +02:00
parent 4dcb23cfe5
commit 0e40da1c70
2 changed files with 9 additions and 12 deletions

View File

@ -293,24 +293,21 @@ class Post:
lambda d: d['data']['shortcode_media']['edge_media_to_comment'], lambda d: d['data']['shortcode_media']['edge_media_to_comment'],
self._rhx_gis) self._rhx_gis)
def get_likes(self) -> Iterator[Dict[str, Any]]: def get_likes(self) -> Iterator['Profile']:
"""Iterate over all likes of the post. """Iterate over all likes of the post. A :class:`Profile` instance of each likee is yielded."""
Each like is represented by a dictionary having the keys username, followed_by_viewer, id, is_verified,
requested_by_viewer, followed_by_viewer, profile_pic_url.
"""
if self.likes == 0: if self.likes == 0:
# Avoid doing additional requests if there are no comments # Avoid doing additional requests if there are no comments
return return
likes_edges = self._field('edge_media_preview_like', 'edges') likes_edges = self._field('edge_media_preview_like', 'edges')
if self.likes == len(likes_edges): if self.likes == len(likes_edges):
# If the Post's metadata already contains all likes, don't do GraphQL requests to obtain them # If the Post's metadata already contains all likes, don't do GraphQL requests to obtain them
yield from (like['node'] for like in likes_edges) yield from (Profile(self._context, like['node']) for like in likes_edges)
return return
yield from self._context.graphql_node_list("1cb6ec562846122743b61e492c85999f", {'shortcode': self.shortcode}, yield from (Profile(self._context, node) for node in
'https://www.instagram.com/p/' + self.shortcode + '/', self._context.graphql_node_list("1cb6ec562846122743b61e492c85999f", {'shortcode': self.shortcode},
lambda d: d['data']['shortcode_media']['edge_liked_by'], 'https://www.instagram.com/p/' + self.shortcode + '/',
self._rhx_gis) lambda d: d['data']['shortcode_media']['edge_liked_by'],
self._rhx_gis))
@property @property
def location(self) -> Optional[PostLocation]: def location(self) -> Optional[PostLocation]:

View File

@ -125,7 +125,7 @@ class TestInstaloaderLoggedIn(TestInstaloaderAnonymously):
def test_get_likes(self): def test_get_likes(self):
for post in instaloader.Profile.from_username(self.L.context, OWN_USERNAME).get_posts(): for post in instaloader.Profile.from_username(self.L.context, OWN_USERNAME).get_posts():
for like in post.get_likes(): for like in post.get_likes():
print(like['username']) print(like.username)
break break
def test_explore_paging(self): def test_explore_paging(self):