diff --git a/instagrapi/media.py b/instagrapi/media.py index ae7a9b45..9ed43585 100644 --- a/instagrapi/media.py +++ b/instagrapi/media.py @@ -1,4 +1,5 @@ import json +import random from typing import List from copy import deepcopy from urllib.parse import urlparse @@ -238,3 +239,41 @@ def media_comment(self, media_id: str, text: str) -> Comment: ), ) return extract_comment(result["comment"]) + + def media_like(self, media_id: str) -> bool: + """Like media + """ + assert self.user_id, "Login required" + media_id = self.media_id(media_id) + data = { + "inventory_source": "media_or_ad", + "media_id": media_id, + "radio_type": "wifi-none", + "is_carousel_bumped_post": "false", + "container_module": "feed_timeline", + "feed_position": str(random.randint(0, 6)) + } + result = self.private_request( + f"media/{media_id}/like/", + self.with_action_data(data) + ) + return result['status'] == 'ok' + + def media_unlike(self, media_id: str) -> bool: + """Unlike media + """ + assert self.user_id, "Login required" + media_id = self.media_id(media_id) + data = { + "inventory_source": "media_or_ad", + "media_id": media_id, + "radio_type": "wifi-none", + "is_carousel_bumped_post": "false", + "container_module": "feed_timeline", + "feed_position": str(random.randint(0, 6)) + } + result = self.private_request( + f"media/{media_id}/unlike/", + self.with_action_data(data) + ) + return result['status'] == 'ok' diff --git a/setup.py b/setup.py index 235be966..b60280d6 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ setup( name='instagrapi', - version='1.2.7', + version='1.2.8', author='Mikhail Andreev', author_email='x11org@gmail.com', license='MIT', diff --git a/tests.py b/tests.py index e53bad97..8bccf19f 100644 --- a/tests.py +++ b/tests.py @@ -390,6 +390,31 @@ def test_media_comment(self): for field in ["pk", "username", "full_name", "profile_pic_url"]: self.assertIn(field, user_fields) + def test_media_like_by_pk(self): + media_pk = self.api.media_pk_from_url( + "https://www.instagram.com/p/ByU3LAslgWY/" + ) + self.assertTrue( + self.api.media_like(media_pk) + ) + + def test_media_like_and_unlike(self): + media_pk = self.api.media_pk_from_url( + "https://www.instagram.com/p/B3mr1-OlWMG/" + ) + self.assertTrue(self.api.media_unlike(media_pk)) + media = self.api.media_info_v1(media_pk) + like_count = int(media.like_count) + # like + self.assertTrue(self.api.media_like(media.id)) + media = self.api.media_info_v1(media_pk) # refresh after like + new_like_count = int(media.like_count) + self.assertEqual(new_like_count, like_count + 1) + # unlike + self.assertTrue(self.api.media_unlike(media.id)) + media = self.api.media_info_v1(media_pk) # refresh after unlike + self.assertEqual(media.like_count, like_count) + class ClientCompareExtractTestCase(ClientPrivateTestCase): def assertLocation(self, v1, gql):