Skip to content

Commit

Permalink
Implement list of likes in Media object [#74] Added cl.media_likers -…
Browse files Browse the repository at this point in the history
…> List[UserShort]
  • Loading branch information
adw0rd committed Jan 7, 2021
1 parent 066f3ac commit d222b77
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion instagrapi/mixins/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion instagrapi/mixins/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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']]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

setup(
name='instagrapi',
version='1.4.5',
version='1.4.6',
author='Mikhail Andreev',
author_email='[email protected]',
license='MIT',
Expand Down
10 changes: 9 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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__':
Expand Down

0 comments on commit d222b77

Please sign in to comment.