Skip to content

Commit

Permalink
Like and Unlike features [#28] Added media_like and media_unlike
Browse files Browse the repository at this point in the history
  • Loading branch information
adw0rd committed Dec 7, 2020
1 parent 1882e39 commit 9ee50ba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
39 changes: 39 additions & 0 deletions instagrapi/media.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import random
from typing import List
from copy import deepcopy
from urllib.parse import urlparse
Expand Down Expand Up @@ -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'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

0 comments on commit 9ee50ba

Please sign in to comment.