From 650112a59204b01bc2123ff3e55788f59b3462a5 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 8 Sep 2019 15:52:21 +0200 Subject: [PATCH] Remove automatic fb_dtsg refreshing This was error prone, inefficient and wouldn't handle all error cases. The real solution is to make some way to retry the request in the general case (since you can alway just get logged out), and that's probably out of scope for this project, at least right now. :/ --- fbchat/_client.py | 6 +++++- fbchat/_state.py | 48 +++++++---------------------------------------- 2 files changed, 12 insertions(+), 42 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index d85e0d8..dc66c51 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -817,6 +817,7 @@ class Client: image_id = str(image_id) data = {"photo_id": str(image_id)} j = self._post("/mercury/attachments/photo/", data) + _util.handle_payload_error(j) url = _util.get_jsmods_require(j, 3) if url is None: @@ -2017,6 +2018,7 @@ class Client: "https://{}-edge-chat.facebook.com/active_ping".format(self._pull_channel), data, ) + _util.handle_payload_error(j) def _pullMessage(self): """Call pull api to fetch message data.""" @@ -2028,9 +2030,11 @@ class Client: "clientid": self._state._client_id, "state": "active" if self._markAlive else "offline", } - return self._get( + j = self._get( "https://{}-edge-chat.facebook.com/pull".format(self._pull_channel), data ) + _util.handle_payload_error(j) + return j def _parseDelta(self, m): def getThreadIdAndThreadType(msg_metadata): diff --git a/fbchat/_state.py b/fbchat/_state.py index f8b489a..5215229 100644 --- a/fbchat/_state.py +++ b/fbchat/_state.py @@ -206,58 +206,24 @@ class State: session.cookies = requests.cookies.merge_cookies(session.cookies, cookies) return cls.from_session(session=session) - def _do_refresh(self): - # TODO: Raise the error instead, and make the user do the refresh manually - # It may be a bad idea to do this in an exception handler, if you have a better method, please suggest it! - log.warning("Refreshing state and resending request") - new = State.from_session(session=self._session) - self.user_id = new.user_id - self._fb_dtsg = new._fb_dtsg - self._revision = new._revision - self._counter = new._counter - self._logout_h = new._logout_h or self._logout_h - def _get(self, url, params, error_retries=3): params.update(self.get_params()) r = self._session.get(_util.prefix_url(url), params=params) content = _util.check_request(r) - j = _util.to_json(content) - try: - _util.handle_payload_error(j) - except _exception.FBchatPleaseRefresh: - if error_retries > 0: - self._do_refresh() - return self._get(url, params, error_retries=error_retries - 1) - raise - return j + return _util.to_json(content) - def _post(self, url, data, files=None, as_graphql=False, error_retries=3): + def _post(self, url, data, files=None, as_graphql=False): data.update(self.get_params()) r = self._session.post(_util.prefix_url(url), data=data, files=files) content = _util.check_request(r) - try: - if as_graphql: - return _graphql.response_to_json(content) - else: - j = _util.to_json(content) - # TODO: Remove this, and move it to _payload_post instead - # We can't yet, since errors raised in here need to be caught below - _util.handle_payload_error(j) - return j - except _exception.FBchatPleaseRefresh: - if error_retries > 0: - self._do_refresh() - return self._post( - url, - data, - files=files, - as_graphql=as_graphql, - error_retries=error_retries - 1, - ) - raise + if as_graphql: + return _graphql.response_to_json(content) + else: + return _util.to_json(content) def _payload_post(self, url, data, files=None): j = self._post(url, data, files=files) + _util.handle_payload_error(j) try: return j["payload"] except (KeyError, TypeError):