From 749ff606398fdfd459d341ec7d51c28b3fa0398f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Koch-Kramer?= Date: Fri, 17 May 2019 21:38:39 +0200 Subject: [PATCH] Do not require rhx_gix in metadata Closes #297. --- instaloader/instaloader.py | 2 +- instaloader/instaloadercontext.py | 6 +++--- instaloader/structures.py | 11 ++++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/instaloader/instaloader.py b/instaloader/instaloader.py index 1c9554f..3a00d91 100644 --- a/instaloader/instaloader.py +++ b/instaloader/instaloader.py @@ -796,7 +796,7 @@ class Instaloader: for node in self.context.graphql_node_list("df0dcc250c2b18d9fd27c5581ef33c7c", {}, 'https://www.instagram.com/explore/', lambda d: d['data']['user']['edge_web_discover_media'], - data['rhx_gis'])) + data.get('rhx_gis'))) def get_hashtag_posts(self, hashtag: str) -> Iterator[Post]: """Get Posts associated with a #hashtag.""" diff --git a/instaloader/instaloadercontext.py b/instaloader/instaloadercontext.py index 0d9f069..dce9e13 100644 --- a/instaloader/instaloadercontext.py +++ b/instaloader/instaloadercontext.py @@ -530,6 +530,6 @@ class InstaloaderContext: # At the moment, rhx_gis seems to be required for anonymous requests only. By returning None when logged # in, we can save the root_rhx_gis lookup query. return None - if not self._root_rhx_gis: - self._root_rhx_gis = self.get_json('', {})['rhx_gis'] - return self._root_rhx_gis + if self._root_rhx_gis is None: + self._root_rhx_gis = self.get_json('', {}).get('rhx_gis', '') + return self._root_rhx_gis or None diff --git a/instaloader/structures.py b/instaloader/structures.py index 72867c1..92427bd 100644 --- a/instaloader/structures.py +++ b/instaloader/structures.py @@ -132,7 +132,7 @@ class Post: if not self._full_metadata_dict: pic_json = self._context.get_json("p/{0}/".format(self.shortcode), params={}) self._full_metadata_dict = pic_json['entry_data']['PostPage'][0]['graphql']['shortcode_media'] - self._rhx_gis_str = pic_json['rhx_gis'] + self._rhx_gis_str = pic_json.get('rhx_gis') if self.shortcode != self._full_metadata_dict['shortcode']: self._node.update(self._full_metadata_dict) raise PostChangedException @@ -144,9 +144,8 @@ class Post: return self._full_metadata_dict @property - def _rhx_gis(self) -> str: + def _rhx_gis(self) -> Optional[str]: self._obtain_metadata() - assert self._rhx_gis_str is not None return self._rhx_gis_str def _field(self, *keys) -> Any: @@ -454,6 +453,7 @@ class Profile: self._context = context self._has_public_story = None # type: Optional[bool] self._node = node + self._has_full_metadata = False self._rhx_gis = None self._iphone_struct_ = None if 'iphone_struct' in node: @@ -514,10 +514,11 @@ class Profile: def _obtain_metadata(self): try: - if not self._rhx_gis: + if not self._has_full_metadata: metadata = self._context.get_json('{}/'.format(self.username), params={}) self._node = metadata['entry_data']['ProfilePage'][0]['graphql']['user'] - self._rhx_gis = metadata['rhx_gis'] + self._has_full_metadata = True + self._rhx_gis = metadata.get('rhx_gis') except (QueryReturnedNotFoundException, KeyError) as err: raise ProfileNotExistsException('Profile {} does not exist.'.format(self.username)) from err