From 637b0ded0969f3c09dc424f83cc21d74ccc82882 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 27 Jun 2018 10:45:11 +0200 Subject: [PATCH] Added `FBchatRedirectError` --- fbchat/client.py | 16 ++++++++-------- fbchat/models.py | 9 +++++++++ fbchat/utils.py | 5 ++++- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index 79adb39..934abd4 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -96,13 +96,13 @@ class Client(object): self.req_counter += 1 return payload - def _fix_fb_errors(self, error_code): + def _fix_fb_errors(self, error): """ This fixes "Please try closing and re-opening your browser window" errors (1357004) This error usually happens after 1-2 days of inactivity It may be a bad idea to do this in an exception handler, if you have a better method, please suggest it! """ - if error_code == '1357004': + if isinstanceof(error, FBchatFacebookError) and error.fb_error_code == '1357004': log.warning('Got error #1357004. Doing a _postLogin, and resending request') self._postLogin() return True @@ -115,8 +115,8 @@ class Client(object): return r try: return check_request(r, as_json=as_json) - except FBchatFacebookError as e: - if error_retries > 0 and self._fix_fb_errors(e.fb_error_code): + except FBchatException as e: + if error_retries > 0 and self._fix_fb_errors(e): return self._get(url, query=query, timeout=timeout, fix_request=fix_request, as_json=as_json, error_retries=error_retries-1) raise e @@ -127,8 +127,8 @@ class Client(object): return r try: return check_request(r, as_json=as_json) - except FBchatFacebookError as e: - if error_retries > 0 and self._fix_fb_errors(e.fb_error_code): + except FBchatException as e: + if error_retries > 0 and self._fix_fb_errors(e): return self._post(url, query=query, timeout=timeout, fix_request=fix_request, as_json=as_json, error_retries=error_retries-1) raise e @@ -136,8 +136,8 @@ class Client(object): content = self._post(self.req_url.GRAPHQL, payload, fix_request=True, as_json=False) try: return graphql_response_to_json(content) - except FBchatFacebookError as e: - if error_retries > 0 and self._fix_fb_errors(e.fb_error_code): + except FBchatException as e: + if error_retries > 0 and self._fix_fb_errors(e): return self._graphql(payload, error_retries=error_retries-1) raise e diff --git a/fbchat/models.py b/fbchat/models.py index a380599..d6036dc 100644 --- a/fbchat/models.py +++ b/fbchat/models.py @@ -21,6 +21,15 @@ class FBchatFacebookError(FBchatException): self.fb_error_message = fb_error_message self.request_status_code = request_status_code +class FBchatRedirectError(Exception): + #: The url that Facebook redirected to + fb_redirect_url = None + def __init__(self, message, fb_redirect_url=None): + super(FBchatFacebookError, self).__init__(message) + """Thrown by fbchat when Facebook returns an unexpected redirect""" + self.fb_redirect_url = str(fb_redirect_url) + + class FBchatUserError(FBchatException): """Thrown by fbchat when wrong values are entered""" diff --git a/fbchat/utils.py b/fbchat/utils.py index 571a16e..57412f7 100644 --- a/fbchat/utils.py +++ b/fbchat/utils.py @@ -187,6 +187,10 @@ def generateOfflineThreadingID(): return str(int(msgs, 2)) def check_json(j): + if j.get('jsmods') is not None and j['jsmods'].get('require') is not None: + for x in j['jsmods']['require']: + if len(x) > 3 and x[0] == 'ServerRedirect' and x[1] == 'redirectPageTo': + return FBchatRedirectError('Redicrect when sending request: {}'.format(j), fb_redirect_url=x[3][0]) if j.get('error') is None: return if 'errorDescription' in j: @@ -216,7 +220,6 @@ def check_request(r, as_json=True): log.debug(j) return j else: - log.debug(content) return content def get_jsmods_require(j, index):