From a4268f36cf4a955a9fdac91ed4a9e67219fe826b Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 2 May 2019 20:50:25 +0200 Subject: [PATCH] Move logout `h` into the State model --- fbchat/_client.py | 18 +++++++++--------- fbchat/_state.py | 5 +++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index 05404fc..0bcb96b 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -284,13 +284,14 @@ class Client(object): else: fb_dtsg = re.search(r'name="fb_dtsg" value="(.*?)"', r.text).group(1) - fb_h_element = soup.find("input", {"name": "h"}) - if fb_h_element: - self._fb_h = fb_h_element["value"] + logout_h = None + logout_h_element = soup.find("input", {"name": "h"}) + if logout_h_element: + logout_h = logout_h_element["value"] revision = int(r.text.split('"client_revision":', 1)[1].split(",", 1)[0]) - self._state = State(fb_dtsg=fb_dtsg, revision=revision) + self._state = State(fb_dtsg=fb_dtsg, revision=revision, logout_h=logout_h) def _login(self, email, password): soup = bs(self._get("https://m.facebook.com/").text, "html.parser") @@ -468,16 +469,15 @@ class Client(object): :return: True if the action was successful :rtype: bool """ - if not hasattr(self, "_fb_h"): + logout_h = self._state.logout_h + if not logout_h: h_r = self._post("/bluebar/modern_settings_menu/", {"pmid": "4"}) - self._fb_h = re.search(r'name=\\"h\\" value=\\"(.*?)\\"', h_r.text).group(1) + logout_h = re.search(r'name=\\"h\\" value=\\"(.*?)\\"', h_r.text).group(1) - data = {"ref": "mb", "h": self._fb_h} + data = {"ref": "mb", "h": logout_h} r = self._get("/logout.php", data) - self._resetValues() - return r.ok """ diff --git a/fbchat/_state.py b/fbchat/_state.py index a5892e9..a0542cc 100644 --- a/fbchat/_state.py +++ b/fbchat/_state.py @@ -13,6 +13,11 @@ class State(object): fb_dtsg = attr.ib(None) _revision = attr.ib(None) _counter = attr.ib(0) + _logout_h = attr.ib(None) + + @property + def logout_h(self): + return self._logout_h def get_params(self): if self.fb_dtsg is None: