New Profile.from_id() that works without posts
The old method needed the target profile to have at least one post. The new method works anonymously even for private profiles. Closes #249.
This commit is contained in:
parent
34416298a0
commit
9e04ef3436
@ -22,6 +22,10 @@ class ProfileNotExistsException(InstaloaderException):
|
|||||||
|
|
||||||
|
|
||||||
class ProfileHasNoPicsException(InstaloaderException):
|
class ProfileHasNoPicsException(InstaloaderException):
|
||||||
|
"""
|
||||||
|
.. deprecated:: 4.2.2
|
||||||
|
Not raised anymore.
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -396,28 +396,27 @@ class Profile:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def from_id(cls, context: InstaloaderContext, profile_id: int):
|
def from_id(cls, context: InstaloaderContext, profile_id: int):
|
||||||
"""Create a Profile instance from a given userid. If possible, use :meth:`Profile.from_username`
|
"""Create a Profile instance from a given userid. If possible, use :meth:`Profile.from_username`
|
||||||
or constructor directly rather than this method, since it does many requests.
|
or constructor directly rather than this method, since it requires more requests.
|
||||||
|
|
||||||
:param context: :attr:`Instaloader.context`
|
:param context: :attr:`Instaloader.context`
|
||||||
:param profile_id: userid
|
:param profile_id: userid
|
||||||
:raises: :class:`ProfileNotExistsException`, :class:`ProfileHasNoPicsException`
|
:raises: :class:`ProfileNotExistsException`
|
||||||
"""
|
"""
|
||||||
if profile_id in context.profile_id_cache:
|
if profile_id in context.profile_id_cache:
|
||||||
return context.profile_id_cache[profile_id]
|
return context.profile_id_cache[profile_id]
|
||||||
data = context.graphql_query("472f257a40c653c64c666ce877d59d2b",
|
data = context.graphql_query('7c16654f22c819fb63d1183034a5162f',
|
||||||
{'id': str(profile_id), 'first': 1},
|
{'user_id': str(profile_id),
|
||||||
|
'include_chaining': False,
|
||||||
|
'include_reel': True,
|
||||||
|
'include_suggested_users': False,
|
||||||
|
'include_logged_out_extras': False,
|
||||||
|
'include_highlight_reels': False},
|
||||||
rhx_gis=context.root_rhx_gis)['data']['user']
|
rhx_gis=context.root_rhx_gis)['data']['user']
|
||||||
if data:
|
if data:
|
||||||
data = data["edge_owner_to_timeline_media"]
|
profile = cls(context, data['reel']['owner'])
|
||||||
else:
|
else:
|
||||||
raise ProfileNotExistsException("No profile found, the user may have blocked you (ID: " +
|
raise ProfileNotExistsException("No profile found, the user may have blocked you (ID: " +
|
||||||
str(profile_id) + ").")
|
str(profile_id) + ").")
|
||||||
if not data['edges']:
|
|
||||||
if data['count'] == 0:
|
|
||||||
raise ProfileHasNoPicsException("Profile with ID {0}: no pics found.".format(str(profile_id)))
|
|
||||||
else:
|
|
||||||
raise LoginRequiredException("Login required to determine username (ID: " + str(profile_id) + ").")
|
|
||||||
profile = Post(context, data['edges'][0]['node']).owner_profile
|
|
||||||
context.profile_id_cache[profile_id] = profile
|
context.profile_id_cache[profile_id] = profile
|
||||||
return profile
|
return profile
|
||||||
|
|
||||||
|
@ -17,6 +17,9 @@ OWN_USERNAME = "aandergr"
|
|||||||
NORMAL_MAX_COUNT = 2
|
NORMAL_MAX_COUNT = 2
|
||||||
PAGING_MAX_COUNT = 15
|
PAGING_MAX_COUNT = 15
|
||||||
PRIVATE_PROFILE = "aandergr"
|
PRIVATE_PROFILE = "aandergr"
|
||||||
|
PRIVATE_PROFILE_ID = 1706625676
|
||||||
|
EMPTY_PROFILE = "not_public"
|
||||||
|
EMPTY_PROFILE_ID = 1928659031
|
||||||
|
|
||||||
# Preserve query timestamps (rate control) between tests to not get rate limited
|
# Preserve query timestamps (rate control) between tests to not get rate limited
|
||||||
instaloadercontext_query_timestamps = list()
|
instaloadercontext_query_timestamps = list()
|
||||||
@ -83,10 +86,18 @@ class TestInstaloaderAnonymously(unittest.TestCase):
|
|||||||
self.assertEqual(PUBLIC_PROFILE_ID,
|
self.assertEqual(PUBLIC_PROFILE_ID,
|
||||||
instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).userid)
|
instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).userid)
|
||||||
|
|
||||||
def test_get_username_by_id(self):
|
def test_get_username_by_id_private(self):
|
||||||
|
self.assertEqual(PRIVATE_PROFILE.lower(),
|
||||||
|
instaloader.Profile.from_id(self.L.context, PRIVATE_PROFILE_ID).username)
|
||||||
|
|
||||||
|
def test_get_username_by_id_public(self):
|
||||||
self.assertEqual(PUBLIC_PROFILE.lower(),
|
self.assertEqual(PUBLIC_PROFILE.lower(),
|
||||||
instaloader.Profile.from_id(self.L.context, PUBLIC_PROFILE_ID).username)
|
instaloader.Profile.from_id(self.L.context, PUBLIC_PROFILE_ID).username)
|
||||||
|
|
||||||
|
def test_get_username_by_id_empty(self):
|
||||||
|
self.assertEqual(EMPTY_PROFILE.lower(),
|
||||||
|
instaloader.Profile.from_id(self.L.context, EMPTY_PROFILE_ID).username)
|
||||||
|
|
||||||
def test_post_from_mediaid(self):
|
def test_post_from_mediaid(self):
|
||||||
for post in instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).get_posts():
|
for post in instaloader.Profile.from_username(self.L.context, PUBLIC_PROFILE).get_posts():
|
||||||
post2 = instaloader.Post.from_mediaid(self.L.context, post.mediaid)
|
post2 = instaloader.Post.from_mediaid(self.L.context, post.mediaid)
|
||||||
@ -169,10 +180,6 @@ class TestInstaloaderLoggedIn(TestInstaloaderAnonymously):
|
|||||||
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):
|
|
||||||
self.assertEqual(PUBLIC_PROFILE.lower(),
|
|
||||||
instaloader.Profile.from_id(self.L.context, PUBLIC_PROFILE_ID).username)
|
|
||||||
|
|
||||||
def test_get_likes(self):
|
def test_get_likes(self):
|
||||||
for post in instaloader.Profile.from_username(self.L.context, OWN_USERNAME).get_posts():
|
for post in instaloader.Profile.from_username(self.L.context, OWN_USERNAME).get_posts():
|
||||||
for like in post.get_likes():
|
for like in post.get_likes():
|
||||||
|
Loading…
Reference in New Issue
Block a user