From d222b778c1d5965ed013100abbae9142499f5aa6 Mon Sep 17 00:00:00 2001 From: "Mikhail Andreev (adw0rd)" Date: Fri, 8 Jan 2021 02:09:33 +0300 Subject: [PATCH] Implement list of likes in Media object [#74] Added cl.media_likers -> List[UserShort] --- README.md | 41 ++++++++++++++++++--------------- instagrapi/mixins/collection.py | 2 +- instagrapi/mixins/media.py | 8 ++++++- setup.py | 2 +- tests.py | 10 +++++++- 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 138e292d..c4e9f914 100644 --- a/README.md +++ b/README.md @@ -160,20 +160,22 @@ Viewing and editing publications (medias) * `code` - Short code (slug for media), example `BjNLpA1AhXM` from `"https://www.instagram.com/p/BjNLpA1AhXM/"` * `url` - URL to media publication -| Method | Return | Description | -| -------------------------------------------------- | ------------------ | ------------------------------------------------------------- | -| media_id(media_pk: int) | str | Return media_id by media_pk (e.g. 2277033926878261772 -> 2277033926878261772_1903424587) | -| media_pk(media_id: str) | int | Return media_pk by media_id (e.g. 2277033926878261772_1903424587 -> 2277033926878261772) | -| media_pk_from_code(code: str) | int | Return media_pk | -| media_pk_from_url(url: str) | int | Return media_pk | -| user_medias(user_id: int, amount: int = 20) | List\[Media] | Get list of medias by user_id | -| media_info(media_pk: int) | Media | Return media info | -| media_delete(media_pk: int) | bool | Delete media | -| media_edit(media_pk: int, caption: str, title: str, usertags: List[Usertag], location: Location) | dict | Change caption for media | -| media_user(media_pk: int) | User | Get user info for media | -| media_oembed(url: str) | MediaOembed | Return short media info by media URL | -| media_like(media_id: str) | bool | Like media | -| media_unlike(media_id: str) | bool | Unlike media | +| Method | Return | Description | +| --------------------------------------------------------------- | ------------------ | -------------------------------------------- | +| media_id(media_pk: int) | str | Return media_id by media_pk (e.g. 2277033926878261772 -> 2277033926878261772_1903424587) +| media_pk(media_id: str) | int | Return media_pk by media_id (e.g. 2277033926878261772_1903424587 -> 2277033926878261772) +| media_pk_from_code(code: str) | int | Return media_pk +| media_pk_from_url(url: str) | int | Return media_pk +| user_medias(user_id: int, amount: int = 20) | List\[Media] | Get list of medias by user_id +| media_info(media_pk: int) | Media | Return media info +| media_delete(media_pk: int) | bool | Delete media +| media_edit(media_pk: int, caption: str, title: str, usertags: List[Usertag], location: Location) | dict | Change caption for media +| media_user(media_pk: int) | User | Get user info for media +| media_oembed(url: str) | MediaOembed | Return short media info by media URL +| media_like(media_id: str) | bool | Like media +| media_unlike(media_id: str) | bool | Unlike media +| media_seen(media_ids: List[str], skipped_media_ids: List[str]) | bool | Mark a story as seen +| media_likers(media_id: str) | List\[UserShort] | Return list of users who liked this post Example: @@ -255,11 +257,12 @@ Example: #### Story -| Method | Return | Description | -| -------------------------------------------------- | ----------- | ------------------------------------------------------------- | -| user_stories(user_id: int, amount: int = None) | List[Story] | Get list of stories by user_id | -| story_info(story_pk: int, use_cache: bool = True) | Story | Return story info | -| story_delete(story_pk: int) | bool | Delete story | +| Method | Return | Description +| --------------------------------------------------------------- | ----------- | ---------------------------------- | +| user_stories(user_id: int, amount: int = None) | List[Story] | Get list of stories by user_id +| story_info(story_pk: int, use_cache: bool = True) | Story | Return story info +| story_delete(story_pk: int) | bool | Delete story +| story_seen(story_pks: List[int], skipped_story_pks: List[int]) | bool | Mark a story as seen #### Comment diff --git a/instagrapi/mixins/collection.py b/instagrapi/mixins/collection.py index 8337d013..785d5fb7 100644 --- a/instagrapi/mixins/collection.py +++ b/instagrapi/mixins/collection.py @@ -24,7 +24,7 @@ def collections(self) -> List[Collection]: while True: try: result = self.private_request( - "/collections/list/", + "collections/list/", params={ "collection_types": '["ALL_MEDIA_AUTO_COLLECTION","PRODUCT_AUTO_COLLECTION","MEDIA"]', "max_id": next_max_id, diff --git a/instagrapi/mixins/media.py b/instagrapi/mixins/media.py index f853f892..9cad3fbd 100644 --- a/instagrapi/mixins/media.py +++ b/instagrapi/mixins/media.py @@ -9,7 +9,8 @@ from instagrapi.exceptions import (ClientError, ClientLoginRequired, ClientNotFoundError, MediaNotFound) from instagrapi.extractors import (extract_location, extract_media_gql, - extract_media_oembed, extract_media_v1) + extract_media_oembed, extract_media_v1, + extract_user_short) from instagrapi.types import Location, Media, UserShort, Usertag from instagrapi.utils import InstagramIdCodec, json_value @@ -565,3 +566,8 @@ def gen(media_ids): self.with_default_data(data) ) return result["status"] == "ok" + + def media_likers(self, media_id: str) -> List[UserShort]: + media_id = self.media_id(media_id) + result = self.private_request(f"media/{media_id}/likers/") + return [extract_user_short(u) for u in result['users']] diff --git a/setup.py b/setup.py index d05924bc..c15ac7b8 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ setup( name='instagrapi', - version='1.4.5', + version='1.4.6', author='Mikhail Andreev', author_email='x11org@gmail.com', license='MIT', diff --git a/tests.py b/tests.py index 37d5e1ad..bed9fd34 100644 --- a/tests.py +++ b/tests.py @@ -428,6 +428,13 @@ def test_media_like_and_unlike(self): media = self.api.media_info_v1(media_pk) # refresh after unlike self.assertEqual(media.like_count, like_count) + def test_media_likers(self): + media = self.api.user_medias(self.api.user_id, amount=3)[-1] + self.assertIsInstance(media, Media) + likers = self.api.media_likers(media.pk) + self.assertTrue(len(likers) > 0) + self.assertIsInstance(likers[0], UserShort) + class ClientCommentTestCase(ClientPrivateTestCase): @@ -1206,11 +1213,12 @@ def test_user_stories(self): def test_story_info(self): user_id = self.api.user_id_from_username("dhbastards") - stories = self.api.user_stories(user_id, 1) + stories = self.api.user_stories(user_id, amount=1) story = self.api.story_info(stories[0].pk) self.assertIsInstance(story, Story) story = self.api.story_info(stories[0].id) self.assertIsInstance(story, Story) + self.assertTrue(self.api.story_seen(story.pk)) if __name__ == '__main__':