Refactor most of _postLogin into the State model

This commit is contained in:
Mads Marquart
2019-05-02 21:04:00 +02:00
parent a4268f36cf
commit 56786406ec
2 changed files with 24 additions and 18 deletions

View File

@@ -2,9 +2,13 @@
from __future__ import unicode_literals
import attr
import bs4
import re
from . import _util
FB_DTSG_REGEX = re.compile(r'name="fb_dtsg" value="(.*?)"')
@attr.s(slots=True, kw_only=True)
class State(object):
@@ -29,3 +33,21 @@ class State(object):
"__rev": self._revision,
"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)