Repository: InstaPy/InstaPy

Issue:
KeyError: 'graphql'
`Traceback (most recent call last):
  File "quickstart.py", line 16, in <module>
    with smart_run(session):
  File "C:\Users\rwds\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Users\rwds\Desktop\a\instapy\util.py", line 1931, in smart_run
    session.login()
  File "C:\Users\rwds\Desktop\a\instapy\instapy.py", line 470, in login
    self.followed_by = log_follower_num(self.browser, self.username, self.logfolder)
  File "C:\Users\rwds\Desktop\a\instapy\print_log_writer.py", line 20, in log_follower_num
    followed_by = getUserData("graphql.user.edge_followed_by.count", browser)
  File "C:\Users\rwds\Desktop\a\instapy\util.py", line 469, in getUserData
    data = data[subobject]
KeyError: 'graphql'`

This happens right after Instapy tries to save account progress. 
`Unable to save account progress, skipping data update
        b"'graphql'"`


Discussion:
Same for mee 0.6.3 MacOS 

 Unable to save account progress, skipping data update 
	b'Message: TypeError: window._sharedData.entry_data.ProfilePage[0].graphql is undefined\n'
Same for mee.
```
Unable to save account progress, skipping data update 
	b"'ProfilePage'"
Sessional Live Report:
	|> No any statistics to show
[Session lasted 2.44 minutes]
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
INFO [2021-06-01 12:08:02] [xyz@gmail.com]  Session ended!
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
Traceback (most recent call last):
  File "/Users/macbook/Documents/instapy_bot.py", line 47, in <module>
    with smart_run(session):
  File "/usr/local/Cellar/python@3.7/3.7.8_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.7/site-packages/instapy/util.py", line 1931, in smart_run
    session.login()
  File "/usr/local/lib/python3.7/site-packages/instapy/instapy.py", line 470, in login
    self.followed_by = log_follower_num(self.browser, self.username, self.logfolder)
  File "/usr/local/lib/python3.7/site-packages/instapy/print_log_writer.py", line 20, in log_follower_num
    followed_by = getUserData("graphql.user.edge_followed_by.count", browser)
  File "/usr/local/lib/python3.7/site-packages/instapy/util.py", line 462, in getUserData
    data = shared_data["entry_data"]["ProfilePage"][0]
KeyError: 'ProfilePage'
```

possibly related to #6195 and fixed by PR #6195. Can you please check if that PR fixes your issue too and leave feedback?

Resolving patch:
diff --git a/instapy/util.py b/instapy/util.py
--- a/instapy/util.py
+++ b/instapy/util.py
@@ -79,9 +79,14 @@ def is_private_profile(browser, logger, following=True):
     """
 
     shared_data = get_shared_data(browser)
-    is_private = shared_data["entry_data"]["ProfilePage"][0]["graphql"]["user"][
-        "is_private"
-    ]
+    data = shared_data["entry_data"]["ProfilePage"][0]
+
+    # Sometimes shared_data["entry_data"]["ProfilePage"][0] is empty, but get_additional_data()
+    # fetches all data needed
+    if not data:
+        data = get_additional_data(browser)
+
+    is_private = data["graphql"]["user"]["is_private"]
 
     return is_private
 
@@ -482,6 +487,11 @@ def getUserData(
     shared_data = get_shared_data(browser)
     data = shared_data["entry_data"]["ProfilePage"][0]
 
+    # Sometimes shared_data["entry_data"]["ProfilePage"][0] is empty, but get_additional_data()
+    # fetches all data needed
+    if not data:
+        data = get_additional_data(browser)
+
     if query.find(".") == -1:
         data = data[query]
     else:
@@ -2593,7 +2603,8 @@ def get_additional_data(browser):
     soup = BeautifulSoup(browser.page_source, "html.parser")
     for text in soup(text=re.compile(r"window.__additionalDataLoaded")):
         if re.search("^window.__additionalDataLoaded", text):
-            additional_data = json.loads(text[48:-2])
+            additional_data = json.loads(re.search('{.*}', text).group())
+            break
 
     return additional_data
 
@@ -2610,6 +2621,7 @@ def get_shared_data(browser):
     soup = BeautifulSoup(browser.page_source, "html.parser")
     for text in soup(text=re.compile(r"window._sharedData")):
         if re.search("^window._sharedData", text):
-            shared_data = json.loads(text[21:-1])
+            shared_data = json.loads(re.search('{.*}', text).group())
+            break
 
     return shared_data


Test patch: