From 97049556ed99be9f401975e45a4e2839138d76e7 Mon Sep 17 00:00:00 2001 From: Oren Date: Tue, 8 May 2018 12:41:22 +0300 Subject: [PATCH] Update obtaining fb_dtsg and fb_h fb_dtsg is sometimes returned inside an HTML comment, and beautifulsoup can't find it - in that case we'll use a regular expression to extract it. fb_h is sometimes not returned in the HTML of req_url.BASE (in my experience, when resuming a session using session_cookies). Following the discussion here: https://github.com/Schmavery/facebook-chat-api/issues/505 I learned it is used for logging out, and can be found in the response of `https://www.facebook.com/bluebar/modern_settings_menu/`. I included support for fetching it from there. Because this library is used many more times for logging in, than for logging out, instead of adding an extra HTTP request during login, I decided to perform it during logout, only in case fb_h is not found in the HTML of req_url.BASE. --- fbchat/client.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index 4698155..91f2b74 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -209,8 +209,18 @@ class Client(object): r = self._get(self.req_url.BASE) soup = bs(r.text, "lxml") - self.fb_dtsg = soup.find("input", {'name':'fb_dtsg'})['value'] - self.fb_h = soup.find("input", {'name':'h'})['value'] + + fb_dtsg_element = soup.find("input", {'name': 'fb_dtsg'}) + if fb_dtsg_element: + self.fb_dtsg = fb_dtsg_element['value'] + else: + self.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'] + for i in self.fb_dtsg: self.ttstamp += str(ord(i)) self.ttstamp += '2' @@ -400,6 +410,11 @@ class Client(object): :return: True if the action was successful :rtype: bool """ + + if not hasattr(self, 'fb_h'): + h_r = self._post(self.req_url.MODERN_SETTINGS_MENU, {'pmid': '4'}) + self.fb_h = re.search(r'name=\\"h\\" value=\\"(.*?)\\"', h_r.text).group(1) + data = { 'ref': "mb", 'h': self.fb_h