From 89b56bdfa8af0c45d4c18cf60bbca12d7fb1bbf5 Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Fri, 21 Dec 2018 22:37:39 +0100 Subject: [PATCH] Login: Handle suspicious login and JSON error Motivated by #92. --- instaloader/instaloadercontext.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/instaloader/instaloadercontext.py b/instaloader/instaloadercontext.py index 2d605c2..bed073d 100644 --- a/instaloader/instaloadercontext.py +++ b/instaloader/instaloadercontext.py @@ -198,7 +198,10 @@ class InstaloaderContext: self._sleep() login = session.post('https://www.instagram.com/accounts/login/ajax/', data={'password': passwd, 'username': user}, allow_redirects=True) - resp_json = login.json() + try: + resp_json = login.json() + except json.decoder.JSONDecodeError: + raise ConnectionException("Login error: JSON decode fail, {} - {}.".format(login.status_code, login.reason)) if resp_json.get('two_factor_required'): two_factor_session = copy_session(session) two_factor_session.headers.update({'X-CSRFToken': csrf_token}) @@ -207,6 +210,10 @@ class InstaloaderContext: user, resp_json['two_factor_info']['two_factor_identifier']) raise TwoFactorAuthRequiredException("Login error: two-factor authentication required.") + if resp_json.get('checkpoint_url'): + raise ConnectionException("Login: Checkpoint required. Point your browser to " + "https://www.instagram.com{}, " + "follow the instructions, then retry.".format(resp_json.get('checkpoint_url'))) if resp_json['status'] != 'ok': if 'message' in resp_json: raise ConnectionException("Login error: \"{}\" status, message \"{}\".".format(resp_json['status'],