Refactor most of _postLogin into the State model
This commit is contained in:
@@ -266,32 +266,16 @@ class Client(object):
|
|||||||
self._session = requests.session()
|
self._session = requests.session()
|
||||||
self._seq = "0"
|
self._seq = "0"
|
||||||
self._uid = None
|
self._uid = None
|
||||||
|
self._client_id = hex(int(random() * 2147483648))[2:]
|
||||||
|
|
||||||
def _postLogin(self):
|
def _postLogin(self):
|
||||||
self._state = State()
|
|
||||||
self._client_id = hex(int(random() * 2147483648))[2:]
|
|
||||||
self._uid = self._session.cookies.get_dict().get("c_user")
|
self._uid = self._session.cookies.get_dict().get("c_user")
|
||||||
if self._uid is None:
|
if self._uid is None:
|
||||||
raise FBchatException("Could not find c_user cookie")
|
raise FBchatException("Could not find c_user cookie")
|
||||||
self._uid = str(self._uid)
|
self._uid = str(self._uid)
|
||||||
|
|
||||||
r = self._get("/")
|
r = self._get("/")
|
||||||
soup = bs(r.text, "html.parser")
|
self._state = State.from_base_request(r.text)
|
||||||
|
|
||||||
fb_dtsg_element = soup.find("input", {"name": "fb_dtsg"})
|
|
||||||
if fb_dtsg_element:
|
|
||||||
fb_dtsg = fb_dtsg_element["value"]
|
|
||||||
else:
|
|
||||||
fb_dtsg = re.search(r'name="fb_dtsg" value="(.*?)"', r.text).group(1)
|
|
||||||
|
|
||||||
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, logout_h=logout_h)
|
|
||||||
|
|
||||||
def _login(self, email, password):
|
def _login(self, email, password):
|
||||||
soup = bs(self._get("https://m.facebook.com/").text, "html.parser")
|
soup = bs(self._get("https://m.facebook.com/").text, "html.parser")
|
||||||
|
@@ -2,9 +2,13 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
|
import bs4
|
||||||
|
import re
|
||||||
|
|
||||||
from . import _util
|
from . import _util
|
||||||
|
|
||||||
|
FB_DTSG_REGEX = re.compile(r'name="fb_dtsg" value="(.*?)"')
|
||||||
|
|
||||||
|
|
||||||
@attr.s(slots=True, kw_only=True)
|
@attr.s(slots=True, kw_only=True)
|
||||||
class State(object):
|
class State(object):
|
||||||
@@ -29,3 +33,21 @@ class State(object):
|
|||||||
"__rev": self._revision,
|
"__rev": self._revision,
|
||||||
"fb_dtsg": self.fb_dtsg,
|
"fb_dtsg": self.fb_dtsg,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_base_request(cls, content):
|
||||||
|
soup = bs4.BeautifulSoup(content, "html.parser")
|
||||||
|
|
||||||
|
fb_dtsg_element = soup.find("input", {"name": "fb_dtsg"})
|
||||||
|
if fb_dtsg_element:
|
||||||
|
fb_dtsg = fb_dtsg_element["value"]
|
||||||
|
else:
|
||||||
|
# Fall back to searching with a regex
|
||||||
|
fb_dtsg = FB_DTSG_REGEX.search(content).group(1)
|
||||||
|
|
||||||
|
revision = int(content.split('"client_revision":', 1)[1].split(",", 1)[0])
|
||||||
|
|
||||||
|
logout_h_element = soup.find("input", {"name": "h"})
|
||||||
|
logout_h = logout_h_element["value"] if logout_h_element else None
|
||||||
|
|
||||||
|
return cls(fb_dtsg=fb_dtsg, revision=revision, logout_h=logout_h)
|
||||||
|
Reference in New Issue
Block a user