From 466f27a8c5c7d38c167cd60992f901b8ee717211 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 26 Jun 2019 23:44:14 +0200 Subject: [PATCH] Move login check code into State --- fbchat/_client.py | 21 +++------------------ fbchat/_state.py | 6 ++++++ 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index bff06db..6ff03a7 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -120,19 +120,9 @@ class Client(object): return True return False - def _get( - self, - url, - query=None, - fix_request=False, - as_json=False, - error_retries=3, - allow_redirects=True, - ): + def _get(self, url, query=None, fix_request=False, as_json=False, error_retries=3): payload = self._generatePayload(query) - r = self._state._session.get( - prefix_url(url), params=payload, allow_redirects=allow_redirects - ) + r = self._state._session.get(prefix_url(url), params=payload) if not fix_request: return r try: @@ -145,7 +135,6 @@ class Client(object): fix_request=fix_request, as_json=as_json, error_retries=error_retries - 1, - allow_redirects=allow_redirects, ) raise e @@ -248,11 +237,7 @@ class Client(object): :return: True if the client is still logged in :rtype: bool """ - # Send a request to the login url, to see if we're directed to the home page - 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"] + return self._state.is_logged_in() def getSession(self): """Retrieves session cookies diff --git a/fbchat/_state.py b/fbchat/_state.py index 03f9970..75b9dff 100644 --- a/fbchat/_state.py +++ b/fbchat/_state.py @@ -139,6 +139,12 @@ class State(object): "(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): logout_h = self._logout_h if not logout_h: