Move login check code into State
This commit is contained in:
@@ -120,19 +120,9 @@ class Client(object):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _get(
|
def _get(self, url, query=None, fix_request=False, as_json=False, error_retries=3):
|
||||||
self,
|
|
||||||
url,
|
|
||||||
query=None,
|
|
||||||
fix_request=False,
|
|
||||||
as_json=False,
|
|
||||||
error_retries=3,
|
|
||||||
allow_redirects=True,
|
|
||||||
):
|
|
||||||
payload = self._generatePayload(query)
|
payload = self._generatePayload(query)
|
||||||
r = self._state._session.get(
|
r = self._state._session.get(prefix_url(url), params=payload)
|
||||||
prefix_url(url), params=payload, allow_redirects=allow_redirects
|
|
||||||
)
|
|
||||||
if not fix_request:
|
if not fix_request:
|
||||||
return r
|
return r
|
||||||
try:
|
try:
|
||||||
@@ -145,7 +135,6 @@ class Client(object):
|
|||||||
fix_request=fix_request,
|
fix_request=fix_request,
|
||||||
as_json=as_json,
|
as_json=as_json,
|
||||||
error_retries=error_retries - 1,
|
error_retries=error_retries - 1,
|
||||||
allow_redirects=allow_redirects,
|
|
||||||
)
|
)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
@@ -248,11 +237,7 @@ class Client(object):
|
|||||||
:return: True if the client is still logged in
|
:return: True if the client is still logged in
|
||||||
:rtype: bool
|
:rtype: bool
|
||||||
"""
|
"""
|
||||||
# Send a request to the login url, to see if we're directed to the home page
|
return self._state.is_logged_in()
|
||||||
r = self._get(
|
|
||||||
"https://m.facebook.com/login.php?login_attempt=1", allow_redirects=False
|
|
||||||
)
|
|
||||||
return "Location" in r.headers and "home" in r.headers["Location"]
|
|
||||||
|
|
||||||
def getSession(self):
|
def getSession(self):
|
||||||
"""Retrieves session cookies
|
"""Retrieves session cookies
|
||||||
|
@@ -139,6 +139,12 @@ class State(object):
|
|||||||
"(Failed on url: {})".format(r.url)
|
"(Failed on url: {})".format(r.url)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def is_logged_in(self):
|
||||||
|
# Send a request to the login url, to see if we're directed to the home page
|
||||||
|
url = "https://m.facebook.com/login.php?login_attempt=1"
|
||||||
|
r = self._session.get(url, allow_redirects=False)
|
||||||
|
return "Location" in r.headers and "home" in r.headers["Location"]
|
||||||
|
|
||||||
def logout(self):
|
def logout(self):
|
||||||
logout_h = self._logout_h
|
logout_h = self._logout_h
|
||||||
if not logout_h:
|
if not logout_h:
|
||||||
|
Reference in New Issue
Block a user