get_followe{e,r}s yield Profiles rather than dicts

This commit is contained in:
Alexander Graf 2018-04-17 15:11:50 +02:00
parent 9d8175354b
commit 066c3de113
3 changed files with 17 additions and 21 deletions

View File

@ -109,7 +109,7 @@ def _main(instaloader: Instaloader, targetlist: List[str],
instaloader.context.log("Retrieving followees of %s..." % target[1:]) instaloader.context.log("Retrieving followees of %s..." % target[1:])
profile = Profile.from_username(instaloader.context, target[1:]) profile = Profile.from_username(instaloader.context, target[1:])
followees = profile.get_followees() followees = profile.get_followees()
profiles.update([followee['username'] for followee in followees]) profiles.update([followee.username for followee in followees])
elif target[0] == '#': elif target[0] == '#':
instaloader.download_hashtag(hashtag=target[1:], max_count=max_count, fast_update=fast_update, instaloader.download_hashtag(hashtag=target[1:], max_count=max_count, fast_update=fast_update,
filter_func=filter_func) filter_func=filter_func)

View File

@ -477,41 +477,37 @@ class Profile:
self._rhx_gis, self._rhx_gis,
self._metadata('edge_saved_media'))) self._metadata('edge_saved_media')))
def get_followers(self) -> Iterator[Dict[str, Any]]: def get_followers(self) -> Iterator['Profile']:
""" """
Retrieve list of followers of given profile. Retrieve list of followers of given profile.
To use this, one needs to be logged in and private profiles has to be followed, To use this, one needs to be logged in and private profiles has to be followed,
otherwise this returns an empty list. otherwise this returns an empty list.
:param profile: Name of profile to lookup followers.
""" """
if not self._context.is_logged_in: if not self._context.is_logged_in:
raise LoginRequiredException("--login required to get a profile's followers.") raise LoginRequiredException("--login required to get a profile's followers.")
self._obtain_metadata() self._obtain_metadata()
yield from self._context.graphql_node_list("37479f2b8209594dde7facb0d904896a", yield from (Profile(self._context, node) for node in
{'id': str(self.userid)}, self._context.graphql_node_list("37479f2b8209594dde7facb0d904896a",
'https://www.instagram.com/' + self.username + '/', {'id': str(self.userid)},
lambda d: d['data']['user']['edge_followed_by'], 'https://www.instagram.com/' + self.username + '/',
self._rhx_gis) lambda d: d['data']['user']['edge_followed_by'],
self._rhx_gis))
def get_followees(self) -> Iterator[Dict[str, Any]]: def get_followees(self) -> Iterator['Profile']:
""" """
Retrieve list of followees (followings) of given profile. Retrieve list of followees (followings) of given profile.
To use this, one needs to be logged in and private profiles has to be followed, To use this, one needs to be logged in and private profiles has to be followed,
otherwise this returns an empty list. otherwise this returns an empty list.
:param profile: Name of profile to lookup followers.
""" """
if not self._context.is_logged_in: if not self._context.is_logged_in:
raise LoginRequiredException("--login required to get a profile's followees.") raise LoginRequiredException("--login required to get a profile's followees.")
self._obtain_metadata() self._obtain_metadata()
yield from self._context.graphql_node_list("58712303d941c6855d4e888c5f0cd22f", yield from (Profile(self._context, node) for node in
{'id': str(self.userid)}, self._context.graphql_node_list("58712303d941c6855d4e888c5f0cd22f",
'https://www.instagram.com/' + self.username + '/', {'id': str(self.userid)},
lambda d: d['data']['user']['edge_follow'], 'https://www.instagram.com/' + self.username + '/',
self._rhx_gis) lambda d: d['data']['user']['edge_follow'],
self._rhx_gis))
class StoryItem: class StoryItem:

View File

@ -111,12 +111,12 @@ class TestInstaloaderLoggedIn(TestInstaloaderAnonymously):
def test_get_followees(self): def test_get_followees(self):
profile = instaloader.Profile.from_username(self.L.context, OWN_USERNAME) profile = instaloader.Profile.from_username(self.L.context, OWN_USERNAME)
for f in profile.get_followees(): for f in profile.get_followees():
print(f['username']) print(f.username)
def test_get_followers(self): def test_get_followers(self):
profile = instaloader.Profile.from_username(self.L.context, OWN_USERNAME) profile = instaloader.Profile.from_username(self.L.context, OWN_USERNAME)
for f in profile.get_followers(): for f in profile.get_followers():
print(f['username']) print(f.username)
def test_get_username_by_id(self): def test_get_username_by_id(self):
self.assertEqual(PUBLIC_PROFILE.lower(), self.assertEqual(PUBLIC_PROFILE.lower(),