Skip to content

Commit

Permalink
web: Update reels_feed, add media_likers
Browse files Browse the repository at this point in the history
  • Loading branch information
ping committed May 12, 2018
1 parent 18302d9 commit 78294a8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
46 changes: 45 additions & 1 deletion instagram_web_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,50 @@ def media_comments(self, short_code, **kwargs):
'edge_media_to_comment', {}).get('edges', [])]
return info

@login_required
def media_likers(self, short_code, **kwargs):
"""
Get media likers
:param short_code:
:param kwargs:
- **count**: Number of comments to return. Default: 24. Maximum: 50
- **end_cursor**: For pagination
- **extract**: bool. Return a simple list of comments
:return:
"""
end_cursor = kwargs.pop('end_cursor', None)
# request 24 by default for the first page
if end_cursor:
count = kwargs.pop('count', 12)
else:
count = kwargs.pop('count', 24)
if count > 50:
raise ValueError('count cannot be greater than 50')

variables = {
'shortcode': short_code,
'first': int(count)
}
if end_cursor:
variables['after'] = end_cursor
query = {
'query_hash': '1cb6ec562846122743b61e492c85999f',
'variables': json.dumps(variables, separators=(',', ':'))
}

info = self._make_request(self.GRAPHQL_API_URL, query=query)

if not info.get('data', {}).get('shortcode_media'):
# deleted media does not return 'comments' at all
# media without comments will return comments, with counts = 0, nodes = [], etc
raise ClientError('Not Found', 404)

if kwargs.pop('extract', True):
return [c['node'] for c in info.get('data', {}).get('shortcode_media', {}).get(
'edge_liked_by', {}).get('edges', [])]
return info

@login_required
def user_following(self, user_id, **kwargs):
"""
Expand Down Expand Up @@ -914,7 +958,7 @@ def reels_feed(self, reel_ids, **kwargs):
'precomposed_overlay': False,
}
query = {
'query_hash': '297c491471fff978fa2ab83c0673a618',
'query_hash': '45246d3fe16ccc6577e0bd297a5db1ab',
'variables': json.dumps(variables, separators=(',', ':'))
}
return self._make_request(self.GRAPHQL_API_URL, query=query)
3 changes: 1 addition & 2 deletions tests/web/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,4 @@ def test_reels_tray(self):

def test_reels_feed(self):
results = self.api.reels_feed(['25025320']).get('data', {})
self.assertGreater(
len(results.get('reels_media')), 0)
self.assertTrue('reels_media' in results)
27 changes: 26 additions & 1 deletion tests/web/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,19 @@ def init_all(api):
{
'name': 'test_post_comment_validation_mock',
'test': MediaTests('test_post_comment_validation_mock', api),
}
},
{
'name': 'test_media_likers',
'test': MediaTests('test_media_likers', api),
},
{
'name': 'test_notfound_media_likers',
'test': MediaTests('test_notfound_media_likers', api),
},
{
'name': 'test_media_likers_noextract',
'test': MediaTests('test_media_likers_noextract', api),
},
]

@unittest.skip('Deprecated.')
Expand Down Expand Up @@ -110,6 +122,19 @@ def test_media_comments_noextract(self):
results = self.api.media_comments(self.test_media_shortcode, count=20, extract=False)
self.assertIsInstance(results, dict)

def test_media_likers(self):
results = self.api.media_likers(self.test_media_shortcode, count=20)
self.assertGreaterEqual(len(results), 0)
self.assertIsInstance(results, list)
self.assertIsInstance(results[0], dict)

def test_notfound_media_likers(self):
self.assertRaises(ClientError, lambda: self.api.media_likers('BSgmaRDg-xX'))

def test_media_likers_noextract(self):
results = self.api.media_likers(self.test_media_shortcode, count=20, extract=False)
self.assertIsInstance(results, dict)

@unittest.skip('Modifies data.')
def test_post_comment(self):
results = self.api.post_comment(self.test_media_id, '<3')
Expand Down

0 comments on commit 78294a8

Please sign in to comment.