From 51bfa01c2aaf1aa22b497cd4718ca8c62d659c83 Mon Sep 17 00:00:00 2001 From: Marc Seguin Date: Mon, 21 Oct 2019 13:47:14 -0400 Subject: [PATCH] Solving KeyError: 'edge_sidecar_to_children' (#411) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #380. Closes #394. Co-Authored-By: Dāvis Co-Authored-By: Alexander Graf Co-Authored-By: Thammus --- instaloader/instaloadercontext.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/instaloader/instaloadercontext.py b/instaloader/instaloadercontext.py index b8eec58..3b508a0 100644 --- a/instaloader/instaloadercontext.py +++ b/instaloader/instaloadercontext.py @@ -379,7 +379,18 @@ class InstaloaderContext: match = re.search(r'window\._sharedData = (.*);', resp.text) if match is None: raise ConnectionException("Could not find \"window._sharedData\" in html response.") - return json.loads(match.group(1)) + resp_json = json.loads(match.group(1)) + entry_data = resp_json.get('entry_data') + post_or_profile_page = list(entry_data.values())[0] if entry_data is not None else None + if post_or_profile_page is None: + raise ConnectionException("\"window._sharedData\" does not contain required keys.") + # If GraphQL data is missing in `window._sharedData`, search for it in `__additionalDataLoaded`. + if 'graphql' not in post_or_profile_page[0]: + match = re.search(r'window\.__additionalDataLoaded\([^{]+{"graphql":({.*})}\);', + resp.text) + if match is not None: + post_or_profile_page[0]['graphql'] = json.loads(match.group(1)) + return resp_json else: resp_json = resp.json() if 'status' in resp_json and resp_json['status'] != "ok":