Trivial improvement to Profile.from_id()

This commit is contained in:
Alexander Graf 2018-05-01 19:52:39 +02:00
parent 084cfb102c
commit b5ad92e236

View File

@ -139,20 +139,17 @@ class Post:
@property @property
def owner_profile(self) -> 'Profile': def owner_profile(self) -> 'Profile':
""":class:`Profile` instance of the Post's owner."""
if not self._owner_profile: if not self._owner_profile:
if 'username' in self._node['owner']: if 'username' in self._node['owner']:
owner_struct = self._node['owner'] owner_struct = self._node['owner']
else: else:
# Sometimes, the 'owner' structure does not contain the username, only the user's ID. In that case, # Sometimes, the 'owner' structure does not contain the username, only the user's ID. In that case,
# this call triggers downloading of the complete Post metadata struct, where the owner username # this call triggers downloading of the complete Post metadata struct, where the owner username
# is contained. This is better than to get the username by user ID, since it # is contained.
# gives us other information that is more likely to be usable. # Note that we cannot use Profile.from_id() here since that would lead us into a recursion.
owner_struct = self._full_metadata['owner'] owner_struct = self._full_metadata['owner']
if 'username' in owner_struct: self._owner_profile = Profile(self._context, owner_struct)
self._owner_profile = Profile(self._context, owner_struct)
else:
# Fallback, if we still did not get the owner username
self._owner_profile = Profile.from_id(self._context, owner_struct['id'])
return self._owner_profile return self._owner_profile
@property @property
@ -399,8 +396,7 @@ class Profile:
raise ProfileHasNoPicsException("Profile with ID {0}: no pics found.".format(str(profile_id))) raise ProfileHasNoPicsException("Profile with ID {0}: no pics found.".format(str(profile_id)))
else: else:
raise LoginRequiredException("Login required to determine username (ID: " + str(profile_id) + ").") raise LoginRequiredException("Login required to determine username (ID: " + str(profile_id) + ").")
username = Post.from_shortcode(context, data['edges'][0]["node"]["shortcode"]).owner_username return Post(context, data['edges'][0]['node']).owner_profile
return cls(context, {'username': username.lower(), 'id': profile_id})
def _asdict(self): def _asdict(self):
json_node = self._node.copy() json_node = self._node.copy()