Always create the State object in a valid state
This commit is contained in:
@@ -78,9 +78,7 @@ class Client(object):
|
|||||||
:raises: FBchatException on failed login
|
:raises: FBchatException on failed login
|
||||||
"""
|
"""
|
||||||
self._sticky, self._pool = (None, None)
|
self._sticky, self._pool = (None, None)
|
||||||
self._state = State.with_user_agent(user_agent=user_agent)
|
|
||||||
self._seq = "0"
|
self._seq = "0"
|
||||||
self._uid = None
|
|
||||||
self._client_id = hex(int(random() * 2 ** 31))[2:]
|
self._client_id = hex(int(random() * 2 ** 31))[2:]
|
||||||
self._default_thread_id = None
|
self._default_thread_id = None
|
||||||
self._default_thread_type = None
|
self._default_thread_type = None
|
||||||
@@ -93,7 +91,7 @@ class Client(object):
|
|||||||
# If session cookies aren't set, not properly loaded or gives us an invalid session, then do the login
|
# If session cookies aren't set, not properly loaded or gives us an invalid session, then do the login
|
||||||
if (
|
if (
|
||||||
not session_cookies
|
not session_cookies
|
||||||
or not self.setSession(session_cookies)
|
or not self.setSession(session_cookies, user_agent=user_agent)
|
||||||
or not self.isLoggedIn()
|
or not self.isLoggedIn()
|
||||||
):
|
):
|
||||||
self.login(email, password, max_tries, user_agent=user_agent)
|
self.login(email, password, max_tries, user_agent=user_agent)
|
||||||
@@ -247,7 +245,7 @@ class Client(object):
|
|||||||
"""
|
"""
|
||||||
return self._state.get_cookies()
|
return self._state.get_cookies()
|
||||||
|
|
||||||
def setSession(self, session_cookies):
|
def setSession(self, session_cookies, user_agent=None):
|
||||||
"""Loads session cookies
|
"""Loads session cookies
|
||||||
|
|
||||||
:param session_cookies: A dictionay containing session cookies
|
:param session_cookies: A dictionay containing session cookies
|
||||||
@@ -255,23 +253,17 @@ class Client(object):
|
|||||||
:return: False if `session_cookies` does not contain proper cookies
|
:return: False if `session_cookies` does not contain proper cookies
|
||||||
:rtype: bool
|
:rtype: bool
|
||||||
"""
|
"""
|
||||||
# Quick check to see if session_cookies is formatted properly
|
|
||||||
if not session_cookies or "c_user" not in session_cookies:
|
|
||||||
return False
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Load cookies into current session
|
# Load cookies into current session
|
||||||
self._state = State.from_cookies(
|
state = State.from_cookies(session_cookies, user_agent=user_agent)
|
||||||
session_cookies, user_agent=self._state._session.headers["User-Agent"]
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception("Failed loading session")
|
log.exception("Failed loading session")
|
||||||
self._state = State()
|
|
||||||
return False
|
return False
|
||||||
uid = self._state.get_user_id()
|
uid = state.get_user_id()
|
||||||
if uid is None:
|
if uid is None:
|
||||||
log.warning("Could not find c_user cookie")
|
log.warning("Could not find c_user cookie")
|
||||||
return False
|
return False
|
||||||
|
self._state = state
|
||||||
self._uid = uid
|
self._uid = uid
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@@ -82,8 +82,8 @@ class State(object):
|
|||||||
"""Stores and manages state required for most Facebook requests."""
|
"""Stores and manages state required for most Facebook requests."""
|
||||||
|
|
||||||
_session = attr.ib(factory=session_factory)
|
_session = attr.ib(factory=session_factory)
|
||||||
fb_dtsg = attr.ib(None)
|
fb_dtsg = attr.ib()
|
||||||
_revision = attr.ib(None)
|
_revision = attr.ib()
|
||||||
_counter = attr.ib(0)
|
_counter = attr.ib(0)
|
||||||
_logout_h = attr.ib(None)
|
_logout_h = attr.ib(None)
|
||||||
|
|
||||||
@@ -94,8 +94,6 @@ class State(object):
|
|||||||
return str(rtn)
|
return str(rtn)
|
||||||
|
|
||||||
def get_params(self):
|
def get_params(self):
|
||||||
if self.fb_dtsg is None:
|
|
||||||
return {}
|
|
||||||
self._counter += 1 # TODO: Make this operation atomic / thread-safe
|
self._counter += 1 # TODO: Make this operation atomic / thread-safe
|
||||||
return {
|
return {
|
||||||
"__a": 1,
|
"__a": 1,
|
||||||
@@ -185,7 +183,3 @@ class State(object):
|
|||||||
session = session_factory(user_agent=user_agent)
|
session = session_factory(user_agent=user_agent)
|
||||||
session.cookies = requests.cookies.merge_cookies(session.cookies, cookies)
|
session.cookies = requests.cookies.merge_cookies(session.cookies, cookies)
|
||||||
return cls.from_session(session=session)
|
return cls.from_session(session=session)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def with_user_agent(cls, user_agent=None, **kwargs):
|
|
||||||
return cls(session=session_factory(user_agent=user_agent), **kwargs)
|
|
||||||
|
Reference in New Issue
Block a user